diff --git a/README.md b/README.md index 69786237..dc6fd794 100644 --- a/README.md +++ b/README.md @@ -249,6 +249,7 @@ Send a Plex notification as the final step when all processing is completed. Thi - `refresh` - `True`/`False` - Enable or disable the feature - `host` - Plex hostname. Default `localhost` - `port` - Plex port. Default `32400` + - `token` - Plex Home Token Manual Script Usage -------------- diff --git a/autoProcess.ini.sample b/autoProcess.ini.sample index 4274cd33..6f6798b7 100644 --- a/autoProcess.ini.sample +++ b/autoProcess.ini.sample @@ -99,3 +99,4 @@ api_key= host=localhost port=32400 refresh=True +token= diff --git a/autoprocess/plex.py b/autoprocess/plex.py index 2aae1ba2..89931886 100644 --- a/autoprocess/plex.py +++ b/autoprocess/plex.py @@ -1,22 +1,41 @@ #!/usr/bin/env python import urllib +import logging from xml.dom import minidom -def refreshPlex(settings, source_type): - host = settings.Plex['host'] - port = settings.Plex['port'] - approved_sources = ['movie', 'show'] - if settings.Plex['refresh'] and source_type in approved_sources: - base_url = 'http://%s:%s/library/sections' % (host, port) - refresh_url = '%s/%%s/refresh' % base_url +def refreshPlex(settings, source_type, logger=None): + if logger: + log = logger + else: + log = logging.getLogger(__name__) - try: - xml_sections = minidom.parse(urllib.urlopen(base_url)) - sections = xml_sections.getElementsByTagName('Directory') - for s in sections: - if s.getAttribute('type') == source_type: - url = refresh_url % s.getAttribute('key') - x = urllib.urlopen(url) - except Exception as e: - print "Unable to refresh plex, check your settings" - print e + host = settings.Plex['host'] + port = settings.Plex['port'] + token = settings.Plex['token'] + + log.debug("Host: %s." % host) + log.debug("Port: %s." % port) + log.debug("Token: %s." % token) + + approved_sources = ['movie', 'show'] + if settings.Plex['refresh'] and source_type in approved_sources: + base_url = 'http://%s:%s/library/sections' % (host, port) + refresh_url = '%s/%%s/refresh' % base_url + + if token: + refresh_url = refresh_url + "?X-Plex-Token=" + token + base_url = base_url + "?X-Plex-Token=" + token + log.debug("Plex home token detected.") + + log.debug("Refresh URL: %s." % refresh_url) + log.debug("Base URL: %s." % base_url) + + try: + xml_sections = minidom.parse(urllib.urlopen(base_url)) + sections = xml_sections.getElementsByTagName('Directory') + for s in sections: + if s.getAttribute('type') == source_type: + url = refresh_url % s.getAttribute('key') + x = urllib.urlopen(url) + except Exception: + log.exception("Unable to refresh plex, check your settings.") diff --git a/postConversion.py b/postConversion.py index 4c1b7781..254a733d 100755 --- a/postConversion.py +++ b/postConversion.py @@ -58,7 +58,7 @@ except (IOError, ValueError): log.exception("Couldn't refresh Sickbeard, check your autoProcess.ini settings.") - plex.refreshPlex(settings, 'show') + plex.refreshPlex(settings, 'show', log) else: log.error("Not enough command line arguments present %s." % len(sys.argv)) diff --git a/readSettings.py b/readSettings.py index 4c86a42c..6a79e5e5 100644 --- a/readSettings.py +++ b/readSettings.py @@ -130,7 +130,8 @@ def __init__(self, directory, filename, logger=None): # Default Plex Settings plex_defaults = {'host': 'localhost', 'port': '32400', - 'refresh': 'true'} + 'refresh': 'true', + 'token': ''} defaults = {'SickBeard': sb_defaults, 'CouchPotato': cp_defaults, 'Sonarr': sonarr_defaults, 'MP4': mp4_defaults, 'uTorrent': utorrent_defaults, 'SABNZBD': sab_defaults, 'Sickrage': sr_defaults, 'Deluge': deluge_defaults, 'Plex': plex_defaults} write = False # Will be changed to true if a value is missing from the config file and needs to be written @@ -465,6 +466,9 @@ def __init__(self, directory, filename, logger=None): self.Plex['refresh'] = config.getboolean(section, "refresh") except: self.Plex['refresh'] = False + self.Plex['token'] = config.get(section, "token") + if self.Plex['token'] == '': + self.Plex['token'] = None; #Pass the values on self.config = config diff --git a/setup/PostProcess/main.py b/setup/PostProcess/main.py index 4381898f..1f028f63 100644 --- a/setup/PostProcess/main.py +++ b/setup/PostProcess/main.py @@ -71,6 +71,6 @@ def callscript(self, message = None, group = None): except: log.error('File processing failed: %s', (traceback.format_exc())) - plex.refreshPlex(settings, 'movie') + plex.refreshPlex(settings, 'movie', log) return success