From 86393f9da40eae436ee5494959ef80fd3d3899b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joel=20K=C3=A5berg?= Date: Fri, 1 Feb 2013 09:14:17 +0100 Subject: [PATCH] unfinished code An attempt on adding extraction support. This code isnt finished, and will probably not work, comments added in code --- ...ia.cfg.sample => TorrentToMedia.cfg.sample | 5 +- TorrentToMedia.py | 171 ++++++++++++++++++ TransmissionToMedia.py | 113 ------------ 3 files changed, 173 insertions(+), 116 deletions(-) rename TransmissionToMedia.cfg.sample => TorrentToMedia.cfg.sample (83%) create mode 100644 TorrentToMedia.py delete mode 100755 TransmissionToMedia.py diff --git a/TransmissionToMedia.cfg.sample b/TorrentToMedia.cfg.sample similarity index 83% rename from TransmissionToMedia.cfg.sample rename to TorrentToMedia.cfg.sample index b375f9dd..55646952 100644 --- a/TransmissionToMedia.cfg.sample +++ b/TorrentToMedia.cfg.sample @@ -6,10 +6,9 @@ destination = /abs/path/to/complete/movies category = tv destination = /abs/path/to/complete/tv -[Transmission] +[Torrent] copy = 1 -unrar = /abs/path/to/unrar -unzip = /abs/path/to/unzip +unpacker = 'C:\\Program Files\\7-Zip\\7z.exe' parcheck = /abs/path/to/par2 [Notes] diff --git a/TorrentToMedia.py b/TorrentToMedia.py new file mode 100644 index 00000000..aeccf5cf --- /dev/null +++ b/TorrentToMedia.py @@ -0,0 +1,171 @@ +#!/usr/bin/env python + +import sys +import autoProcessMovie +import autoProcessTV +import ConfigParser +import os +from os import listdir +from os.path import isfile, join +import glob + + +##You can use the following parameters: +## +##%F - Name of downloaded file (for single file torrents) +##%D - Directory where files are saved +##%N - Title of torrent +##%P - Previous state of torrent +##%L - Label +##%T - Tracker +##%M - Status message string (same as status column) +##%I - hex encoded info-hash +##%S - State of torrent +##%K - kind of torrent (single|multi) +## +##Where State is one of: +## +##Error - 1 +##Checked - 2 +##Paused - 3 +##Super seeding - 4 +##Seeding - 5 +##Downloading - 6 +##Super seed [F] - 7 +##Seeding [F] - 8 +##Downloading [F] - 9 +##Queued seed - 10 +##Finished - 11 +##Queued - 12 +##Stopped - 13 + +## We will pass in %D, %N, %L +Directory = sys.argv[1] +Name = sys.argv[2] +Category = sys.argv[3] +print "transmissionToMedia v 4.1" + +config = ConfigParser.ConfigParser() +configFilename = os.path.join(os.path.dirname(sys.argv[0]), "TorrentToMedia.cfg") + +print "Loading config from", configFilename + +if not os.path.isfile(configFilename): + print "ERROR: You need an autoProcessMovie.cfg file - did you rename and edit the .sample?" + sys.exit(-1) + +config.read(configFilename) + +Movie_Cat = config.get("CouchPotato", "category") +TV_Cat = config.get("SickBeard", "category") +Movie_dest = config.get("CouchPotato", "destination") +TV_dest = config.get("CouchPotato", "destination") +Use_cp = int(config.get("Torrent", "copy")) +packed = config.get("Torrent", "packed") +unpacker = config.get("Torrent", "unpacker") +parcheck = config.get("Torrent", "parcheck") + +if Category == Movie_Cat: + destination = Movie_dest +elif Category == TV_Cat: + destination = TV_dest +else; + print "Not assigned a label of either", Movie_Cat, "or", TV_Cat, ". Exiting" + +status = 0 +packed = 0 +par2 = 0 + +test = re.compile('^(.*)\.((zip|rar|7z|gz|bz|tar|arj)|(r[0-9]{1,3})|([0-9]{1,3}))$', re.IGNORECASE|re.UNICODE); +if test.match(destination): + print "packed files detected" + packed = 1 + #status = 1 + +test = os.path.join(destination, '*.par2') +if glob.glob(test): + print "par2 files detected" + par2 = 1 + +# QUESTION: Do we need this? PAR check is only for usenet? +if par2: + #parcheck here + #parcheck + +# TODO: Check that files actully is .mkv / .avi etc, and not packed files +if Use_cp and packed == 0: + print "copying all files from", Directory, "to", destination + shutil.copytree(Directory, destination) +else: + print "creating hard link from", Directory, "to", destination + shutil.copytree(src, dst, copy_function=os.link) + +if packed: + # 7z x test.rar ---- need to add "yes" to command + # windows only for now, should be adapted to *nix + cmd_7zip = [unpacker, 'x'] + ext_7zip = [".rar", + ".zip", + ".tar.gz", "tgz", + ".tar.bz2", ".tbz", + ".tar.lzma", ".tlz", + ".7z", ".xz"] + EXTRACT_COMMANDS = dict.fromkeys(ext_zip, cmd_7zip) + print('windows check passed') + + # save path + if Category == Movie_Cat: + save_path = Movie_dest + elif Category == TV_Cat: + save_path = TV_dest + + files = [ f for f in listdir(Directory) if isfile(join(Directory,f)) ] + + for f in files: + ext = os.path.splitext(f["path"]) + if ext[1] in (".gz", ".bz2", ".lzma"): + # check if this is a tar + if os.path.splitext(ext[0]) == ".tar": + cmd = EXTRACT_COMMANDS[".tar" + ext[1]] + else: + if ext[1] in EXTRACT_COMMANDS: + cmd = EXTRACT_COMMANDS[ext[1]] + else: + print("unknown file type: %s", ext[1]) + continue + + fp = os.path.join(save_path, os.path.normpath(f["path"])) + + # destination path + dest = os.path.join(save_path, Name) + + # create destionation folder + if not os.path.exists(dest): + try: + os.makedirs(dest) + except Exception, e: + print("cant create destination folder: %s", e) + return + + print("extracting to %s", dest) + def on_extract_success(result): + print("extract was successful for %s") + + def on_extract_failed(result, torrent_id): + print("extract failed for %s") + print("hmm %s %s %s %s", cmd[0], cmd[1], fp, dest) + + # running.. + d = getProcessValue(cmd[0], cmd[1].split() + [str(fp)], {}, str(dest)) + d.addCallback(on_extract_success) + d.addErrback(on_extract_failed) + + +status = 0 + +status = int(status) +##Now we pass off to CouchPotato or SickBeard. +if Category == Movie_Cat: + autoProcessMovie.process(destination, Name, status) +elif Category == TV_Cat: + autoProcessTV.processEpisode(destination, Name, status) diff --git a/TransmissionToMedia.py b/TransmissionToMedia.py deleted file mode 100755 index ccf65d38..00000000 --- a/TransmissionToMedia.py +++ /dev/null @@ -1,113 +0,0 @@ -#!/usr/bin/env python - -import sys -import autoProcessMovie -import autoProcessTV -import ConfigParser -import os -import glob - -##You can use the following parameters: -## -##%F - Name of downloaded file (for single file torrents) -##%D - Directory where files are saved -##%N - Title of torrent -##%P - Previous state of torrent -##%L - Label -##%T - Tracker -##%M - Status message string (same as status column) -##%I - hex encoded info-hash -##%S - State of torrent -##%K - kind of torrent (single|multi) -## -##Where State is one of: -## -##Error - 1 -##Checked - 2 -##Paused - 3 -##Super seeding - 4 -##Seeding - 5 -##Downloading - 6 -##Super seed [F] - 7 -##Seeding [F] - 8 -##Downloading [F] - 9 -##Queued seed - 10 -##Finished - 11 -##Queued - 12 -##Stopped - 13 - -## We will pass in %D, %N, %L -Directory = sys.argv[1] -Name = sys.argv[2] -Categoty = sys.argv[3] -print "transmissionToMedia v 4.0" - -config = ConfigParser.ConfigParser() -configFilename = os.path.join(os.path.dirname(sys.argv[0]), "TransmissionToMedia.cfg") - -print "Loading config from", configFilename - -if not os.path.isfile(configFilename): - print "ERROR: You need an autoProcessMovie.cfg file - did you rename and edit the .sample?" - sys.exit(-1) - -config.read(configFilename) - -Movie_Cat = config.get("CouchPotato", "category") -TV_Cat = config.get("SickBeard", "category") -Movie_dest = config.get("CouchPotato", "destination") -TV_dest = config.get("CouchPotato", "destination") -Use_cp = int(config.get("Transmission", "copy")) -unrar = config.get("Transmission", "unrar") -unzip = config.get("Transmission", "unzip") -parcheck = config.get("Transmission", "parcheck") - -if Category == Movie_Cat: - destination = Movie_dest -elif Category == TV_Cat: - destination = TV_dest -else; - print "Not assigned a label of either", Movie_Cat, "or", TV_Cat, ". Exiting" - -if Use_cp: - print "copying all files from", Directory, "to", destination - shutil.copytree(Directory, destination) -else: - print "creating hard link from", Directory, "to", destination - shutil.copytree(src, dst, copy_function=os.link) - -status = 0 -rared = 0 -par2 = 0 -ziptest = 0 -test = os.path.join(destination, '*.rar') -if glob.glob(test): - print "rar files detected" - rared = 1 - #status = 1 -test = os.path.join(destination, '*.par2') -if glob.glob(test): - print "par2 files detected" - par2 = 1 -test = os.path.join(destination, '*.zip') -if glob.glob(test): - print "zip files detected" - ziped = 1 - #status = 1 -if ziped: - #unzip here and reset status to 0 is successful - #unzip -if par2: - #parcheck here - #parcheck -if rared: - #unrar here and reset status to 0 if successful - #unrar -status = 0 - -status = int(status) -##Now we pass off to CouchPotato or SickBeard. -if Category == Movie_Cat: - autoProcessMovie.process(destination, Name, status) -elif Category == TV_Cat: - autoProcessTV.processEpisode(destination, Name, status)