37
by Ted Smith
simple script to add stuff to deluge |
1 |
import sys, os, base64 |
2 |
||
3 |
from deluge.ui.client import client |
|
4 |
from twisted.internet import reactor |
|
5 |
||
6 |
# Set up the logger to print out errors
|
|
7 |
from deluge.log import setupLogger |
|
8 |
setupLogger() |
|
9 |
||
10 |
def validate_filename(f): |
|
11 |
"""
|
|
12 |
Ensure that the filename f is a real filename and not a directory.
|
|
13 |
"""
|
|
14 |
if not (os.path.exists(f) |
|
15 |
and (not os.path.isdir(f)) |
|
16 |
and f.endswith(".torrent")): |
|
17 |
raise ValueError("Argument must be a torrent file!") |
|
18 |
||
38
by Ted Smith
a script that adds torrents to deluge in an object-oriented fashion, using the twisted mainloop |
19 |
class DelugeAdder(object): |
20 |
"""
|
|
21 |
Create a connection and add a number of torrents to deluge.
|
|
22 |
Starts and stops Twisted reactor loop.
|
|
23 |
"""
|
|
24 |
||
25 |
def __init__(self, default_path="/usr/share/warez/Downloads"): |
|
26 |
self.default_path = default_path |
|
27 |
self.added_success = True |
|
28 |
self.connect_success = True |
|
29 |
self.error_message = "" |
|
30 |
||
31 |
def __disabled_setattr__(self, attr, val): |
|
32 |
print "Setting attribute {} to {}".format(attr, val) |
|
33 |
return super(DelugeAdder, self).__setattr__(attr, val) |
|
34 |
||
35 |
def get_connect_failure_callback(self): |
|
36 |
def on_connect_fail(res): |
|
37 |
self.connect_success = False |
|
38 |
self.error_message = "Couldn't connect" |
|
39 |
# print "Couldn't connect"
|
|
40 |
return on_connect_fail |
|
41 |
||
42 |
def get_add_success_callback(self): |
|
37
by Ted Smith
simple script to add stuff to deluge |
43 |
def on_add_success(r): |
38
by Ted Smith
a script that adds torrents to deluge in an object-oriented fashion, using the twisted mainloop |
44 |
self.added_success = True |
37
by Ted Smith
simple script to add stuff to deluge |
45 |
client.disconnect() |
38
by Ted Smith
a script that adds torrents to deluge in an object-oriented fashion, using the twisted mainloop |
46 |
# print "Successfully added torrent"
|
37
by Ted Smith
simple script to add stuff to deluge |
47 |
reactor.stop() |
38
by Ted Smith
a script that adds torrents to deluge in an object-oriented fashion, using the twisted mainloop |
48 |
return on_add_success |
49 |
||
50 |
def get_add_failure_callback(self): |
|
37
by Ted Smith
simple script to add stuff to deluge |
51 |
def on_add_failure(r): |
38
by Ted Smith
a script that adds torrents to deluge in an object-oriented fashion, using the twisted mainloop |
52 |
self.added_success = False |
53 |
self.error_message = "Couldn't add torrent" |
|
54 |
# print "Failed to add torrent"
|
|
55 |
reactor.stop() |
|
56 |
return on_add_failure |
|
37
by Ted Smith
simple script to add stuff to deluge |
57 |
|
38
by Ted Smith
a script that adds torrents to deluge in an object-oriented fashion, using the twisted mainloop |
58 |
def get_connect_success_callback(self, filename, path): |
59 |
def on_connect_success(rslt): |
|
60 |
self.connect_success = True |
|
61 |
# print "Successfully connected! Adding torrent {}".format(filename)
|
|
62 |
with open(filename, "r") as fh: |
|
63 |
filedump = base64.encodestring(fh.read()) |
|
64 |
fh.close() |
|
65 |
||
66 |
client.core.add_torrent_file(filename, filedump, |
|
67 |
{'download_location': path})\ |
|
68 |
.addCallback(self.get_add_success_callback())\ |
|
69 |
.addErrback(self.get_add_failure_callback()) |
|
70 |
return on_connect_success |
|
71 |
||
72 |
def add_torrent(self, filename, path=None): |
|
73 |
validate_filename(filename) |
|
74 |
# print "Trying to add torrent"
|
|
75 |
d = client.connect() |
|
76 |
d.addCallbacks(self.get_connect_success_callback(filename, path), |
|
77 |
self.get_connect_failure_callback()) |
|
78 |
reactor.run() |
|
79 |
return self.added_success |
|
37
by Ted Smith
simple script to add stuff to deluge |
80 |
|
81 |
if __name__ == "__main__": |
|
38
by Ted Smith
a script that adds torrents to deluge in an object-oriented fashion, using the twisted mainloop |
82 |
da = DelugeAdder() |
83 |
if not da.add_torrent(sys.argv[1], sys.argv[2]): |
|
84 |
print "Error adding torrent: {}".format(da.error_message) |