Skip to content

Commit 10849a0

Browse files
authored
Merge pull request blind-oracle#5 from ahmubashshir/patch-2
add support for toml config file
2 parents 354c4a2 + 097dd9a commit 10849a0

File tree

2 files changed

+84
-51
lines changed

2 files changed

+84
-51
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: 81 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,75 @@
33

44
# Host, port, username and password to connect to Transmission
55
# 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+
4475

4576
### Configuration ends here ###
4677
hdrs = {'User-Agent': 'Mozilla/5.0'}
@@ -52,6 +83,9 @@
5283
except ImportError:
5384
try:
5485
from transmission_rpc import Client
86+
if 'user' in client:
87+
client['username'] = client['user']
88+
del client['user']
5589
except ImportError:
5690
print("neither transmissionrpc nor transmission-rpc is installed")
5791
exit()
@@ -64,10 +98,10 @@
6498
from urllib.parse import urlparse
6599

66100
def lg(msg):
67-
if not silent: print(msg)
101+
if not config['silent']: print(msg)
68102

69103
def dbg(msg):
70-
if debug: lg(msg)
104+
if config['debug']: lg(msg)
71105

72106
def parse(txt):
73107
l = []
@@ -126,7 +160,7 @@ def downloadLists():
126160

127161
try:
128162
mt = os.stat(cache_file).st_mtime
129-
if time.time() - mt > update_freq:
163+
if time.time() - mt > config['update_freq']:
130164
update = True
131165
except:
132166
update = True
@@ -135,7 +169,7 @@ def downloadLists():
135169
return None
136170

137171
trk = []
138-
for url in urls:
172+
for url in config['remote_lists']:
139173
l = loadURL(url)
140174
trk += l
141175
dbg("Remote URL '{}' loaded: {} trackers".format(url, len(l)))
@@ -152,7 +186,7 @@ def downloadLists():
152186

153187
def readLocalLists():
154188
trk = []
155-
for f in local_lists:
189+
for f in config['local_lists']:
156190
l = loadFile(f)
157191
trk += l
158192
dbg("Local list '{}' loaded: {} trackers".format(f, len(l)))
@@ -168,7 +202,7 @@ def readLocalLists():
168202
lg('Remote URLs downloaded: {} trackers'.format(len(trk_remote)))
169203
elif trk_remote is None:
170204
trk_remote = []
171-
local_lists.append(cache_file)
205+
config['local_lists'].append(cache_file)
172206

173207
trk_local = readLocalLists()
174208
if trk_local:
@@ -182,12 +216,9 @@ def readLocalLists():
182216
exit(1)
183217

184218
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)
189220
except:
190-
if not err_on_connect:
221+
if not config['err_on_connect']:
191222
exit()
192223

193224
print("Unable to connect to Transmission: ", sys.exc_info()[0])
@@ -198,7 +229,7 @@ def readLocalLists():
198229
dbg('{} torrents total'.format(len(torrents)))
199230

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

0 commit comments

Comments
 (0)