22
33# Host, port, username and password to connect to Transmission
44# 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+
4374
4475### Configuration ends here ###
4576hdrs = {'User-Agent' : 'Mozilla/5.0' }
4980try :
5081 from transmissionrpc import Client
5182except ImportError :
52- try
83+ try :
5384 from transmission_rpc import Client
85+ if 'user' in client :
86+ client ['username' ] = client ['user' ]
87+ del client ['user' ]
5488 except ImportError :
5589 print ("neither transmissionrpc nor transmission-rpc is installed" )
5690 exit ()
6397 from urllib .parse import urlparse
6498
6599def lg (msg ):
66- if not silent : print (msg )
100+ if not config [ ' silent' ] : print (msg )
67101
68102def dbg (msg ):
69- if debug : lg (msg )
103+ if config [ ' debug' ] : lg (msg )
70104
71105def parse (txt ):
72106 l = []
@@ -125,7 +159,7 @@ def downloadLists():
125159
126160 try :
127161 mt = os .stat (cache_file ).st_mtime
128- if time .time () - mt > update_freq :
162+ if time .time () - mt > config [ ' update_freq' ] :
129163 update = True
130164 except :
131165 update = True
@@ -134,7 +168,7 @@ def downloadLists():
134168 return None
135169
136170 trk = []
137- for url in urls :
171+ for url in config [ 'remote_lists' ] :
138172 l = loadURL (url )
139173 trk += l
140174 dbg ("Remote URL '{}' loaded: {} trackers" .format (url , len (l )))
@@ -151,7 +185,7 @@ def downloadLists():
151185
152186def readLocalLists ():
153187 trk = []
154- for f in local_lists :
188+ for f in config [ ' local_lists' ] :
155189 l = loadFile (f )
156190 trk += l
157191 dbg ("Local list '{}' loaded: {} trackers" .format (f , len (l )))
@@ -167,7 +201,7 @@ def readLocalLists():
167201 lg ('Remote URLs downloaded: {} trackers' .format (len (trk_remote )))
168202elif trk_remote is None :
169203 trk_remote = []
170- local_lists .append (cache_file )
204+ config [ ' local_lists' ] .append (cache_file )
171205
172206trk_local = readLocalLists ()
173207if trk_local :
@@ -181,12 +215,9 @@ def readLocalLists():
181215 exit (1 )
182216
183217try :
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 )
188219except :
189- if not err_on_connect :
220+ if not config [ ' err_on_connect' ] :
190221 exit ()
191222
192223 print ("Unable to connect to Transmission: " , sys .exc_info ()[0 ])
@@ -197,7 +228,7 @@ def readLocalLists():
197228dbg ('{} torrents total' .format (len (torrents )))
198229
199230for 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' ] :
201232 dbg ('{}: skipping due to status filter' .format (t .name ))
202233 continue
203234
0 commit comments