From 042fcce6b7a7ab0f697af68d6f4e0eff25fd159b Mon Sep 17 00:00:00 2001 From: clinton-hall Date: Wed, 27 May 2015 22:03:13 +0930 Subject: [PATCH] add plex library update. Fixes #566 --- TorrentToMedia.py | 4 +++- autoProcessMedia.cfg.spec | 9 +++++++++ core/__init__.py | 18 +++++++++++++++++- core/nzbToMediaUtil.py | 19 +++++++++++++++++++ nzbToMedia.py | 4 +++- 5 files changed, 51 insertions(+), 3 deletions(-) diff --git a/TorrentToMedia.py b/TorrentToMedia.py index 5f7ce106..730ad364 100755 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -8,7 +8,7 @@ import core from subprocess import Popen from core import logger, nzbToMediaDB -from core.nzbToMediaUtil import convert_to_ascii, CharReplace +from core.nzbToMediaUtil import convert_to_ascii, CharReplace, plex_update from core.nzbToMediaUserScript import external_script def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID, clientAgent): @@ -217,6 +217,8 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID, elif sectionName == 'Gamez': result = core.autoProcessGames().process(sectionName,outputDestination, inputName, status, clientAgent, inputCategory) + plex_update(inputCategory) + if result[0] != 0: if clientAgent != 'manual': logger.error( diff --git a/autoProcessMedia.cfg.spec b/autoProcessMedia.cfg.spec index d9264a78..1d493f15 100644 --- a/autoProcessMedia.cfg.spec +++ b/autoProcessMedia.cfg.spec @@ -246,6 +246,15 @@ audioExtensions = .mp3, .aac, .ogg, .ape, .m4a, .asf, .wma, .flac metaExtensions = .nfo,.sub,.srt,.jpg,.gif +[Plex] + plex_host = localhost + plex_port = 32400 + plex_token = + plex_ssl = 0 + # Enter Plex category to section mapping as Category,section and separate each pair with '|' + # e.g. plex_sections = movie,3|tv,4 + plex_sections = + [Transcoder] # getsubs. enable to download subtitles. getSubs = 0 diff --git a/core/__init__.py b/core/__init__.py index b8ba6193..55bb6d8d 100644 --- a/core/__init__.py +++ b/core/__init__.py @@ -116,6 +116,12 @@ DELUGEPORT = None DELUGEUSR = None DELUGEPWD = None +PLEXSSL = None +PLEXHOST = None +PLEXPORT = None +PLEXTOKEN = None +PLEXSEC = None + EXTCONTAINER = [] COMPRESSEDCONTAINER = [] MEDIACONTAINER = [] @@ -206,7 +212,8 @@ def initialize(section=None): NICENESS, LOG_DEBUG, FORCE_CLEAN, FFMPEG_PATH, FFMPEG, FFPROBE, AUDIOCONTAINER, EXTCONTAINER, TORRENT_CLASS, \ DELETE_ORIGINAL, PASSWORDSFILE, USER_DELAY, USER_SCRIPT, USER_SCRIPT_CLEAN, USER_SCRIPT_MEDIAEXTENSIONS, \ USER_SCRIPT_PARAM, USER_SCRIPT_RUNONCE, USER_SCRIPT_SUCCESSCODES, DOWNLOADINFO, CHECK_MEDIA, SAFE_MODE, \ - TORRENT_DEFAULTDIR, NZB_DEFAULTDIR, REMOTEPATHS, LOG_ENV, PID_FILE, MYAPP, ACHANNELS, ACHANNELS2, ACHANNELS3 + TORRENT_DEFAULTDIR, NZB_DEFAULTDIR, REMOTEPATHS, LOG_ENV, PID_FILE, MYAPP, ACHANNELS, ACHANNELS2, ACHANNELS3, \ + PLEXSSL, PLEXHOST, PLEXPORT, PLEXTOKEN, PLEXSEC if __INITIALIZED__: return False @@ -351,6 +358,15 @@ def initialize(section=None): if isinstance(REMOTEPATHS, list): REMOTEPATHS = ','.join(REMOTEPATHS) # fix in case this imported as list. REMOTEPATHS = [ tuple(item.split(',')) for item in REMOTEPATHS.split('|') ] # /volume1/Public/,E:\|/volume2/share/,\\NAS\ + PLEXSSL = int(CFG["Plex"]["plex_ssl"]) + PLEXHOST = CFG["Plex"]["plex_host"] + PLEXPORT = CFG["Plex"]["plex_port"] + PLEXTOKEN = CFG["Plex"]["plex_token"] + PLEXSEC = CFG["Plex"]["plex_sections"] or None + if PLEXSEC: + if isinstance(PLEXSEC, list): PLEXSEC = ','.join(PLEXSEC) # fix in case this imported as list. + PLEXSEC = [ tuple(item.split(',')) for item in PLEXSEC.split('|') ] + devnull = open(os.devnull, 'w') try: subprocess.Popen(["nice"], stdout=devnull, stderr=devnull).communicate() diff --git a/core/nzbToMediaUtil.py b/core/nzbToMediaUtil.py index 3e7610eb..d4d0fd68 100644 --- a/core/nzbToMediaUtil.py +++ b/core/nzbToMediaUtil.py @@ -1053,6 +1053,25 @@ def server_responding(baseURL): except (requests.ConnectionError, requests.exceptions.Timeout): return False +def plex_update(category): + logger.debug("Attempting to update Plex Library for category %s." %(category), 'PLEX') + if core.PLEXSSL: + ulr = 'https://' + else: + url = 'http://' + url = url + core.PLEXHOST + ':' + core.PLEXPORT + '/library/sections/' + section = None + for item in core.PLEXSEC: + if item[0] == category: + section = item[1] + + if section: + url = url + section + '/refresh?X-Plex-Token=' + core.PLEXTOKEN + requests.get(url, timeout=(60, 120), verify=False) + logger.debug("Plex Library has been refreshed.", 'PLEX') + else: + logger.debug("Could not identify section for plex update", 'PLEX') + def backupVersionedFile(old_file, version): numTries = 0 diff --git a/nzbToMedia.py b/nzbToMedia.py index a445652d..16a04a73 100755 --- a/nzbToMedia.py +++ b/nzbToMedia.py @@ -506,7 +506,7 @@ from core.autoProcess.autoProcessGames import autoProcessGames from core.autoProcess.autoProcessMovie import autoProcessMovie from core.autoProcess.autoProcessMusic import autoProcessMusic from core.autoProcess.autoProcessTV import autoProcessTV -from core.nzbToMediaUtil import getDirs, extractFiles, cleanDir, update_downloadInfoStatus, get_downloadInfo, CharReplace, convert_to_ascii, get_nzoid +from core.nzbToMediaUtil import getDirs, extractFiles, cleanDir, update_downloadInfoStatus, get_downloadInfo, CharReplace, convert_to_ascii, get_nzoid, plex_update from core.nzbToMediaUserScript import external_script from core import logger, nzbToMediaDB @@ -608,6 +608,8 @@ def process(inputDirectory, inputName=None, status=0, clientAgent='manual', down else: result = [-1, ""] + plex_update(inputCategory) + if result[0] == 0: if clientAgent != 'manual': # update download status in our DB