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 5da1c2d
Showing 1 changed file with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import logging
import os
import peewee
import platform
import requests

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -240,10 +241,54 @@ 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':
import _winreg as winreg

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':
import plistlib

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:
import xml.etree.ElementTree as ET

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 5da1c2d

Please sign in to comment.