-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuwebapi.py
More file actions
85 lines (75 loc) · 3.03 KB
/
uwebapi.py
File metadata and controls
85 lines (75 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# -*- coding: UTF-8 -*-
import cookielib
import json
import re
import urllib2
import utils
class ConnectionError(Exception):
"""
Általános kapcsolódási hiba.
Minden esetben ilyen típusú hibával tér vissza a uWebAPI osztály.
"""
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg
class uWebAPI:
"""
A uTorrent vezérlése a webes API-n keresztül. Támogatja a token alapú azonosítás.
Az azonosításhoz szükséges adatokat a CONFIG_FILE-ból nyeri.
"""
def __init__(self):
self.token = ""
self.set_authentication()
def set_authentication(self):
""" Az azonosításhoz szükséges fejléc, és a cookie tárolásához szükséges tároló beállítása."""
config = utils.getConfig()
self.headers = {"Authorization" : "Basic %s" % config["webui"]["auth"]}
self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookielib.CookieJar()))
self.url = "%s:%d/gui" % (config["webui"]["url"], config["webui"]["port"])
def request_token(self):
""" Új token igénylése."""
req = urllib2.Request("%s/token.html" % self.url, None, self.headers)
try:
data = self.opener.open(req).read()
except urllib2.URLError:
raise ConnectionError("Cannot request token at %s" % self.url)
p = re.compile(r"<.*?>")
self.token = p.sub("", data)
def get_torrents(self):
""" A torrent lista lekérdezése. """
self.request_token()
req = urllib2.Request("%s/?token=%s&list=1" % (self.url, self.token), None, self.headers)
try:
resp = self.opener.open(req).read()
return json.loads(resp)['torrents']
except urllib2.URLError:
raise ConnectionError("Cannot get the torrent list.")
def action(self, cmd, hash):
"""
A cmd típusú művelet végrehajtása az adot hash kódú torrenten.
"""
req = urllib2.Request("%s/?token=%s&action=%s&hash=%s" % (self.url, self.token, cmd, hash), None, self.headers)
try:
self.opener.open(req)
except urllib2.HTTPError as e:
if e.code == 400:
self.request_token()
self.action(cmd, hash)
else:
raise ConnectionError("Cannot do the action %s, on the %s hash coded torrent." % (cmd, hash) )
return True
def deactivate(self):
""" Minden aktív torrent megállítása. """
config = utils.getConfig()
for i in self.get_torrents():
if i[1]&1 == 1:
config["torrents"].append(i[0])
self.action("stop", i[0])
utils.setConfig(config)
def reactivate(self):
""" A megállított torrentek elindítása. """
config = utils.getConfig()
while len(config["torrents"]):
self.action("start", config["torrents"].pop())
utils.setConfig(config)