diff --git a/README.md b/README.md index 448479eb..fe220960 100644 --- a/README.md +++ b/README.md @@ -88,11 +88,18 @@ Couch Potato Support uTorrent Support (BETA) -------------- - `uTorrentToSickbeardwithConverter.py` is the file you'll be using here. This script will allow post processing of torrent files with conversion and will forward the converted files to Sickbeard for final post processing. -- Currently the script will not touch the actual download but will instead make copies (the script will obey settings regarding output directory, copyto, moveto etc) and process those copies, so you can continue to seed your torrents. -- uTorrent must be set up with the following post command options: ```#Args: %L %T %D %K %F``` +- ** YOU MUST INSTALL THE PYTHON REQUESTS LIBRARY FOR THIS TO COMMUNICATE WITH UTORRENT. ** Run "pip install requests" to install. TODO: Better instructions. +- uTorrent Web UI should also be enabled. +- uTorrent must be set up with the following post command options: ```#Args: %L %S %D %K %F %S %I``` - Picture: http://i.imgur.com/7eADkCI.png -- Set your uTorrent Label whitelist in autoProcess.ini +- Set your uTorrent settings in autoProcess.ini - Set `label` to match your uTorrent label, files without this corrrect label will be ignored. If no label is set, all torrents will be processed. + - Set `webui` to True/False. If True the script can change the state of the torrent. + - Set `action_before` to stop/pause or any other action from http://help.utorrent.com/customer/portal/articles/1573952-actions---webapi + - Set `action_after` to start/stop/pause/unpause/remove/removedata or any other action from http://help.utorrent.com/customer/portal/articles/1573952-actions---webapi + - Set `hostname` to your uTorrent Web UI URL, eg http://localhost:8080/ including the trailing slash. + - Set `username` to your uTorrent Web UI username + - Set `password` to your uTorrent Web UI password Manual Script Usage -------------- diff --git a/autoProcess.ini.sample b/autoProcess.ini.sample index 97192daa..306e35a6 100644 --- a/autoProcess.ini.sample +++ b/autoProcess.ini.sample @@ -55,4 +55,10 @@ method = renamer delete_failed = 0 [uTorrent] -label= \ No newline at end of file +label= +webui=False +action_before=stop +action_after=removedata +host=http://localhost:8080/ +username= +password= \ No newline at end of file diff --git a/readSettings.py b/readSettings.py index 65c6c785..16aef636 100644 --- a/readSettings.py +++ b/readSettings.py @@ -62,7 +62,13 @@ def __init__(self, directory, filename): 'ssl': 'False', 'web_root': ''} # Default uTorrent settings - utorrent_defaults = { 'label': '' } + utorrent_defaults = { 'label': '', + 'webui': 'False', + 'action_before': 'stop', + 'action_after': 'removedata', + 'host': 'http://localhost:8080/', + 'username': '', + 'password': ''} defaults = {'SickBeard': sb_defaults, 'CouchPotato': cp_defaults, 'MP4': mp4_defaults, 'uTorrent': utorrent_defaults} write = False # Will be changed to true if a value is missing from the config file and needs to be written @@ -268,6 +274,12 @@ def __init__(self, directory, filename): #Read relevant uTorrent section information section = "uTorrent" self.uTorrentLabel = config.get(section, "label").lower() + self.uTorrentWebUI = config.getboolean(section, "webui") + self.uTorrentActionBefore = config.get(section, "action_before").lower() + self.uTorrentActionAfter = config.get(section, "action_after").lower() + self.uTorrentHost = config.get(section, "host").lower() + self.uTorrentUsername = config.get(section, "username") + self.uTorrentPassword = config.get(section, "password") if self.uTorrentLabel == "": self.uTorrentLabel = None; diff --git a/uTorrentToSickBeardwithConverter.py b/uTorrentToSickBeardwithConverter.py index 663a2de8..59d12e6d 100644 --- a/uTorrentToSickBeardwithConverter.py +++ b/uTorrentToSickBeardwithConverter.py @@ -1,16 +1,60 @@ import os +import re import sys import autoProcessTV from readSettings import ReadSettings from mkvtomp4 import MkvtoMp4 -#Args: %L %T %D %K %F Label, Tracker, Directory, single|multi, NameofFile(if single) +#Args: %L %T %D %K %F %I Label, Tracker, Directory, single|multi, NameofFile(if single), InfoHash +def _authToken(session=None, host=None, username=None, password=None): + auth = None + if not session: + session = requests.Session() + response = session.get(host + "gui/token.html", auth=(username, password), verify=False, timeout=30) + if response.status_code == 200: + auth = re.search("(\S+)<\/div>", response.text).group(1) + else: + print "[uTorrent] Authentication Failed - Status Code " + response.status_code + + return auth,session + +def _sendRequest(session, host='http://localhost:8080/', username=None, password=None, params=None, files=None, fnct=None): + try: + response = session.post(host + "gui/", auth=(username, password), params=params, files=files, timeout=30) + except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError), e: + print "[uTorrent] Problem sending command " + fnct + " - " + ex(e) + return False + + if response.status_code == 200: + return True + + print "[uTorrent] Problem sending command " + fnct + ", return code = " + str(response.status_code) + return False + path = str(sys.argv[3]) -settings = ReadSettings(os.path.dirname(sys.argv[0]), "autoProcess.ini") +settings = ReadSettings(os.path.dirname(sys.argv[0]), "autoProcess.ini") if settings.uTorrentLabel.lower() == sys.argv[1].lower() or not settings.uTorrentLabel: + try: + import requests + web_ui = settings.uTorrentWebUI + except: + web_ui = False + + torrent_hash = sys.argv[6] + + # Run a uTorrent action before conversion. + if web_ui: + session = requests.Session() + if session: + auth,session = _authToken(session, settings.uTorrentHost, settings.uTorrentUsername, settings.uTorrentPassword) + if auth and settings.uTorrentActionBefore: + params = {'token': auth, 'action': settings.uTorrentActionBefore, 'hash': torrent_hash} + _sendRequest(session, settings.uTorrentHost, settings.uTorrentUsername, settings.uTorrentPassword, params, None, "Stop") + + # Perform conversion. delete_dir = False settings.delete = False if not settings.output_dir: @@ -39,5 +83,11 @@ except: print "Unable to delete temporary conversion directory" + # Run a uTorrent action after conversion. + if web_ui: + if session and auth and settings.uTorrentActionAfter: + params = {'token': auth, 'action': settings.uTorrentActionAfter, 'hash': torrent_hash} + _sendRequest(session, settings.uTorrentHost, settings.uTorrentUsername, settings.uTorrentPassword, params, None, "Remove Data") + else: print "Incorrect label, ignoring"