Skip to content

Commit

Permalink
plex home support
Browse files Browse the repository at this point in the history
Adds the `token` option for plex home token refreshing
  • Loading branch information
michael committed Jul 1, 2015
1 parent 1bc7148 commit 85e2a18
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 20 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------------
Expand Down
1 change: 1 addition & 0 deletions autoProcess.ini.sample
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,4 @@ api_key=
host=localhost
port=32400
refresh=True
token=
53 changes: 36 additions & 17 deletions autoprocess/plex.py
Original file line number Diff line number Diff line change
@@ -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.")
2 changes: 1 addition & 1 deletion postConversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 5 additions & 1 deletion readSettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup/PostProcess/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 85e2a18

Please sign in to comment.