Created
November 4, 2012 07:00
-
-
Save yoyicue/4010683 to your computer and use it in GitHub Desktop.
deluge control script - wyr888
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
{} |
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/env python | |
# -*- coding: utf-8 -*- | |
from __future__ import division | |
import json | |
import time | |
from deluge.ui.client import client | |
from deluge.log import setupLogger | |
from twisted.internet import reactor, defer | |
db = {} | |
#Seeding filter | |
MAX_WAITTIME_SEEDING = 10 | |
MIN_UPLOAD_SEEDING = 200 * 1024 | |
MAX_RANK_SEEDING = 20 / 100 | |
#Downloading filter | |
MAX_WAITTIME_DOWNLOADING = 20 | |
MIN_UPLOAD_DOWNLOADING = 100 * 1024 | |
MAX_RANK_DOWNLOADING = 30 / 100 | |
def donothing(status): | |
return status | |
def cleanup(status): | |
global db | |
client.disconnect() | |
try: | |
f = open('autoclean.db', 'w') | |
f.write(json.dumps(db)) | |
f.close() | |
except: | |
db = {} | |
reactor.stop() | |
def on_get_torrents_status(torrents_status): | |
global db | |
global MAX_WAITTIME_SEEDING | |
global MIN_UPLOAD_SEEDING | |
global MAX_RANK_SEEDING | |
global MAX_WAITTIME_DOWNLOADING | |
global MIN_UPLOAD_DOWNLOADING | |
global MAX_RANK_DOWNLOADING | |
tlist = [] | |
for torrent_id in torrents_status: | |
torrent_info = torrents_status[torrent_id] | |
tlist.append(client.core.force_reannounce([torrent_id]).addCallback(donothing)) | |
if torrent_info['state'] == 'Seeding': | |
if not db.has_key(torrent_id): | |
db[torrent_id] = 0 | |
if torrent_info['upload_payload_rate'] < MIN_UPLOAD_SEEDING: | |
db[torrent_id] = db[torrent_id] + 1 | |
else: | |
db[torrent_id] = 0 | |
if db[torrent_id] > MAX_WAITTIME_SEEDING: | |
db.pop(torrent_id) | |
tlist.append(client.core.pause_torrent([torrent_id]).addCallback(donothing)) | |
all_clients = torrent_info['total_seeds']+torrent_info['total_peers'] | |
seed_clients = torrent_info['total_seeds'] | |
rank = seed_clients/all_clients | |
if rank > MAX_RANK_SEEDING: | |
db.pop(torrent_id) | |
tlist.append(client.core.pause_torrent([torrent_id]).addCallback(donothing)) | |
if torrent_info['state'] == 'Downloading': | |
if not db.has_key(torrent_id): | |
db[torrent_id] = 0 | |
if torrent_info['upload_payload_rate'] < MIN_UPLOAD_DOWNLOADING: | |
db[torrent_id] = db[torrent_id] + 1 | |
else: | |
db[torrent_id] = 0 | |
if db[torrent_id] > MAX_WAITTIME_DOWNLOADING: | |
db.pop(torrent_id) | |
tlist.append(client.core.pause_torrent([torrent_id]).addCallback(donothing)) | |
progress = torrent_info['progress'] | |
all_clients = [x for x in torrent_info['peers']] + [{'progress': progress}] | |
me = sorted(all_clients, key=lambda x: x['progress']).index({'progress': progress}) | |
rank = (len(all_clients)-me)/len(all_clients) | |
if rank > MAX_RANK_DOWNLOADING: | |
db.pop(torrent_id) | |
tlist.append(client.core.pause_torrent([torrent_id]).addCallback(donothing)) | |
if torrent_info['state'] == 'Queued': | |
tlist.append(client.core.pause_torrent([torrent_id]).addCallback(donothing)) | |
if torrent_info['state'] == 'Error': | |
tlist.append(client.core.pause_torrent([torrent_id]).addCallback(donothing)) | |
if len(tlist) > 0: | |
defer.DeferredList(tlist).addCallback(cleanup) | |
time.sleep(3) | |
else: | |
cleanup(True) | |
def on_connect_success(result): | |
client.core.get_torrents_status(None, | |
['id', | |
'name', | |
'state', | |
'ratio', | |
'peers', | |
'progress', | |
'upload_payload_rate', | |
'total_seeds', | |
'total_peers']).addCallback(on_get_torrents_status) | |
def on_connect_fail(result): | |
print "Connection failed!" | |
print "result:", result | |
def main(): | |
global db | |
setupLogger() | |
try: | |
f = open('autoclean.db', 'r') | |
db = json.loads(f.readlines()[0]) | |
f.close() | |
except: | |
db = {} | |
d = client.connect() | |
d.addCallback(on_connect_success) | |
d.addErrback(on_connect_fail) | |
reactor.run() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment