Created
July 20, 2019 14:30
-
-
Save rumly111/bf89f3053ee122996291513e3f16c608 to your computer and use it in GitHub Desktop.
transmission sequential download
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# Monitor transmission and enable sequential downloads | |
# by manipulating file download priorities | |
# | |
# Author: Joseph Botosh <[email protected]> | |
# License: GPL | |
from time import sleep | |
from argparse import ArgumentParser | |
from clutch.core import Client | |
HOST='localhost' | |
PORT=9091 | |
USERNAME='transmission' | |
PASSWORD='********' | |
INTERVAL=-1 | |
DEBUG=False | |
def parse_cmdline_args(): | |
parser = ArgumentParser() | |
parser.add_argument('--host', default=HOST, help='Transmission host/IP [default: localhost]') | |
parser.add_argument('--port', default=PORT, type=int, help='Transmission port [default: 9091]') | |
parser.add_argument('--username', default=USERNAME, help='username [default: transmission]') | |
parser.add_argument('--password', default=PASSWORD, help='your most guarded secret') | |
parser.add_argument('--interval', default=INTERVAL, type=int, | |
help='repetition interval in seconds [default: -1 (run only once)]') | |
parser.add_argument('--debug', default=False, action='store_true', help='show debug messages') | |
return parser.parse_args() | |
def fix_prio(client, tid): | |
info = client.torrent.files(ids=(tid,))[tid] | |
files = info['files'] | |
wanted = info['wanted'] | |
prio = info['priorities'] | |
unfinished = [] | |
for i, f in enumerate(files): | |
f['priority'] = prio[i] | |
f['index'] = i | |
if wanted[i] == 1 and f['bytes_completed'] < f['length']: | |
unfinished.append(f) | |
if len(unfinished) == 0: | |
return | |
unfinished.sort(key=lambda x: x['name']) | |
high_prio_idx = [] | |
low_prio_idx = [] | |
if unfinished[0]['priority'].name != 'high': | |
high_prio_idx = [unfinished[0]['index']] | |
if DEBUG: print('setting prio to HIGH for '+unfinished[0]['name']) | |
for unf in unfinished[1:]: | |
if unf['priority'].name != 'low': | |
low_prio_idx.append(unf['index']) | |
if DEBUG: print('setting prio to LOW for '+unf['name']) | |
if len(high_prio_idx) > 0: | |
client.torrent.set(ids=[tid],priority_high=high_prio_idx) | |
if len(low_prio_idx) > 0: | |
client.torrent.set(ids=[tid],priority_low=low_prio_idx) | |
if __name__=='__main__': | |
cfg = parse_cmdline_args() | |
DEBUG = cfg.debug | |
client = Client(host=cfg.host, port=cfg.port, | |
username=cfg.username, password=cfg.password) | |
while True: | |
torrents = client.torrent.get(['id','status']) | |
for t in torrents: | |
tid = t['id'] | |
status = t['status'] | |
if status.name == 'downloading': | |
fix_prio(client, tid) | |
if cfg.interval < 0: | |
break | |
else: | |
sleep(cfg.interval) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment