update utorrent client. Fixes #1138

This commit is contained in:
clinton-hall 2017-01-10 08:25:55 +10:30
commit 454db6fe6a
2 changed files with 77 additions and 71 deletions

View file

@ -1,50 +1,47 @@
# coding=utf8
import json
import urllib
import urllib2
import urlparse
import cookielib
import re
import StringIO
try:
import json
except ImportError:
import simplejson as json
from six import StringIO
from six.moves.http_cookiejar import CookieJar
from six.moves.urllib_error import HTTPError
from six.moves.urllib_parse import urljoin, urlencode
from six.moves.urllib_request import (
build_opener, install_opener,
HTTPBasicAuthHandler, HTTPCookieProcessor,
Request,
)
from core.utorrent.upload import MultiPartForm
from upload import MultiPartForm
class UTorrentClient(object):
def __init__(self, base_url, username, password):
self.base_url = base_url
self.username = username
self.password = password
self.opener = self._make_opener('uTorrent', base_url, username, password)
self.token = self._get_token()
# TODO refresh token, when necessary
#TODO refresh token, when necessary
def _make_opener(self, realm, base_url, username, password):
"""uTorrent API need HTTP Basic Auth and cookie support for token verify."""
'''uTorrent API need HTTP Basic Auth and cookie support for token verify.'''
auth_handler = HTTPBasicAuthHandler()
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(realm=realm,
uri=base_url,
user=username,
passwd=password)
opener = build_opener(auth_handler)
install_opener(opener)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
cookie_jar = CookieJar()
cookie_handler = HTTPCookieProcessor(cookie_jar)
cookie_jar = cookielib.CookieJar()
cookie_handler = urllib2.HTTPCookieProcessor(cookie_jar)
handlers = [auth_handler, cookie_handler]
opener = build_opener(*handlers)
opener = urllib2.build_opener(*handlers)
return opener
def _get_token(self):
url = urljoin(self.base_url, 'token.html')
url = urlparse.urljoin(self.base_url, 'token.html')
response = self.opener.open(url)
token_re = "<div id='token' style='display:none;'>([^<>]+)</div>"
match = re.search(token_re, response.read())
@ -56,43 +53,25 @@ class UTorrentClient(object):
return self._action(params)
def start(self, *hashes):
params = [('action', 'start'), ]
params = [('action', 'start'),]
for hash in hashes:
params.append(('hash', hash))
return self._action(params)
def stop(self, *hashes):
params = [('action', 'stop'), ]
params = [('action', 'stop'),]
for hash in hashes:
params.append(('hash', hash))
return self._action(params)
def pause(self, *hashes):
params = [('action', 'pause'), ]
params = [('action', 'pause'),]
for hash in hashes:
params.append(('hash', hash))
return self._action(params)
def forcestart(self, *hashes):
params = [('action', 'forcestart'), ]
for hash in hashes:
params.append(('hash', hash))
return self._action(params)
def remove(self, *hashes):
params = [('action', 'remove'), ]
for hash in hashes:
params.append(('hash', hash))
return self._action(params)
def removedata(self, *hashes):
params = [('action', 'removedata'), ]
for hash in hashes:
params.append(('hash', hash))
return self._action(params)
def recheck(self, *hashes):
params = [('action', 'recheck'), ]
params = [('action', 'forcestart'),]
for hash in hashes:
params.append(('hash', hash))
return self._action(params)
@ -105,6 +84,14 @@ class UTorrentClient(object):
params = [('action', 'getprops'), ('hash', hash)]
return self._action(params)
def setprops(self, hash, **kvpairs):
params = [('action', 'setprops'), ('hash', hash)]
for k, v in kvpairs.iteritems():
params.append( ("s", k) )
params.append( ("v", v) )
return self._action(params)
def setprio(self, hash, priority, *files):
params = [('action', 'setprio'), ('hash', hash), ('p', str(priority))]
for file_index in files:
@ -117,7 +104,7 @@ class UTorrentClient(object):
form = MultiPartForm()
if filepath is not None:
file_handler = open(filepath)
file_handler = open(filepath,'rb')
else:
file_handler = StringIO.StringIO(bytes)
@ -125,10 +112,26 @@ class UTorrentClient(object):
return self._action(params, str(form), form.get_content_type())
def addurl(self, url):
params = [('action', 'add-url'), ('s', url)]
self._action(params)
def remove(self, *hashes):
params = [('action', 'remove'),]
for hash in hashes:
params.append(('hash', hash))
return self._action(params)
def removedata(self, *hashes):
params = [('action', 'removedata'),]
for hash in hashes:
params.append(('hash', hash))
return self._action(params)
def _action(self, params, body=None, content_type=None):
# about token, see https://github.com/bittorrent/webui/wiki/TokenSystem
url = '{url}?token={token}&{params}'.format(url=self.url, token=self.token, params=urlencode(params))
request = Request(url)
#about token, see https://github.com/bittorrent/webui/wiki/TokenSystem
url = self.base_url + '?token=' + self.token + '&' + urllib.urlencode(params)
request = urllib2.Request(url)
if body:
request.add_data(body)
@ -139,5 +142,5 @@ class UTorrentClient(object):
try:
response = self.opener.open(request)
return response.code, json.loads(response.read())
except HTTPError:
raise
except urllib2.HTTPError,e:
raise