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