Created
February 12, 2013 02:54
-
-
Save zlalanne/4759855 to your computer and use it in GitHub Desktop.
Python script to link files once deluge finishes downloading
This file contains hidden or 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/python | |
import os | |
import sys | |
import string | |
import syslog | |
# Import the client module | |
from deluge.ui.client import client | |
# Import the reactor module from Twisted - this is for our mainloop | |
from twisted.internet import reactor | |
from twisted.internet import defer | |
# Set up the logger to print out errors | |
from deluge.log import setupLogger | |
setupLogger() | |
syslog.syslog('deluge test: the script started running') | |
# Directories | |
MOVIE_DIR="movies" | |
TV_DIR="tv" | |
DROPBOX_DIR="/home/server/Documents/Dropbox/Bitorrent/share" | |
label="" | |
tracker="" | |
files="" | |
path="" | |
# Connect to a daemon running on the localhost | |
# We get a Deferred object from this method and we use this to know if and when | |
# the connection succeeded or failed. | |
d = client.connect() | |
# Torrent ID is passed from deluge execute plugin | |
torrent_id = sys.argv[1] | |
# We create a callback function to be called upon a successful connection | |
def on_connect_success(result): | |
print "Connection was successful!" | |
def on_get_config_value(value, key): | |
print "Got config value from the daemon!" | |
print "%s: %s" % (key, value) | |
# Disconnect from the daemon once we've got what we needed | |
client.disconnect() | |
# Stop the twisted main loop and exit | |
reactor.stop() | |
def on_get_torrent(torrent): | |
global label | |
global tracker | |
global files | |
global path | |
label=torrent["label"] | |
tracker=torrent["tracker_host"] | |
files=torrent["files"] | |
path=torrent["move_completed_path"] | |
print label | |
print tracker | |
print files | |
print path | |
client.disconnect() | |
reactor.stop() | |
client.core.get_torrent_status(torrent_id, ["label","tracker_host","files","move_completed_path"]).addCallback(on_get_torrent) | |
# We add the callback to the Deferred object we got from connect() | |
d.addCallback(on_connect_success) | |
# We create another callback function to be called when an error is encountered | |
def on_connect_fail(result): | |
print "Connection failed!" | |
print "result:", result | |
# We add the callback (in this case it's an errback, for error) | |
d.addErrback(on_connect_fail) | |
# Run the twisted main loop to make everything go | |
reactor.run() | |
# Deciding how to process files based on labels | |
if (label == "parents") or (label == "nicole") or (label == "ignore"): | |
print "Nothing to do" | |
elif (label == "dropbox"): | |
print "Linking to dropbox" | |
for file in files: | |
source = path + "/" + file["path"] | |
dest = DROPBOX_DIR + "/" + file["path"] | |
dirname = os.path.dirname(dest) | |
if os.path.exists(dirname) == False: | |
os.makedirs(dirname) | |
os.symlink(source,dest) | |
else: | |
print "Something to do" | |
dest="" | |
if tracker == "passthepopcorn.me": | |
print "Movie" | |
for file in files: | |
source = path + "/" + file["path"] | |
dest = path + "/" + MOVIE_DIR + "/" + file["path"] | |
dirname = os.path.dirname(dest) | |
if os.path.exists(dirname) == False: | |
os.makedirs(dirname) | |
os.link(source,dest) | |
elif tracker == "broadcasthe.net": | |
print "TV Show" | |
for file in files: | |
source = path + "/" + file["path"] | |
dest = path + "/" + TV_DIR + "/" + file["path"] | |
dirname = os.path.dirname(dest) | |
if os.path.exists(dirname) == False: | |
os.makedirs(dirname) | |
os.link(source,dest) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment