Skip to content

Commit

Permalink
Automatically retrieve PlexOnlineToken from PMS hidden settings
Browse files Browse the repository at this point in the history
  • Loading branch information
JonnyWong16 committed Dec 5, 2021
1 parent aeb0bfb commit 393c3f9
Showing 1 changed file with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
import logging
import os
import peewee
import platform
import requests
import _winreg as winreg
import plistlib
import xml.etree.ElementTree as ET

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -240,10 +244,48 @@ def get_token(cls, request_headers=None):
# Environment token
env_token = os.environ.get('PLEXTOKEN')

if env_token:
if env_token and not env_token.startswith('local-'):
log.info('Plex Token: environment')
return env_token

# Windows Registry token
if platform.system() == 'Windows':
try:
reg_path = "Software\\Plex, Inc.\\Plex Media Server"
registry_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, reg_path, 0, winreg.KEY_ALL_ACCESS)
reg_token = winreg.QueryValueEx(registry_key, 'PlexOnlineToken')[0]
except:
reg_token = None

if reg_token:
log.info('Plex Token: Windows Registry')
return reg_token

# MacOS plist token
elif platform.system() == 'Darwin':
try:
plist_file = plistlib.readPlist(os.path.expanduser('~/Library/Preferences/com.plexapp.plexmediaserver.plist'))
plist_token = plist_file['PlexOnlineToken']
except:
plist_token = None

if plist_token:
log.info('Plex Token: MacOS plist')
return plist_token

# Linux Preferences.xml
else:
try:
xml_tree = ET.parse(os.path.join(os.environ.get('HOME'), 'Preferences.xml'))
xml_root = xml_tree.getroot()
xml_token = xml_root.attrib.get('PlexOnlineToken')
except:
xml_token = None

if xml_token:
log.info('Plex Token: Preferences.xml')
return xml_token

# Check if anonymous access is available
server = requests.get('http://localhost:32400')

Expand Down

0 comments on commit 393c3f9

Please sign in to comment.