Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add support for toml config file
  • Loading branch information
ahmubashshir committed Sep 28, 2020
commit 097dd9a8967a526cc0c7bbce67e9313082a3facd
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
transmissionrpc>=0.11
transmission-rpc>=3.2; python_version >= '3'
transmissionrpc>=0.11; python_version < '3'
toml
133 changes: 82 additions & 51 deletions transmission-trackers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,75 @@

# Host, port, username and password to connect to Transmission
# Set user and pw to None if auth is not required
host, port, user, pw = 'localhost', 9091, 'admin', 'passwd'

# Work with torrents having only these statuses.
# Can be any combination of: 'check pending', 'checking', 'downloading', 'seeding', 'stopped'
# If empty - will affect all torrents
status_filter = ()

# How frequently to update trackers cache
update_freq = 86400

# A list of URLs where to get the tracker lists from.
# The lists are combined into one with duplicates removed.
# The trackers from these lists are checked by looking up the URL's hostname in DNS.
urls = [
'https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt',
'https://raw.githubusercontent.com/zcq100/transmission-trackers/master/tracker_ipv6.txt',
# ...
]

# Whether to print an error if connection failed (no Transmission running?)
err_on_connect = False

# Where to cache downloaded lists
cache_file = '/tmp/trackers_cache.txt'

# Additional local lists of trackers to load.
# Better to use absolute paths.
# These are not checked against DNS
local_lists = [
# '/var/cache/trackers1.txt'
# '/var/cache/trackers2.txt'
# ...
]

# Don't print anything (unless an error occures)
silent = False
# Debug output
debug = False
client = {
'host': 'localhost',
'port': 9091,
'user': 'admin',
'password': 'passwd'
}
config = {

# Work with torrents having only these statuses.
# Can be any combination of: 'check pending', 'checking', 'downloading', 'seeding', 'stopped'
# If empty - will affect all torrents
'status_filter': (),

# A list of URLs where to get the tracker lists from.
# The lists are combined into one with duplicates removed.
# The trackers from these lists are checked by looking up the URL's hostname in DNS.
'remote_lists': [
'https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt',
'https://raw.githubusercontent.com/zcq100/transmission-trackers/master/tracker_ipv6.txt',
# ...
],

# How frequently to update trackers cache
'update_freq': 86400,

# Additional local lists of trackers to load.
# Better to use absolute paths.
# These are not checked against DNS
'local_lists': [
# '/var/cache/trackers1.txt'
# '/var/cache/trackers2.txt'
# ...
],

# Whether to print an error if connection failed (no Transmission running?)
'err_on_connect': False,

# Don't print anything (unless an error occures)
'silent': False,

# Debug output
'debug': False
}

from os import getcwd
if getcwd() != '/docker/transmission/transmission-trackers':
from os import environ as env, path, mkdir
try:
import toml
configfile = path.join( \
env.get('XDG_CONFIG_HOME', path.join(env['HOME'],'.config')),
'transmission/trackers.toml'
)
if path.exists(configfile):
with open(configfile, 'r') as f:
client, config = toml.load(f).values()
else:
if not path.isdir(path.dirname(configfile)):
mkdir(path.dirname(configfile))
with open(configfile, 'w') as f:
toml.dump( {'client': client, 'config': config }, f )
except:
pass
# Where to cache downloaded lists
cache_file = path.join(env['HOME'] ,'.cache/trackers.txt')
else:
cache_file = '/tmp/trackers_cache.txt'



### Configuration ends here ###
hdrs = {'User-Agent': 'Mozilla/5.0'}
Expand All @@ -49,8 +80,11 @@
try:
from transmissionrpc import Client
except ImportError:
try
try:
from transmission_rpc import Client
if 'user' in client:
client['username'] = client['user']
del client['user']
except ImportError:
print("neither transmissionrpc nor transmission-rpc is installed")
exit()
Expand All @@ -63,10 +97,10 @@
from urllib.parse import urlparse

def lg(msg):
if not silent: print(msg)
if not config['silent']: print(msg)

def dbg(msg):
if debug: lg(msg)
if config['debug']: lg(msg)

def parse(txt):
l = []
Expand Down Expand Up @@ -125,7 +159,7 @@ def downloadLists():

try:
mt = os.stat(cache_file).st_mtime
if time.time() - mt > update_freq:
if time.time() - mt > config['update_freq']:
update = True
except:
update = True
Expand All @@ -134,7 +168,7 @@ def downloadLists():
return None

trk = []
for url in urls:
for url in config['remote_lists']:
l = loadURL(url)
trk += l
dbg("Remote URL '{}' loaded: {} trackers".format(url, len(l)))
Expand All @@ -151,7 +185,7 @@ def downloadLists():

def readLocalLists():
trk = []
for f in local_lists:
for f in config['local_lists']:
l = loadFile(f)
trk += l
dbg("Local list '{}' loaded: {} trackers".format(f, len(l)))
Expand All @@ -167,7 +201,7 @@ def readLocalLists():
lg('Remote URLs downloaded: {} trackers'.format(len(trk_remote)))
elif trk_remote is None:
trk_remote = []
local_lists.append(cache_file)
config['local_lists'].append(cache_file)

trk_local = readLocalLists()
if trk_local:
Expand All @@ -181,12 +215,9 @@ def readLocalLists():
exit(1)

try:
if Client.__module__ == 'transmission_rpc.client':
tc = Client(host=host, port=port, username=user, password=pw)
else:
tc = Client(host, port=port, user=user, password=pw)
tc = Client(**client)
except:
if not err_on_connect:
if not config['err_on_connect']:
exit()

print("Unable to connect to Transmission: ", sys.exc_info()[0])
Expand All @@ -197,7 +228,7 @@ def readLocalLists():
dbg('{} torrents total'.format(len(torrents)))

for t in torrents:
if status_filter and not t.status in status_filter:
if config['status_filter'] and not t.status in config['status_filter']:
dbg('{}: skipping due to status filter'.format(t.name))
continue

Expand Down