|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import transmissionrpc, sys, os, time |
| 4 | + |
| 5 | +# Host, port, username and password to connect to Transmission |
| 6 | +# Set user and pw to None if auth is not required |
| 7 | +host, port, user, pw = 'localhost', 9091, 'admin', 'pwd' |
| 8 | + |
| 9 | +# Work with torrents having only these statuses. |
| 10 | +# Can be any combination of: 'check pending', 'checking', 'downloading', 'seeding', 'stopped' |
| 11 | +# If empty - will affect all torrents |
| 12 | +status_filter = () |
| 13 | + |
| 14 | +# How frequently to update trackers cache |
| 15 | +update_freq = 86400 |
| 16 | + |
| 17 | +# Path to trackers URL and local cache |
| 18 | +trackers_url = 'https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt' |
| 19 | +trackers_file = '/tmp/trackers_all.txt' |
| 20 | + |
| 21 | +# Don't print anything (unless an error occures) |
| 22 | +silent = False |
| 23 | +# Debug output |
| 24 | +debug = False |
| 25 | + |
| 26 | +### |
| 27 | +if silent: debug = False |
| 28 | +trackers = None |
| 29 | + |
| 30 | +def readTrackers(): |
| 31 | + f = open(trackers_file, 'r') |
| 32 | + trackers = set(()) |
| 33 | + |
| 34 | + for t in f.readlines(): |
| 35 | + t = t.strip() |
| 36 | + if not t.startswith('http') or not t.startswith('udp'): |
| 37 | + continue |
| 38 | + trackers.add(t) |
| 39 | + |
| 40 | + f.close() |
| 41 | + if debug: print('{} trackers loaded from {}'.format(len(trackers), trackers_file)) |
| 42 | + return trackers |
| 43 | + |
| 44 | +def downloadTrackers(): |
| 45 | + update = False |
| 46 | + |
| 47 | + try: |
| 48 | + mt = os.stat(trackers_file).st_mtime |
| 49 | + if time.time() - mt > update_freq: |
| 50 | + update = True |
| 51 | + except: |
| 52 | + update = True |
| 53 | + |
| 54 | + if not update: |
| 55 | + return |
| 56 | + |
| 57 | + if sys.version_info[0] == 2: |
| 58 | + import urllib |
| 59 | + urllib.urlretrieve(trackers_url, trackers_file) |
| 60 | + else: |
| 61 | + import urllib.request |
| 62 | + urllib.request.urlretrieve(trackers_url, trackers_file) |
| 63 | + |
| 64 | + trackers = readTrackers() |
| 65 | + if not silent: print('Trackers list updated ({} loaded)'.format(len(trackers))) |
| 66 | + |
| 67 | +downloadTrackers() |
| 68 | +if not trackers: trackers = readTrackers() |
| 69 | + |
| 70 | +tc = transmissionrpc.Client(host, port=port, user=user, password=pw) |
| 71 | +torrents = tc.get_torrents() |
| 72 | + |
| 73 | +if debug: print('{} torrents total'.format(len(torrents))) |
| 74 | + |
| 75 | +for t in torrents: |
| 76 | + if status_filter and not t.status in status_filter: |
| 77 | + if debug: print('{}: skipping due to status filter'.format(t.name)) |
| 78 | + continue |
| 79 | + |
| 80 | + ttrk = set(()) |
| 81 | + for trk in t.trackers: |
| 82 | + ttrk.add(trk['announce']) |
| 83 | + |
| 84 | + diff = trackers - ttrk |
| 85 | + |
| 86 | + if diff: |
| 87 | + if not silent: print('{}: Adding {} trackers (before: {})'.format(t.name, len(diff)), len(ttrk)) |
| 88 | + tc.change_torrent(t.id, trackerAdd=list(diff)) |
| 89 | + else: |
| 90 | + if debug: print('{}: update not needed'.format(t.name)) |
0 commit comments