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 1/9] 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) From 501fb42657ac1145e13506b44a27dc0f92e658b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joel=20K=C3=A5berg?= Date: Fri, 1 Feb 2013 09:33:08 +0100 Subject: [PATCH 2/9] cleanup --- TorrentToMedia.cfg.sample | 2 +- TorrentToMedia.py | 101 ++++++++++++++++++-------------------- 2 files changed, 49 insertions(+), 54 deletions(-) diff --git a/TorrentToMedia.cfg.sample b/TorrentToMedia.cfg.sample index 55646952..cbfc21ec 100644 --- a/TorrentToMedia.cfg.sample +++ b/TorrentToMedia.cfg.sample @@ -7,7 +7,7 @@ category = tv destination = /abs/path/to/complete/tv [Torrent] -copy = 1 +uselink = 1 unpacker = 'C:\\Program Files\\7-Zip\\7z.exe' parcheck = /abs/path/to/par2 diff --git a/TorrentToMedia.py b/TorrentToMedia.py index aeccf5cf..40df71a8 100644 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -10,7 +10,7 @@ from os.path import isfile, join import glob -##You can use the following parameters: +##You can use the following parameters (UTORRENT): ## ##%F - Name of downloaded file (for single file torrents) ##%D - Directory where files are saved @@ -40,14 +40,18 @@ import glob ##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" +Directory = sys.argv[1] ## Example output: F:\path\to\dir\My.Series.S01E01.720p.HDTV.x264-2HD +Name = sys.argv[2] ## Example output: My.Series.S01E01.720p.HDTV.x264-2HD +Category = sys.argv[3] ## Example output: tvseries # this is the label in uTorrent + +status = 0 +packed = 0 +par2 = 0 config = ConfigParser.ConfigParser() configFilename = os.path.join(os.path.dirname(sys.argv[0]), "TorrentToMedia.cfg") +print "transmissionToMedia v 4.1" print "Loading config from", configFilename if not os.path.isfile(configFilename): @@ -58,11 +62,14 @@ 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")) + +useLink = int(config.get("Torrent", "uselink")) packed = config.get("Torrent", "packed") unpacker = config.get("Torrent", "unpacker") + parcheck = config.get("Torrent", "parcheck") if Category == Movie_Cat: @@ -72,37 +79,26 @@ elif Category == TV_Cat: 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): +if glob.glob(test): print "par2 files detected" par2 = 1 -# QUESTION: Do we need this? PAR check is only for usenet? +## 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 + ## 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", @@ -112,19 +108,13 @@ if packed: ".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 + ## check if this is a tar if os.path.splitext(ext[0]) == ".tar": cmd = EXTRACT_COMMANDS[".tar" + ext[1]] else: @@ -134,37 +124,42 @@ if packed: print("unknown file type: %s", ext[1]) continue - fp = os.path.join(save_path, os.path.normpath(f["path"])) + fp = os.path.join(save_path, os.path.normpath(f["path"])) - # destination path - dest = os.path.join(save_path, Name) + ## destination path + dest = os.path.join(destination, Name) - # create destionation folder - if not os.path.exists(dest): - try: - os.makedirs(dest) - except Exception, e: - print("cant create destination folder: %s", e) + ## 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") + 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) + 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 + ## running.. + d = getProcessValue(cmd[0], cmd[1].split() + [str(fp)], {}, str(dest)) + d.addCallback(on_extract_success) + d.addErrback(on_extract_failed) +else: + ## TODO: Check that files actully is .mkv / .avi etc, and not packed files + if useLink 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) status = int(status) -##Now we pass off to CouchPotato or SickBeard. +## Now we pass off to CouchPotato or SickBeard. if Category == Movie_Cat: autoProcessMovie.process(destination, Name, status) elif Category == TV_Cat: From ce0d4b3f23a9a933c665a532f17dc6b2cf14983c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joel=20K=C3=A5berg?= Date: Fri, 1 Feb 2013 10:19:32 +0100 Subject: [PATCH 3/9] more cleanup --- TorrentToMedia.cfg.sample | 5 +-- TorrentToMedia.py | 81 ++++++++++++++------------------------- 2 files changed, 30 insertions(+), 56 deletions(-) diff --git a/TorrentToMedia.cfg.sample b/TorrentToMedia.cfg.sample index cbfc21ec..e1e4c646 100644 --- a/TorrentToMedia.cfg.sample +++ b/TorrentToMedia.cfg.sample @@ -7,9 +7,8 @@ category = tv destination = /abs/path/to/complete/tv [Torrent] -uselink = 1 -unpacker = 'C:\\Program Files\\7-Zip\\7z.exe' -parcheck = /abs/path/to/par2 +uselink = 0 +extractiontool = 'C:\\Program Files\\7-Zip\\7z.exe' [Notes] copy = 0 will create hard links to your original download. diff --git a/TorrentToMedia.py b/TorrentToMedia.py index 40df71a8..9d363f97 100644 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -1,14 +1,10 @@ #!/usr/bin/env python -import sys import autoProcessMovie import autoProcessTV -import ConfigParser -import os +import sys, os, glob, ConfigParser from os import listdir from os.path import isfile, join -import glob - ##You can use the following parameters (UTORRENT): ## @@ -46,7 +42,6 @@ Category = sys.argv[3] ## Example output: tvseries # this is the label in uTorre status = 0 packed = 0 -par2 = 0 config = ConfigParser.ConfigParser() configFilename = os.path.join(os.path.dirname(sys.argv[0]), "TorrentToMedia.cfg") @@ -60,17 +55,12 @@ if not os.path.isfile(configFilename): 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") - +Movie_dest = config.get("CouchPotato", "destination") +Movie_Cat = config.get("CouchPotato", "category") useLink = int(config.get("Torrent", "uselink")) -packed = config.get("Torrent", "packed") -unpacker = config.get("Torrent", "unpacker") - -parcheck = config.get("Torrent", "parcheck") +extractionTool = config.get("Torrent", "extractiontool") if Category == Movie_Cat: destination = Movie_dest @@ -79,84 +69,69 @@ elif Category == TV_Cat: else; print "Not assigned a label of either", Movie_Cat, "or", TV_Cat, ". Exiting" - - 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" + print "Found compressed archives, extracting" packed = 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 - -if packed: +## TODO: Check that files actully is .mkv / .avi etc, and not packed files or anything else +if useLink == 1 and packed == 0: ## symlinks + print "Copying all files from", Directory, "to", destination + shutil.copytree(Directory, destination) + +elif useLink == 0 and packed == 0: ## hardlink + print "Creating hard link from", Directory, "to", destination + shutil.copytree(src, dst, copy_function=os.link) + +elif useLink == 0 and packed == 1: ## unpack ## 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"] + ## Windows only for now, should be adapted to *nix + cmd_7zip = [extractionTool, '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') + print('Found 7zip') 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 + ## 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]) + print("ERROR: Unknown file type: %s", ext[1]) continue fp = os.path.join(save_path, os.path.normpath(f["path"])) - ## destination path + ## Destination path dest = os.path.join(destination, Name) - ## create destionation folder + ## Create destionation folder if not os.path.exists(dest): try: os.makedirs(dest) except Exception, e: - print("cant create destination folder: %s", e) + print("ERROR: Not possible to create destination folder: %s", e) return print("extracting to %s", dest) def on_extract_success(result): - print("extract was successful for %s") + print("Extract was successful for %s") def on_extract_failed(result, torrent_id): - print("extract failed for %s") + print("ERROR: Extract failed for %s") print("hmm %s %s %s %s", cmd[0], cmd[1], fp, dest) - ## running.. + ## Running.. d = getProcessValue(cmd[0], cmd[1].split() + [str(fp)], {}, str(dest)) d.addCallback(on_extract_success) d.addErrback(on_extract_failed) else: - ## TODO: Check that files actully is .mkv / .avi etc, and not packed files - if useLink 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) + print "Didn't find any compressed archives or media files to process, exiting" status = int(status) ## Now we pass off to CouchPotato or SickBeard. From 7c4e14cca066cdd2c4e1d41cfaab03923d02ff4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joel=20K=C3=A5berg?= Date: Fri, 1 Feb 2013 13:17:55 +0100 Subject: [PATCH 4/9] support for *nix and nt systems --- TorrentToMedia.py | 56 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/TorrentToMedia.py b/TorrentToMedia.py index 9d363f97..3a3948f6 100644 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -85,25 +85,45 @@ elif useLink == 0 and packed == 0: ## hardlink elif useLink == 0 and packed == 1: ## unpack ## 7z x test.rar ---- need to add "yes" to command - ## Windows only for now, should be adapted to *nix - cmd_7zip = [extractionTool, '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('Found 7zip') - 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("ERROR: Unknown file type: %s", ext[1]) - continue + ## Using Windows? + if os.name == 'nt': + cmd_7zip = [extractionTool, '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('Found 7zip') + + 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("ERROR: Unknown file type: %s", ext[1]) + continue + ## Using linux? + elif os.name == 'posix': + required_cmds=["unrar", "unzip", "tar", "unxz", "unlzma", "7zr"] + EXTRACT_COMMANDS = { + ".rar": ["unrar", "x -o+ -y"], + ".zip": ["unzip", ""], + ".tar.gz": ["tar", "xzf"], + ".tgz": ["tar", "xzf"], + ".tar.bz2": ["tar", "xjf"], + ".tbz": ["tar", "xjf"], + ".tar.lzma": ["tar", "--lzma xf"], + ".tlz": ["tar", "--lzma xf"], + ".txz": ["tar", "--xz xf"], + ".7z": ["7zr", "x"], + } + ## Need to add check for which commands that can be utilized in Linux.. + else: + print "Unknown OS, exiting" fp = os.path.join(save_path, os.path.normpath(f["path"])) From 68afb6a0854091232b7aecd1f4d01311698da019 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joel=20K=C3=A5berg?= Date: Fri, 1 Feb 2013 13:39:34 +0100 Subject: [PATCH 5/9] to fast to furious, cleanup --- TorrentToMedia.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TorrentToMedia.py b/TorrentToMedia.py index 3a3948f6..98c65b7c 100644 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -131,7 +131,7 @@ elif useLink == 0 and packed == 1: ## unpack dest = os.path.join(destination, Name) ## Create destionation folder - if not os.path.exists(dest): + if not os.path.exists(dest): try: os.makedirs(dest) except Exception, e: From ca9ffb3ce53fabc606444e36607ef2d2238329f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joel=20K=C3=A5berg?= Date: Fri, 1 Feb 2013 13:43:18 +0100 Subject: [PATCH 6/9] cleanup --- TorrentToMedia.py | 100 +++++++++++++++++++++++----------------------- 1 file changed, 51 insertions(+), 49 deletions(-) diff --git a/TorrentToMedia.py b/TorrentToMedia.py index 98c65b7c..89d4c97d 100644 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -35,10 +35,10 @@ from os.path import isfile, join ##Queued - 12 ##Stopped - 13 -## We will pass in %D, %N, %L -Directory = sys.argv[1] ## Example output: F:\path\to\dir\My.Series.S01E01.720p.HDTV.x264-2HD -Name = sys.argv[2] ## Example output: My.Series.S01E01.720p.HDTV.x264-2HD -Category = sys.argv[3] ## Example output: tvseries # this is the label in uTorrent +## We will pass in %D, %N, %L from uTorrent +Directory = sys.argv[1] ## %D -- Example output: F:\path\to\dir\My.Series.S01E01.720p.HDTV.x264-2HD +Name = sys.argv[2] ## %N -- Example output: My.Series.S01E01.720p.HDTV.x264-2HD +Category = sys.argv[3] ## %L -- Example output: tvseries ## This is the label in uTorrent status = 0 packed = 0 @@ -47,7 +47,7 @@ config = ConfigParser.ConfigParser() configFilename = os.path.join(os.path.dirname(sys.argv[0]), "TorrentToMedia.cfg") print "transmissionToMedia v 4.1" -print "Loading config from", configFilename +print "INFO: 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?" @@ -67,20 +67,20 @@ if Category == Movie_Cat: elif Category == TV_Cat: destination = TV_dest else; - print "Not assigned a label of either", Movie_Cat, "or", TV_Cat, ". Exiting" + print "INFO: Not assigned a label of either", Movie_Cat, "or", TV_Cat, ". Exiting" 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 "Found compressed archives, extracting" + print "INFO: Found compressed archives, extracting" packed = 1 ## TODO: Check that files actully is .mkv / .avi etc, and not packed files or anything else if useLink == 1 and packed == 0: ## symlinks - print "Copying all files from", Directory, "to", destination + print "INFO: Copying all files from", Directory, "to", destination shutil.copytree(Directory, destination) elif useLink == 0 and packed == 0: ## hardlink - print "Creating hard link from", Directory, "to", destination + print "INFO: Creating hard link from", Directory, "to", destination shutil.copytree(src, dst, copy_function=os.link) elif useLink == 0 and packed == 1: ## unpack @@ -89,23 +89,10 @@ elif useLink == 0 and packed == 1: ## unpack ## Using Windows? if os.name == 'nt': cmd_7zip = [extractionTool, 'x'] - ext_7zip = [".rar",".zip",".tar.gz","tgz",".tar.bz2", ".tbz",".tar.lzma", ".tlz",".7z", ".xz"] + ext_7zip = [".rar",".zip",".tar.gz","tgz",".tar.bz2",".tbz",".tar.lzma",".tlz",".7z",".xz"] EXTRACT_COMMANDS = dict.fromkeys(ext_zip, cmd_7zip) - print('Found 7zip') - - 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("ERROR: Unknown file type: %s", ext[1]) - continue + print "INFO: We are using Windows" + ## Using linux? elif os.name == 'posix': required_cmds=["unrar", "unzip", "tar", "unxz", "unlzma", "7zr"] @@ -121,37 +108,52 @@ elif useLink == 0 and packed == 1: ## unpack ".txz": ["tar", "--xz xf"], ".7z": ["7zr", "x"], } - ## Need to add check for which commands that can be utilized in Linux.. + print "INFO: We are using *nix" + + ## Need to add a check for which commands that can be utilized in *nix systems.. else: - print "Unknown OS, exiting" + print "ERROR: Unknown OS, exiting" - fp = os.path.join(save_path, os.path.normpath(f["path"])) + 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("ERROR: Unknown file type: %s", ext[1]) + continue + fp = os.path.join(save_path, os.path.normpath(f["path"])) - ## Destination path - dest = os.path.join(destination, Name) + ## Destination path + dest = os.path.join(destination, Name) - ## Create destionation folder - if not os.path.exists(dest): - try: - os.makedirs(dest) - except Exception, e: - print("ERROR: Not possible to create destination folder: %s", e) - return + ## Create destionation folder + if not os.path.exists(dest): + try: + os.makedirs(dest) + except Exception, e: + print("ERROR: Not possible to create destination folder: %s", e) + return - print("extracting to %s", dest) - def on_extract_success(result): - print("Extract was successful for %s") + print("INFO: Extracting to %s", dest) + def on_extract_success(result): + print("INFO: Extraction was successful for %s") - def on_extract_failed(result, torrent_id): - print("ERROR: 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) + def on_extract_failed(result): + print("ERROR: Extraction failed for %s") + + ## Running.. + print("INFO: Extracting %s %s %s %s", cmd[0], cmd[1], fp, dest) + d = getProcessValue(cmd[0], cmd[1].split() + [str(fp)], {}, str(dest)) + d.addCallback(on_extract_success) + d.addErrback(on_extract_failed) else: - print "Didn't find any compressed archives or media files to process, exiting" + print "INFO: Didn't find any compressed archives or media files to process, exiting" status = int(status) ## Now we pass off to CouchPotato or SickBeard. From 1c1dc35e4c004582635c54540e69dea8bb017d1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joel=20K=C3=A5berg?= Date: Fri, 1 Feb 2013 13:47:08 +0100 Subject: [PATCH 7/9] always assume -y switch on windows --- TorrentToMedia.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TorrentToMedia.py b/TorrentToMedia.py index 89d4c97d..cd55c117 100644 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -88,7 +88,7 @@ elif useLink == 0 and packed == 1: ## unpack ## Using Windows? if os.name == 'nt': - cmd_7zip = [extractionTool, 'x'] + cmd_7zip = [extractionTool, 'x -y'] ext_7zip = [".rar",".zip",".tar.gz","tgz",".tar.bz2",".tbz",".tar.lzma",".tlz",".7z",".xz"] EXTRACT_COMMANDS = dict.fromkeys(ext_zip, cmd_7zip) print "INFO: We are using Windows" From 5e9677be6c9e8bbb90a9403bc09cff9833a603a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joel=20K=C3=A5berg?= Date: Fri, 1 Feb 2013 14:20:26 +0100 Subject: [PATCH 8/9] cleanup --- TorrentToMedia.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/TorrentToMedia.py b/TorrentToMedia.py index cd55c117..3c3a793d 100644 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -46,7 +46,7 @@ packed = 0 config = ConfigParser.ConfigParser() configFilename = os.path.join(os.path.dirname(sys.argv[0]), "TorrentToMedia.cfg") -print "transmissionToMedia v 4.1" +print "torrentToMedia v 4.1" print "INFO: Loading config from", configFilename if not os.path.isfile(configFilename): @@ -84,8 +84,6 @@ elif useLink == 0 and packed == 0: ## hardlink shutil.copytree(src, dst, copy_function=os.link) elif useLink == 0 and packed == 1: ## unpack - ## 7z x test.rar ---- need to add "yes" to command - ## Using Windows? if os.name == 'nt': cmd_7zip = [extractionTool, 'x -y'] @@ -127,7 +125,7 @@ elif useLink == 0 and packed == 1: ## unpack else: print("ERROR: Unknown file type: %s", ext[1]) continue - fp = os.path.join(save_path, os.path.normpath(f["path"])) + fp = os.path.join(destination, os.path.normpath(f["path"])) ## Destination path dest = os.path.join(destination, Name) From 1ecd7a790ab76a04ffa5a527e15ccf91eaf79ab8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joel=20K=C3=A5berg?= Date: Fri, 1 Feb 2013 14:31:21 +0100 Subject: [PATCH 9/9] typo :( --- TorrentToMedia.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TorrentToMedia.py b/TorrentToMedia.py index 3c3a793d..07342601 100644 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -70,7 +70,7 @@ else; print "INFO: Not assigned a label of either", Movie_Cat, "or", TV_Cat, ". Exiting" 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): +if test.match(Directory): print "INFO: Found compressed archives, extracting" packed = 1