|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import time |
| 4 | +import logging |
| 5 | +import yaml |
| 6 | +from deluge_client import DelugeRPCClient |
| 7 | + |
| 8 | +logging.basicConfig(level="INFO") |
| 9 | + |
| 10 | + |
| 11 | +def convert(data): |
| 12 | + if isinstance(data, bytes): |
| 13 | + return data.decode() |
| 14 | + if isinstance(data, (str, int)): |
| 15 | + return str(data) |
| 16 | + if isinstance(data, dict): |
| 17 | + return dict(map(convert, data.items())) |
| 18 | + if isinstance(data, tuple): |
| 19 | + return tuple(map(convert, data)) |
| 20 | + if isinstance(data, list): |
| 21 | + return list(map(convert, data)) |
| 22 | + if isinstance(data, set): |
| 23 | + return set(map(convert, data)) |
| 24 | + return data |
| 25 | + |
| 26 | + |
| 27 | +with open("config.yml", "r") as yamlfile: |
| 28 | + cfg = yaml.load(yamlfile, Loader=yaml.BaseLoader) |
| 29 | + |
| 30 | +client = DelugeRPCClient( |
| 31 | + cfg['deluge']['host'], |
| 32 | + int(cfg['deluge']['port']), |
| 33 | + cfg['deluge']['username'], |
| 34 | + cfg['deluge']['password'] |
| 35 | +) |
| 36 | + |
| 37 | +while True: |
| 38 | + if not client.connected: |
| 39 | + try: |
| 40 | + client.connect() |
| 41 | + except client.Exception: |
| 42 | + logging.error(client.Exception) |
| 43 | + |
| 44 | + all_torrent_status_bytes = client.core.get_torrents_status( |
| 45 | + {}, |
| 46 | + ['name', 'torrent_id', 'paused', 'is_finished', 'state', 'trackers'] |
| 47 | + ) |
| 48 | + |
| 49 | + for torrent_id, torrent in all_torrent_status_bytes.items(): |
| 50 | + tracker_whitelist = False |
| 51 | + converted_torrent_id = convert(torrent_id) |
| 52 | + converted_torrent = convert(torrent) |
| 53 | + for tracker in converted_torrent['trackers']: |
| 54 | + for whitelisted_tracker in cfg['deluge']['tracker_whitelist']: |
| 55 | + if whitelisted_tracker.lower() in tracker['url'].lower(): |
| 56 | + tracker_whitelist = True |
| 57 | + continue |
| 58 | + if tracker_whitelist: |
| 59 | + continue |
| 60 | + if converted_torrent['is_finished'] == 'False': |
| 61 | + continue |
| 62 | + if converted_torrent['paused'] == 'True': |
| 63 | + continue |
| 64 | + print("Found torrent not in whitelist that is active and complete: %s\nid: %s" % |
| 65 | + (converted_torrent['name'], converted_torrent_id)) |
| 66 | + client.core.pause_torrent([converted_torrent_id]) |
| 67 | + time.sleep(int(cfg['deluge']['retry_seconds'])) |
0 commit comments