Skip to content

Commit 097dd9a

Browse files
committed
add support for toml config file
1 parent 1554e53 commit 097dd9a

File tree

2 files changed

+85
-52
lines changed

2 files changed

+85
-52
lines changed

requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
transmissionrpc>=0.11
1+
transmission-rpc>=3.2; python_version >= '3'
2+
transmissionrpc>=0.11; python_version < '3'
3+
toml

transmission-trackers.py

Lines changed: 82 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,75 @@
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 ###
4576
hdrs = {'User-Agent': 'Mozilla/5.0'}
@@ -49,8 +80,11 @@
4980
try:
5081
from transmissionrpc import Client
5182
except 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()
@@ -63,10 +97,10 @@
6397
from urllib.parse import urlparse
6498

6599
def lg(msg):
66-
if not silent: print(msg)
100+
if not config['silent']: print(msg)
67101

68102
def dbg(msg):
69-
if debug: lg(msg)
103+
if config['debug']: lg(msg)
70104

71105
def 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

152186
def 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)))
168202
elif trk_remote is None:
169203
trk_remote = []
170-
local_lists.append(cache_file)
204+
config['local_lists'].append(cache_file)
171205

172206
trk_local = readLocalLists()
173207
if trk_local:
@@ -181,12 +215,9 @@ def readLocalLists():
181215
exit(1)
182216

183217
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)
188219
except:
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():
197228
dbg('{} torrents total'.format(len(torrents)))
198229

199230
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']:
201232
dbg('{}: skipping due to status filter'.format(t.name))
202233
continue
203234

0 commit comments

Comments
 (0)