-
Notifications
You must be signed in to change notification settings - Fork 0
/
whatbox.py
102 lines (87 loc) · 3.23 KB
/
whatbox.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import os
import os.path
import sys
import subprocess
try:
import xmlrpclib
except ImportError:
print "xmlrpclib not installed"
sys.exit( 1 )
class WhatboxXMLRPC( object ):
def __init__( self, host, username, password, path='/xmlrpc' ):
self.host = host
self.path = path
self.username = username
self.password = password
self.conn = None
self._setup()
def _setup( self ):
self._setup_conn()
def _setup_conn( self, **kwargs ):
url = 'https://{username}:{password}@{host}{path}'.format(
**self.__dict__
)
self.conn = xmlrpclib.ServerProxy( url )
def get_hashs( self ):
'''
Returns a list of all torrent hashes
'''
return self.conn.download_list()
def get_download_name( self, dlhash ):
'''
Returns the name of a given torrent hash
@param dlhash - The hash to fetch the name for
@returns the name of the torrent
'''
return self.conn.d.get_name( dlhash )
def get_download_path( self, dlhash ):
'''
Gets the path of the given torrent hash
@param dlhash - The hash to fetch the path for
@returns the path on the server the download is going to
'''
return self.conn.d.get_directory( dlhash )
def get_download_files( self, dlhash ):
'''
Gets a list of all the files for a given torrent hash
@param dlhash - The torrent hash to get the filenames for
@returns a list of all the paths to the files for the torrent
'''
mc = xmlrpclib.MultiCall(self.conn)
filenames = []
dlpath = self.get_download_path( dlhash )
for index in range(self.conn.d.size_files( dlhash )):
mc.f.get_path( dlhash, index )
return [os.path.join(dlpath,fn) for fn in mc()]
def get_all_files( self ):
'''
Returns a dictionary of information about all torrents on the server
@returns a dictionary keyed by each torrent hash with information about
each of the torrents
'''
hashs = self.get_hashs()
torrents = {}
for dhash in hashs:
if dhash not in torrents:
torrents[dhash] = {}
torrents[dhash]['name'] = self.get_download_name(dhash)
torrents[dhash]['files'] = self.get_download_files(dhash)
torrents[dhash]['active'] = self.conn.d.is_active(dhash)
torrents[dhash]['complete'] = self.conn.d.get_complete(dhash)
return torrents
def delete_download( self, dlhash ):
'''
Sets the event to delete the download when the torrent is erased
The documentation is not exactly the most straight forward so I can
only hope this is the easiest/best way to do it.
@param dlhash
@returns the return code from running the commands
'''
ret = self.conn.system.method.set_key(
'event.download.erased',
'removefile',
'execute={rm,-rf,$d.get_base_path=}'
)
if ret == 0:
ret = self.conn.d.erase( dlhash )
return ret