From 393c3f9fc252d66ac05d7a7ab02449947c09fcb2 Mon Sep 17 00:00:00 2001 From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> Date: Sun, 5 Dec 2021 14:03:23 -0800 Subject: [PATCH] Automatically retrieve PlexOnlineToken from PMS hidden settings --- .../plugin/modules/migrations/account.py | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/Trakttv.bundle/Contents/Libraries/Shared/plugin/modules/migrations/account.py b/Trakttv.bundle/Contents/Libraries/Shared/plugin/modules/migrations/account.py index 5f7404f64..6bd342781 100644 --- a/Trakttv.bundle/Contents/Libraries/Shared/plugin/modules/migrations/account.py +++ b/Trakttv.bundle/Contents/Libraries/Shared/plugin/modules/migrations/account.py @@ -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__) @@ -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')