Source

TransmissionScripts / transmission-removedone.py

#!/usr/bin/env python2.7

import argparse
import datetime
import sys

import transmissionrpc

def main(argv=None):

    options = argparse.ArgumentParser(description='Removes all torrents on a given host that are either older then the given days or have a larger then given ratio.')
    options.add_argument('-p', '--port', default='9091', help='Port to connect on. Defaults to 9091.')
    options.add_argument('-r', '--ratio', type=float, default=0, help='Remove if ratio is larger. Defaults to 0, which means don\'t use ratio.')
    options.add_argument('-d', '--days', type=int, default=10, help='Remove if older then this in days. Defaults to 10.')
    options.add_argument('-u', '--user', default='', help='The user to connect as. Default is empty.')
    options.add_argument('-w', '--password', default='', help='The password to connect with.  Default is empty.')
    options.add_argument('host', default='localhost', help='The host to connect to. defaults to localhost.')
    theargs = options.parse_args()
    
    tc = transmissionrpc.Client(theargs.host, theargs.port, theargs.user, theargs.password)
    tl = tc.info()
    nothingprocessed = True
    
    for t in tl:
        age = datetime.datetime.now() - tl[t].date_added
        
        if ((theargs.ratio > 0 and tl[t].ratio >= theargs.ratio and tl[t].status != 'downloading') or 
           (age.days >= theargs.days)):
            nothingprocessed = False
            print 'removing torrent ' + tl[t].name +' and data file.'
            tc.remove(t, True)
  
    if nothingprocessed :
        print 'nothing due to be deleted, try again later'

if __name__ == "__main__":
    sys.exit(main())