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] 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: