Get the username and device name

Hi there,

I’m trying to add some additional logging information for the WOL Plugin. Can someone tell me please the variable names, or how to get the name of the user that’s invoked the plugin, plus the name of the device that they are using please? I’ve had a look around and I can’t find it.

Thanks,

SpacemanJT

You can get some info out of Request.Headers. There’s also Client.Product and Client.Platform

I don’t think there’s an easy way to get the username. The only identifier is the X-Plex-Token header. To link it to a username you need to talk to plex.tv.

I have a function that can do it but it takes 2 http requests.

import os

def get_username():
    machine_id = XML.ElementFromURL('http://' + Request.Headers['Host'], headers={'X-Plex-Token': os.environ['PLEXTOKEN']}).get('machineIdentifier')
    access_tokens = XML.ElementFromURL('https://plex.tv/servers/{}/access_tokens.xml?auth_token={}'.format(machine_id, os.environ['PLEXTOKEN']))
    for child in access_tokens.getchildren():
        if child.get('token') == Request.Headers['X-Plex-Token']:
            username = child.get('username')
            return username if username else child.get('title')
    return None


def MainMenu():
    Log.Info('{} just loaded the main menu from {} {}.'.format(
        get_username(), 
        Request.Headers['X-Plex-Device-Name'],
        Request.Headers['X-Plex-Product']))
INFO (logkit:16) - coryo123 just loaded the main menu from Cory-PC Plex Media Player.
INFO (logkit:16) - coryo123 just loaded the main menu from My Nexus 4 Plex for Android.

Many thanks Coryo123, I’ll give it a try :slight_smile:

For machine_id = XML.ElementFromURL('http://' + Request.Headers['Host'], headers={'X-Plex-Token': os.environ['PLEXTOKEN']}).get('machineIdentifier') you should be able to use Platform.MachineIdentifier, which saves you another request.