2
2
3
3
# Host, port, username and password to connect to Transmission
4
4
# Set user and pw to None if auth is not required
5
- host , port , user , pw = 'localhost' , 9091 , 'admin' , 'passwd'
6
-
7
- # Work with torrents having only these statuses.
8
- # Can be any combination of: 'check pending', 'checking', 'downloading', 'seeding', 'stopped'
9
- # If empty - will affect all torrents
10
- status_filter = ()
11
-
12
- # How frequently to update trackers cache
13
- update_freq = 86400
14
-
15
- # A list of URLs where to get the tracker lists from.
16
- # The lists are combined into one with duplicates removed.
17
- # The trackers from these lists are checked by looking up the URL's hostname in DNS.
18
- urls = [
19
- 'https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt' ,
20
- 'https://raw.githubusercontent.com/zcq100/transmission-trackers/master/tracker_ipv6.txt' ,
21
- # ...
22
- ]
23
-
24
- # Whether to print an error if connection failed (no Transmission running?)
25
- err_on_connect = False
26
-
27
- # Where to cache downloaded lists
28
- cache_file = '/tmp/trackers_cache.txt'
29
-
30
- # Additional local lists of trackers to load.
31
- # Better to use absolute paths.
32
- # These are not checked against DNS
33
- local_lists = [
34
- # '/var/cache/trackers1.txt'
35
- # '/var/cache/trackers2.txt'
36
- # ...
37
- ]
38
-
39
- # Don't print anything (unless an error occures)
40
- silent = False
41
- # Debug output
42
- debug = False
5
+ client = {
6
+ 'host' : 'localhost' ,
7
+ 'port' : 9091 ,
8
+ 'user' : 'admin' ,
9
+ 'password' : 'passwd'
10
+ }
11
+ config = {
12
+
13
+ # Work with torrents having only these statuses.
14
+ # Can be any combination of: 'check pending', 'checking', 'downloading', 'seeding', 'stopped'
15
+ # If empty - will affect all torrents
16
+ 'status_filter' : (),
17
+
18
+ # A list of URLs where to get the tracker lists from.
19
+ # The lists are combined into one with duplicates removed.
20
+ # The trackers from these lists are checked by looking up the URL's hostname in DNS.
21
+ 'remote_lists' : [
22
+ 'https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt' ,
23
+ 'https://raw.githubusercontent.com/zcq100/transmission-trackers/master/tracker_ipv6.txt' ,
24
+ # ...
25
+ ],
26
+
27
+ # How frequently to update trackers cache
28
+ 'update_freq' : 86400 ,
29
+
30
+ # Additional local lists of trackers to load.
31
+ # Better to use absolute paths.
32
+ # These are not checked against DNS
33
+ 'local_lists' : [
34
+ # '/var/cache/trackers1.txt'
35
+ # '/var/cache/trackers2.txt'
36
+ # ...
37
+ ],
38
+
39
+ # Whether to print an error if connection failed (no Transmission running?)
40
+ 'err_on_connect' : False ,
41
+
42
+ # Don't print anything (unless an error occures)
43
+ 'silent' : False ,
44
+
45
+ # Debug output
46
+ 'debug' : False
47
+ }
48
+
49
+ from os import getcwd
50
+ if getcwd () != '/docker/transmission/transmission-trackers' :
51
+ from os import environ as env , path , mkdir
52
+ try :
53
+ import toml
54
+ configfile = path .join ( \
55
+ env .get ('XDG_CONFIG_HOME' , path .join (env ['HOME' ],'.config' )),
56
+ 'transmission/trackers.toml'
57
+ )
58
+ if path .exists (configfile ):
59
+ with open (configfile , 'r' ) as f :
60
+ client , config = toml .load (f ).values ()
61
+ else :
62
+ if not path .isdir (path .dirname (configfile )):
63
+ mkdir (path .dirname (configfile ))
64
+ with open (configfile , 'w' ) as f :
65
+ toml .dump ( {'client' : client , 'config' : config }, f )
66
+ except :
67
+ pass
68
+ # Where to cache downloaded lists
69
+ cache_file = path .join (env ['HOME' ] ,'.cache/trackers.txt' )
70
+ else :
71
+ cache_file = '/tmp/trackers_cache.txt'
72
+
73
+
43
74
44
75
### Configuration ends here ###
45
76
hdrs = {'User-Agent' : 'Mozilla/5.0' }
49
80
try :
50
81
from transmissionrpc import Client
51
82
except ImportError :
52
- try
83
+ try :
53
84
from transmission_rpc import Client
85
+ if 'user' in client :
86
+ client ['username' ] = client ['user' ]
87
+ del client ['user' ]
54
88
except ImportError :
55
89
print ("neither transmissionrpc nor transmission-rpc is installed" )
56
90
exit ()
63
97
from urllib .parse import urlparse
64
98
65
99
def lg (msg ):
66
- if not silent : print (msg )
100
+ if not config [ ' silent' ] : print (msg )
67
101
68
102
def dbg (msg ):
69
- if debug : lg (msg )
103
+ if config [ ' debug' ] : lg (msg )
70
104
71
105
def parse (txt ):
72
106
l = []
@@ -125,7 +159,7 @@ def downloadLists():
125
159
126
160
try :
127
161
mt = os .stat (cache_file ).st_mtime
128
- if time .time () - mt > update_freq :
162
+ if time .time () - mt > config [ ' update_freq' ] :
129
163
update = True
130
164
except :
131
165
update = True
@@ -134,7 +168,7 @@ def downloadLists():
134
168
return None
135
169
136
170
trk = []
137
- for url in urls :
171
+ for url in config [ 'remote_lists' ] :
138
172
l = loadURL (url )
139
173
trk += l
140
174
dbg ("Remote URL '{}' loaded: {} trackers" .format (url , len (l )))
@@ -151,7 +185,7 @@ def downloadLists():
151
185
152
186
def readLocalLists ():
153
187
trk = []
154
- for f in local_lists :
188
+ for f in config [ ' local_lists' ] :
155
189
l = loadFile (f )
156
190
trk += l
157
191
dbg ("Local list '{}' loaded: {} trackers" .format (f , len (l )))
@@ -167,7 +201,7 @@ def readLocalLists():
167
201
lg ('Remote URLs downloaded: {} trackers' .format (len (trk_remote )))
168
202
elif trk_remote is None :
169
203
trk_remote = []
170
- local_lists .append (cache_file )
204
+ config [ ' local_lists' ] .append (cache_file )
171
205
172
206
trk_local = readLocalLists ()
173
207
if trk_local :
@@ -181,12 +215,9 @@ def readLocalLists():
181
215
exit (1 )
182
216
183
217
try :
184
- if Client .__module__ == 'transmission_rpc.client' :
185
- tc = Client (host = host , port = port , username = user , password = pw )
186
- else :
187
- tc = Client (host , port = port , user = user , password = pw )
218
+ tc = Client (** client )
188
219
except :
189
- if not err_on_connect :
220
+ if not config [ ' err_on_connect' ] :
190
221
exit ()
191
222
192
223
print ("Unable to connect to Transmission: " , sys .exc_info ()[0 ])
@@ -197,7 +228,7 @@ def readLocalLists():
197
228
dbg ('{} torrents total' .format (len (torrents )))
198
229
199
230
for t in torrents :
200
- if status_filter and not t .status in status_filter :
231
+ if config [ ' status_filter' ] and not t .status in config [ ' status_filter' ] :
201
232
dbg ('{}: skipping due to status filter' .format (t .name ))
202
233
continue
203
234
0 commit comments