Skip to content

Commit

Permalink
Add Web UI integration to the uTorrent processing script.
Browse files Browse the repository at this point in the history
  • Loading branch information
greenacorn-michael committed Dec 28, 2014
1 parent 69921ff commit 2008452
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 7 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------------
Expand Down
8 changes: 7 additions & 1 deletion autoProcess.ini.sample
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,10 @@ method = renamer
delete_failed = 0

[uTorrent]
label=
label=
webui=False
action_before=stop
action_after=removedata
host=http://localhost:8080/
username=
password=
14 changes: 13 additions & 1 deletion readSettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down
54 changes: 52 additions & 2 deletions uTorrentToSickBeardwithConverter.py
Original file line number Diff line number Diff line change
@@ -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("<div.*?>(\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:
Expand Down Expand Up @@ -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"

0 comments on commit 2008452

Please sign in to comment.