[Script] Cron to delete Torrents and Files over certain age

Suggest, post, or discuss plugins for Deluge
gg2057
New User
New User
Posts: 4
Joined: Sun Apr 01, 2012 7:04 pm

Re: Cron to delete Torrents and Files over certain age

Post by gg2057 »

Hi, I get this error. I run the script on a feral seedbox, which has debian. Apparently the script works anyway, but the output stops listing after several torrents skipped and gives this error. I know nothing about programming, maybe you could help me out anyway! Thanks in advance
Unhandled error in Deferred:
Traceback (most recent call last):
File "/usr/lib/python2.6/dist-packages/twisted/internet/tcp.py", line 460, in doRead
return self.protocol.dataReceived(data)
File "/usr/local/lib/python2.6/dist-packages/deluge-1.3.3-py2.6.egg/deluge/ui/ client.py", line 183, in dataReceived
d.callback(request[2])
File "/usr/lib/python2.6/dist-packages/twisted/internet/defer.py", line 318, i n callback
self._startRunCallbacks(result)
File "/usr/lib/python2.6/dist-packages/twisted/internet/defer.py", line 424, i n _startRunCallbacks
self._runCallbacks()
--- <exception caught here> ---
File "/usr/lib/python2.6/dist-packages/twisted/internet/defer.py", line 441, i n _runCallbacks
self.result = callback(self.result, *args, **kw)
File "autoremov.py", line 79, in on_torrents_status
printSuccess(None, False, " Skipping %s: %s from %s" % (humantime, status["n ame"], status["save_path"]))
File "autoremov.py", line 32, in printSuccess
print "", smsg
exceptions.UnicodeEncodeError: 'ascii' codec can't encode characters in position 44-49: ordinal not in range(128)
guardmedia
New User
New User
Posts: 7
Joined: Thu Jul 07, 2011 3:01 pm

Re: Cron to delete Torrents and Files over certain age

Post by guardmedia »

Sorry, not much of a programmer either! Try searching on google, maybe you will come across something? This seems to be an error related to the characters in the torrent name (perhaps your version of python and/or this script is unable to handle unicode).

Good luck!
Cas
Top Bloke
Top Bloke
Posts: 3679
Joined: Mon Dec 07, 2009 6:04 am
Location: Scotland

Re: Cron to delete Torrents and Files over certain age

Post by Cas »

It could be that you have not got a utf8 locale set for the user running this script.
gg2057
New User
New User
Posts: 4
Joined: Sun Apr 01, 2012 7:04 pm

Re: Cron to delete Torrents and Files over certain age

Post by gg2057 »

Hi guardmedia, I solved the utf8 problem, it was as Cas suggested. I would like to ask you if there is a way to make a script that deletes (with data) the torrents in error and download state. I would create a cron job for that and run it every x hours, I'd use it for a seedbox. I would create it myself but I just don't understand where I should look for...
ratzeputz
Member
Member
Posts: 16
Joined: Wed May 09, 2012 1:32 pm

Re: Cron to delete Torrents and Files over certain age

Post by ratzeputz »

Here is a modified Version of the original script.

MODIFIED VERSION:
Delete torrents + files recursivly from given save_path over defined age. This is especially of use, if you e.g. generate content paths with deluge plugin.

To clear empty folders from the structure i would recommend to run

Code: Select all

find save_path -type d -empty -exec rmdir {} \;
afterwards.

Source:

Code: Select all

#!/usr/bin/python

# author: guardmedia
# modified by: ratzeputz22 2012-09

from deluge.log import LOG as log
from deluge.ui.client import client
import deluge.component as component
from twisted.internet import reactor, defer
import time

############
# Change the following
cliconnect = client.connect(host='127.0.0.1',port=58846,username="admin",password="admin")
torrentdir = "/home/ftp/premieres" # Directory to search for torrents to delete (works recursive!!)
timedifference = 2 # Remove torrents older than this this time (in days)
is_interactive = False # Set this to True to allow direct output or set to False for cron
do_remove_data = True # Set to True to delete torrent data as well, false to leave it

###############
# Do not edit below this line!

oldcount = 0
skipcount = 0
seedcount = 0
errorcount = 0
torrent_ids = []

def printSuccess(dresult, is_success, smsg):
    global is_interactive
    if is_interactive:
        if is_success:
            print "[+]", smsg
        else:
            print "[i]", smsg

def printError(emsg):
    global is_interactive
    if is_interactive:
        print "[e]", emsg

def endSession(esresult):
    if esresult:
        print esresult
        reactor.stop()
    else:
        client.disconnect()
        printSuccess(None, False, "Client disconnected.")
        reactor.stop()

def printReport(rresult):
    if errorcount > 0:
        printError(None, "Failed! Number of errors: %i" % (errorcount))
    else:
        if oldcount > 0:
            printSuccess(None, True, "Removed %i torrents -- Skipped %i torrents -- Seeding %i torrents" % (oldcount, skipcount, seedcount))
        else:
            printSuccess(None, True, "No old torrents! -- Skipped %i torrents -- Seeding %i torrents" % (skipcount, seedcount))
    endSession(None)

def on_torrents_status(torrents):
    global filtertime
    tlist=[]
    for torrent_id, status in torrents.items():
    # check if save path is part of torrentdir
        if status["save_path"] in torrentdir:
 	    unixtime = "%s" % (status["time_added"])
            numunixtime = int(unixtime[:-2])
            humantime = time.ctime(numunixtime)
            if numunixtime < filtertime:
                global do_remove_data
                global oldcount
                oldcount += 1
                successmsg = " Removed %s:  %s from %s" % (humantime, status["name"], status["save_path"])
                errormsg = "Error removing %s" % (status["name"])
	        tlist.append(client.core.remove_torrent(torrent_id, do_remove_data).addCallbacks(printSuccess, printError, callbackArgs = (True, successmsg), errbackArgs = (errormsg)))
            else:
                global skipcount
                skipcount += 1
                printSuccess(None, False, " Skipping %s: %s from %s" % (humantime, status["name"], status["save_path"]))
        else:
            global seedcount
            seedcount += 1
    defer.DeferredList(tlist).addCallback(printReport)

def on_session_state(result):
    client.core.get_torrents_status({"id": result}, ["name","time_added","save_path",]).addCallback(on_torrents_status)

def on_connect_success(result):
    printSuccess(None, True, "Connection was successful!")
    global timedifference
    global filtertime
    curtime = time.time()
    filtertime = curtime - (timedifference * 24 * 60 * 60)
    printSuccess(None, False, "Current unix time is %i" % (curtime))
    printSuccess(None, False, "Filtering torrents older than %s" % (time.ctime(int(filtertime))))
    client.core.get_session_state().addCallback(on_session_state)

cliconnect.addCallbacks(on_connect_success, endSession, errbackArgs=("Connection failed: check settings and try again."))

reactor.run()
Have fun.
ratze
Mario
Member
Member
Posts: 12
Joined: Sat Sep 15, 2012 11:05 pm

Re: [Script] Cron to delete Torrents and Files over certain

Post by Mario »

I've been looking at this script as an alternative to cobled bash scripts and the extractor plugin.

ratzeputz: can you please post a complete code. I'm not sure how to use your modifications.

How do I rung this:

Code: Select all

find save_path -type d -empty -exec rmdir {} \;
and for source:

do I just save it as a *.py get deluge to execute it or use a crontab for this?
ratzeputz
Member
Member
Posts: 16
Joined: Wed May 09, 2012 1:32 pm

Re: [Script] Cron to delete Torrents and Files over certain

Post by ratzeputz »

Mario wrote:I've been looking at this script as an alternative to cobled bash scripts and the extractor plugin.

ratzeputz: can you please post a complete code. I'm not sure how to use your modifications.

How do I rung this:

Code: Select all

find save_path -type d -empty -exec rmdir {} \;
and for source:

do I just save it as a *.py get deluge to execute it or use a crontab for this?

yes. save the script as py file and you can run it from crontab. (

Code: Select all

chmod +x scriptname.py
is maybe needed)

the find command is a regular bash-command. you could also put in into crontab, e.g. 1 minute after the previous task.

Code: Select all

/usr/bin/find /data/path -type d -empty -exec rmdir {} \;
Post Reply