Welcome

Hi there my name is Alan MacGregor and this is my blog, hope you enjoy it :)

19 Jan 2009

Torrent linker

Hello there, so its been a while and I really will try to post more, to say sorry I've written some python goodness for you.

This little application was written to organise my torrents, I'm currently using utorrent over wine on my Linux server and most of my torrents are from private trackers each with their own tracker hostname. What this application does is creates a link to your torrents but puts them in a folder depending on what hostname it is, this way when I connect to my XBMC I can see all of the videos from www.thebox.bz and not the audio from what.cd or the videos from passthepopcorn. This also helps management on your system.

To set this up you need to ammend the 4 lines that are at the top of the script refering to folders
- torrentfilesdirectory (folder holding the completed *.torrent)
- torrent_completed_dir (folder holding the complteted files)
- link_directory (folder to create the links in)
- torrent_move_to_dir (folder to move the *.torrents into)

As always the code is open source so do what you want from it, happy new year

import os
import sys
from commands import getoutput

#Declare Variables
torrentfilesdirectory = "/torrent/torrentfilescompleted/"
torrent_completed_dir = "/torrent/completed/"
link_directory = "/torrent/links/"
torrent_move_to_dir = "/torrent/torrentfilescompletedandlinked/"
ary_errors = []
ary_directory = []
ary_tracker = []
ary_torrent = []

#Add list of torrents into variable
torrent_list = getoutput("ls " + torrentfilesdirectory)

#=====================================================
def WorkOnTorrents(line):
#Read data from torrent
directory = getoutput("btshowmetainfo \"" + torrentfilesdirectory + line + "\" | grep \"directory name:\"")
directory = directory + getoutput("btshowmetainfo \"" + torrentfilesdirectory + line + "\" | grep \"file name.....:\"")
tracker = getoutput("btshowmetainfo \"" + torrentfilesdirectory + line + "\" | grep \"announce url..:\"")

#Modify the directory variable
directory = directory.split(": ")
directory = directory[1]

#Modify the tracker variable
try:
tracker = tracker.split("\n")
tracker = tracker[0]
tracker = tracker.split("/")
tracker = tracker[2]
tracker = tracker.split(":")
tracker = tracker[0]
torrent_valid = "Works"
except:
torrent_valid = "Fails"

#Add torrent variables to arrays
if torrent_valid == "Works":
# Check Against Identified Errors
if tracker == "":
ary_errors.append(line);
else:
ary_tracker.append(tracker);
ary_directory.append(directory);
ary_torrent.append(line);

torrent_list = torrent_list.split("\n")
for line in torrent_list:
WorkOnTorrents(line)

#=====================================================
## Create Links
print "Executing Links"
for i in range(len(ary_tracker)):
print "Creating Link for: " + ary_torrent[i];
if os.path.exists(link_directory + ary_tracker[i]) == 1:
os.system("ln -s \"" + torrent_completed_dir + ary_directory[i] + "\" \"" + link_directory + ary_tracker[i] + "/" + ary_directory[i] + "\"")
else:
os.system("mkdir " + link_directory + ary_tracker[i])
os.system("ln -s \"" + torrent_completed_dir + ary_directory[i] + "\" \"" + link_directory + ary_tracker[i] + "/" + ary_directory[i] + "\"")
os.system("mv \"" + torrentfilesdirectory + ary_torrent[i] + "\" " + torrent_move_to_dir)

#=====================================================
## Display Error Torrents
if len(ary_errors) > 0:
print "Errors Found:";
for i in range(len(ary_errors)):
print ary_errors[i];

No comments:

Template by - Abdul Munir | Daya Earth Blogger Template