Skip to content

Instantly share code, notes, and snippets.

@alpo
Created June 17, 2012 09:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alpo/2944062 to your computer and use it in GitHub Desktop.
Save alpo/2944062 to your computer and use it in GitHub Desktop.
Transmission episode bump
#!/usr/bin/env python
'''
Scans all seeding and idling torrents. If all enabled files in the torrent
is done, enables first disabled file in name's alphabethical order.
Optional argument is a maximum number of torrents bumped (default: 1).
'''
import re
import sys
from subprocess import Popen, PIPE
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
try:
max_bump = int(sys.argv[1])
except StandardError:
max_bump = 1
output = Popen(['transmission-remote',
'--list',
], stdout=PIPE).communicate()[0]
eol_re = re.compile(r'\r?\n')
torrent_lines = eol_re.split(output)
torrent_re = re.compile(r'^\s*(?P<ID>\d+)\*?' +
'\s+(?P<Done>\d+%|n/a)' +
'\s+(?P<Have>\d+\.\d+ \w+|None)' +
'\s+(?P<ETA>\d+ \w+|\w+)' +
'\s+(?P<Up>\d+\.\d+)' +
'\s+(?P<Down>\d+\.\d+)' +
'\s+(?P<Ratio>\d+\.\d+|None)' +
'\s+(?P<Status>\w+)' +
'\s+(?P<Name>.*)$')
torrents = []
for line in torrent_lines:
mo = torrent_re.match(line)
if not mo:
logger.debug('Ignored torrent line %s' % line)
continue
torrent_dict = mo.groupdict()
torrents.append(torrent_dict)
latent_bumps = [x for x in torrents if x['Status'] in ('Idle', 'Seeding')]
logger.debug(latent_bumps)
file_re = re.compile(r'^\s*(?P<ID>\d+):' +
'\s+(?P<Done>\d+%|-nan%)' +
'\s+(?P<Priority>\w+)' +
'\s+(?P<Get>\w+)' +
'\s+(?P<Size>[0-9.]+ \w+|None)' +
'\s+(?P<Name>.*)$')
for torrent in latent_bumps:
torrent_id = torrent['ID']
output = Popen(['transmission-remote',
'--torrent',
torrent_id,
'--files'], stdout=PIPE).communicate()[0]
file_lines = eol_re.split(output)
files = []
for line in file_lines:
mo = file_re.match(line)
if not mo:
logger.debug('Ignored file line %s' % line)
continue
filedict = mo.groupdict()
files.append(filedict)
in_download = [x for x in files if x['Get'] == 'Yes']
logger.debug(in_download)
all_downloaded = all([x['Done'] == '100%' for x in in_download])
logger.debug(all_downloaded)
if all_downloaded:
not_active = [x for x in files if x['Get'] == 'No']
not_active.sort(cmp=lambda x, y: cmp(x['Name'], y['Name']))
logger.debug(not_active)
new_get = not_active[0]
logger.debug(new_get)
logger.info(new_get['Name'])
Popen(['transmission-remote',
'--torrent',
torrent_id,
'--get',
new_get['ID'],
], stdout=PIPE).communicate()[0]
max_bump -= 1
if max_bump == 0:
sys.exit()
@jezuk
Copy link

jezuk commented Jan 15, 2013

Hi Alpo,

Found this script really useful, I've made a modded version which keeps all files set to normal priority and sequentially sets one file to high priority per torrent. It also allows (forces) you to login using a .netrc file

Thanks for the inspiration

https://gist.github.com/4534414

Jez

@ravomavain
Copy link

You should realy consider using the rpc api directly instead of transmission-remote, it provides a json reply that is easier to deal with in python ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment