Skip to content

Instantly share code, notes, and snippets.

@manicmonkey
Created June 10, 2014 20:35
Show Gist options
  • Select an option

  • Save manicmonkey/948d686ca5890114e5ef to your computer and use it in GitHub Desktop.

Select an option

Save manicmonkey/948d686ca5890114e5ef to your computer and use it in GitHub Desktop.
Reattach torrents to deluge
import base64
from deluge.bencode import bdecode
from deluge.ui.client import client
from deluge.log import setupLogger
from twisted.internet import reactor
import shutil
import os
TORRENT_DOWNLOAD_PATH = '/home/manicmonkey/downloads/'
TORRENT_PICKUP_PATH = '/home/manicmonkey/torrent_files/'
TORRENT_FOUND_PATH = '_FOUND'
TORRENT_NOT_FOUND_PATH = '_NOT_FOUND'
def is_torrent_at_location(torrent_name, directory):
potential_location = os.path.join(directory, torrent_name)
print "Potential location: ", potential_location
return os.path.exists(potential_location)
def get_torrent_directory(torrent_name, root_directory):
if is_torrent_at_location(torrent_name, root_directory):
return os.path.join(root_directory, torrent_name)
else:
print "Could not find download in standard location: ", root_directory
# for sub_directory in get_sub_directories(root_directory):
# print "Checking sub_directory: ", sub_directory
# if is_torrent_at_location(torrent_name, sub_directory):
# print "Found downloaded torrent in directory: ", sub_directory
# return os.path.join(sub_directory, torrent_file)
return None
#def get_sub_directories(root_directory):
# for dir in os.listdir(root_directory):
# full_path = os.path.join(root_directory, dir)
# if os.path.isdir(full_path):
# yield full_path
def get_torrent_name(torrent_file):
f = open(torrent_file, 'r')
text = f.read()
torrent = bdecode(text)
torrent_name = torrent['info']['name']
print "Got name: ", torrent_name
return torrent_name
def move_or_delete(torrent_file, destination):
if not os.path.exists(destination):
shutil.move(torrent_file, destination)
else:
os.remove(torrent_file)
def add_torrent(torrent_file, directory):
f = open(torrent_file, 'rb')
data = f.read()
f.close()
client.core.add_torrent_file(torrent_file, base64.encodestring(data), {'download_location': directory})
print "Tried to add torrent: ", torrent_file, " to directory: ", directory
def add_torrents():
for dirpath, dirnames, dirfiles in os.walk(TORRENT_PICKUP_PATH):
for dirfile in dirfiles:
torrent_file = os.path.join(dirpath, dirfile)
torrent_name = get_torrent_name(torrent_file)
downloaded_torrent = get_torrent_directory(torrent_name, TORRENT_DOWNLOAD_PATH)
if downloaded_torrent is None:
print "Did not find downloaded torrent: ", torrent_file
move_to = os.path.join(TORRENT_PICKUP_PATH, TORRENT_NOT_FOUND_PATH)
print "Trying to move: ", torrent_file, " to: ", move_to
move_or_delete(torrent_file, os.path.join(move_to, dirfile))
else:
print "Found downloaded torrent: ", torrent_file
add_torrent(torrent_file, TORRENT_DOWNLOAD_PATH)
move_to = os.path.join(TORRENT_PICKUP_PATH, TORRENT_FOUND_PATH)
print "Trying to move: ", torrent_file, " to: ", move_to
move_or_delete(torrent_file, os.path.join(move_to, dirfile))
print
#Deluge code
setupLogger()
def on_connect_success(result):
print "Connection successful: ", result
g = client.core.get_config()
def print_results(result):
print "Result: ", result
g.addCallback(print_results)
add_torrents()
client.disconnect()
reactor.stop()
def on_connect_failure(result):
print "Connection failed: ", result
reactor.stop()
d = client.connect()
d.addCallback(on_connect_success)
d.addErrback(on_connect_failure)
reactor.run()
"""
for all torrents
see if torrent in download directory
if so add to deluge and move torrent to success directory
else move torrent to failure directory
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment