diff --git a/README.md b/README.md index 183ea8f..0b91ed4 100644 --- a/README.md +++ b/README.md @@ -28,3 +28,8 @@ Usage: * Adjust other parameters if needed (see comments) * Make the script run by cron e.g. every minute * You're done + +Cron Setup: +* Open Crontab using crontab -e +* For Updating Trackers to Every New Torrent Every 15 Minutes Add: */15 * * * * /usr/bin/sh [path]]/transmission-trackers-auto-cron.sh +* Modify the transmission-trackers-auto.sh file as per your needs. We have provided two options, transmission-daemon & docker \ No newline at end of file diff --git a/transmission-trackers-auto-cron.sh b/transmission-trackers-auto-cron.sh new file mode 100644 index 0000000..7aca918 --- /dev/null +++ b/transmission-trackers-auto-cron.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +# If you are using transmission-daemon directly on system the below code will ensure that trackers script run only when it is running +if [ "$(systemctl is-active transmission-daemon)" = "active" ] +then +/usr/bin/python3 [path]]/transmission-trackers.py +fi + +# If you are using docker for transmission use the following. Replace 'docker-container-name' with the appropriate name +if [ "$( docker container inspect -f '{{.State.Running}}' 'docker-container-name' )" = "true" ] +then +/usr/bin/python3 [path]]/transmission-trackers.py +fi \ No newline at end of file diff --git a/transmission-trackers.py b/transmission-trackers.py index 19690f8..3a5e8c6 100755 --- a/transmission-trackers.py +++ b/transmission-trackers.py @@ -1,5 +1,10 @@ #!/usr/bin/env python3 from __future__ import print_function +import socket +import time +import os +import sys +from os import getcwd # Host, port, username and password to connect to Transmission # Set user and pw to None if auth is not required @@ -16,6 +21,11 @@ # If empty - will affect all torrents 'status_filter': (), + # Ignore torrents that contain any of these trackers. + # This is useful, e.g., for excluding torrents from private trackers. + # trackers are matched by substring, so you can cover https://my.site/announce with just my.site + 'tracker_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. @@ -47,13 +57,16 @@ 'debug': False } -from os import getcwd +cache_file = None # Universal scope if getcwd() != '/docker/transmission/transmission-trackers': from os import environ as env, path, mkdir try: + cache_file = path.join(env.get('TEMP',env.get('TMP','')) ,'.cache/trackers.txt') + if not path.isdir(path.dirname(cache_file)): + mkdir(path.dirname(cache_file)) import toml configfile = path.join( \ - env.get('XDG_CONFIG_HOME', path.join(env['HOME'],'.config')), + env.get('XDG_CONFIG_HOME', path.join(env.get('HOME',env.get('USERPROFILE',env.get('HOMEPATH',None))),'.config')), 'transmission/trackers.toml' ) if path.exists(configfile): @@ -64,22 +77,22 @@ mkdir(path.dirname(configfile)) with open(configfile, 'w') as f: toml.dump( {'client': client, 'config': config }, f ) - except: - pass + except KeyError: # Where to cache downloaded lists - cache_file = path.join(env['HOME'] ,'.cache/trackers.txt') + cache_file = path.join(env['TEMP'] ,'.cache/trackers.txt') else: cache_file = '/tmp/trackers_cache.txt' - ### Configuration ends here ### hdrs = {'User-Agent': 'Mozilla/5.0'} hosts, ips = set(()), set(()) -import sys, os, time, socket try: from transmissionrpc import Client + if 'host' in client: + client['address'] = client['host'] + del client['host'] except ImportError: try: from transmission_rpc import Client @@ -232,15 +245,24 @@ def readLocalLists(): if config['status_filter'] and not t.status in config['status_filter']: dbg('{}: skipping due to status filter'.format(t.name)) continue + if getattr(t, 'isPrivate', getattr(t, 'is_private', False)): + dbg('{}: skipping private torrent'.format(t.name)) + continue ttrk = set(()) for trk in t.trackers: - ttrk.add(trk['announce']) + ttrk.add(getattr(trk, 'fields', trk).get('announce')) + + if any(tracker in url for url in ttrk for tracker in config['tracker_filter']): + dbg('{}: skipping due to tracker filter'.format(t.name)) + continue diff = trackers - ttrk if diff: lg('{}: Adding {} trackers (before: {})'.format(t.name, len(diff), len(ttrk))) tc.change_torrent(t.id, trackerAdd=list(diff)) + time.sleep(1) + tc.reannounce_torrent(t.id) else: dbg('{}: update not needed'.format(t.name))