From e322c81392c1ad204c4c4ffe091786c51c6536e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joel=20K=C3=A5berg?= Date: Sat, 2 Mar 2013 12:49:22 +0100 Subject: [PATCH] more cleanup, fixes --- TorrentToMedia.py | 84 +++++++++++++++++++++++------------------- extractor/extractor.py | 3 +- nzbToMediaUtil.py | 16 ++++---- 3 files changed, 55 insertions(+), 48 deletions(-) diff --git a/TorrentToMedia.py b/TorrentToMedia.py index 31bcc6e5..ad8d2949 100755 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -21,12 +21,10 @@ from utorrent.client import UTorrentClient def main(inputDirectory, inputName, inputCategory, inputHash) - status = int(1) # We start as "failed" until we verify movie file in destination + status = int(1) # 1 = failed | 0 = success root = int(0) video = int(0) video2 = int(0) - failed_link = int(0) - failed_extract = int(0) Logger.debug("MAIN: Received Directory: %s | Name: %s | Category: %s", inputDirectory, inputName, inputCategory) @@ -40,22 +38,26 @@ def main(inputDirectory, inputName, inputCategory, inputHash) Logger.debug("MAIN: Future versions of this script might do something for Category: %s. Keep updating ;)", inputCategory) sys.exit(-1) - Logger.debug("MAIN: Scanning files in directory: %s", inputDirectory) - if root == 1: - Logger.debug("MAIN: Looking for %s in filename", inputName) - elif root == 2: - Logger.debug("MAIN: Looking for files with modified/created dates less than 5 minutes old.") + Logger.debug("MAIN: Scanning files in directory: %s", inputDirectory) now = datetime.datetime.now() for dirpath, dirnames, filenames in os.walk(inputDirectory): for file in filenames: + + filePath = os.path.join(dirpath, file) + fileExtention = os.path.splitext(file)[1] + targetDirectory = os.path.join(outputDestination, file) + if root == 1: + Logger.debug("MAIN: Looking for %s in filename", inputName) if (inputName in file) or (os.path.splitext(file)[0] in inputName): pass # This file does match the Torrent name Logger.debug("Found file %s that matches Torrent Name %s", file, inputName) else: continue # This file does not match the Torrent name, skip it + if root == 2: + Logger.debug("MAIN: Looking for files with modified/created dates less than 5 minutes old.") mtime_lapse = now - datetime.datetime.fromtimestamp(os.path.getmtime(os.path.join(dirpath, file))) ctime_lapse = now - datetime.datetime.fromtimestamp(os.path.getctime(os.path.join(dirpath, file))) if (mtime_lapse < datetime.timedelta(minutes=5)) or (ctime_lapse < datetime.timedelta(minutes=5)): @@ -63,37 +65,39 @@ def main(inputDirectory, inputName, inputCategory, inputHash) Logger.debug("Found file %s with date modifed/created less than 5 minutes ago.", file) else: continue # This file has not been recently moved or created, skip it - filePath = os.path.join(dirpath, file) - fileExtention = os.path.splitext(file)[1] + if fileExtention in mediaContainer: # If the file is a video file if is_sample(filePath, inputName, minSampleSize): # Ignore samples Logger.info("MAIN: Ignoring sample file: %s ", filePath) continue else: video = video + 1 - source = filePath - target = os.path.join(outputDestination, file) Logger.info("MAIN: Found video file %s in %s", fileExtention, filePath) - state = copy_link(source, target, useLink, outputDestination) - if state == False: + try: + copy_link(filePath, targetDirectory, useLink, outputDestination) + except Exception as e: Logger.error("MAIN: Failed to link file %s", file) - failed_link = 1 + Logger.debug e + linkFailed = True + elif fileExtention in metaContainer: - source = filePath - target = os.path.join(outputDestination, file) Logger.info("MAIN: Found metadata file %s for file %s", fileExtention, filePath) - state = copy_link(source, target, useLink, outputDestination) - if state == False: + try: + copy_link(filePath, targetDirectory, useLink, outputDestination) + except Exception as e: Logger.error("MAIN: Failed to link file %s", file) - failed_link = 1 + Logger.debug e + linkFailed = True + elif fileExtention in compressedContainer: Logger.info("MAIN: Found compressed archive %s for file %s", fileExtention, filePath) - source = filePath - target = os.path.join(outputDestination, file) try: - extractor.extract(dirpath, file, outputDestination) - except: - Logger.warn("Extraction failed for %s", file) + extractor.extract(filePath, outputDestination) + except Exception as e: + Logger.warn("Extraction failed for: %s", file) + Logger.debug e + extractFailed = True + else: Logger.debug("MAIN: Ignoring unknown filetype %s for file %s", fileExtention, filePath) continue @@ -117,7 +121,7 @@ def main(inputDirectory, inputName, inputCategory, inputHash) if status == 0: #### Maybe we should move this to a more appropriate place? Logger.info("MAIN: Successful run") Logger.debug("MAIN: Calling autoProcess script for successful download.") - elif failed_extract == 1 and failed_link == 0: # failed to extract files only. + elif extractFailed and linkFailed == False: # failed to extract files only. Logger.info("MAIN: Failed to extract a compressed archive") Logger.debug("MAIN: Assume this to be password protected file.") Logger.debug("MAIN: Calling autoProcess script for failed download.") @@ -125,40 +129,44 @@ def main(inputDirectory, inputName, inputCategory, inputHash) Logger.error("MAIN: Something failed! Please check logs. Exiting") sys.exit(-1) - # Hardlink solution with uTorrent - if inputHash and useLink: + #### quick 'n dirt hardlink solution for uTorrent, need to implent support for deluge, transmission + if inputHash and useLink and clientAgent == 'utorrent': try: Logger.debug("MAIN: Connecting to uTorrent: %s", uTorrentWEBui) utorrentClass = UTorrentClient(uTorrentWEBui, uTorrentUSR, uTorrentPWD) - except: - Logger.error("MAIN: Failed to connect to uTorrent") + except Exception as e: + Logger.error("MAIN: Failed to connect to uTorrent: %s", e) - Logger.debug("MAIN: Stoping torrent %s in uTorrent while processing", videofile) + Logger.debug("MAIN: Stoping torrent %s in uTorrent while processing", inputName) utorrentClass.stop(inputHash) time.sleep(5) # Give uTorrent some time to catch up with the change + ##### quick 'n dirt hardlink solution for uTorrent, need to implent support for deluge, transmission # Now we pass off to CouchPotato or Sick-Beard if inputCategory == movieCategory: - Logger.info("MAIN: Calling CouchPotatoServer to post-process: %s", videofile) # can we use logger while logfile open? + Logger.info("MAIN: Calling CouchPotatoServer to post-process: %s", inputName) # can we use logger while logfile open? autoProcessMovie.process(outputDestination, inputName, status) elif inputCategory == tvCategory: - Logger.info("MAIN: Calling Sick-Beard to post-process: %s", videofile) # can we use logger while logfile open? + Logger.info("MAIN: Calling Sick-Beard to post-process: %s", inputName) # can we use logger while logfile open? autoProcessTV.processEpisode(outputDestination, inputName, status) # Check if the file still exists in the post-process directory now = datetime.datetime.now() # set time for timeout - while os.path.exists(videofile): # while this file is still here, CPS hasn't finished renaming + while os.path.exists(outputDestination): # while this directory is still here, CPS hasn't finished renaming if (datetime.datetime.now() - now) > datetime.timedelta(minutes=3): # note; minimum 1 minute delay in autoProcessMovie - Logger.info("MAIN: The file %s has not been moved after 3 minutes.", videofile) + Logger.info("MAIN: The directory %s has not been moved after 3 minutes.", outputDestination) break time.sleep(10) #Just stop this looping infinitely and hogging resources for 3 minutes ;) else: # CPS (and SickBeard) have finished. We can now resume seeding. - Logger.info("MAIN: Post-process appears to have succeeded for: %s", videofile) + Logger.info("MAIN: Post-process appears to have succeeded for: %s", inputName) - # Hardlink solution with uTorrent - if inputHash and useLink: + #### quick 'n dirt hardlink solution for uTorrent, need to implent support for deluge, transmission + if inputHash and useLink and clientAgent == 'utorrent': Logger.debug("MAIN: Starting torrent %s in uTorrent", inputName) utorrentClass.start(inputHash) + #### quick 'n dirt hardlink solution for uTorrent, need to implent support for deluge, transmission + + Logger.info("MAIN: All done.") if __name__ == "__main__": diff --git a/extractor/extractor.py b/extractor/extractor.py index 9835ca7f..4755d988 100644 --- a/extractor/extractor.py +++ b/extractor/extractor.py @@ -36,7 +36,7 @@ def which(program): return None -def extract(dirpath, file, outputDestination): +def extract(filePath, outputDestination): # Using Windows if os.name == 'nt': if os_platform() == 'AMD64': @@ -89,7 +89,6 @@ def extract(dirpath, file, outputDestination): Logger.warn("EXTRACTOR: No archive extracting programs found, plugin will be disabled") ext = os.path.splitext(file) - filePath = os.path.join(dirpath, file) if ext[1] in (".gz", ".bz2", ".lzma"): # Check if this is a tar if os.path.splitext(ext[0])[1] == ".tar": diff --git a/nzbToMediaUtil.py b/nzbToMediaUtil.py index 92298288..12509405 100644 --- a/nzbToMediaUtil.py +++ b/nzbToMediaUtil.py @@ -132,22 +132,22 @@ def is_sample(filePath, inputName, minSampleSize): return ('sample' in filePath.lower()) and (not 'sample' in inputName) and (os.path.getsize(filePath) < SIZE_CUTOFF) -def copy_link(source, target, useLink, outputDestination): +def copy_link(filePath, targetDirectory, useLink, outputDestination): create_destination(outputDestination) if useLink: try: - Logger.info("COPYLINK: Linking %s to %s", source, target) - linktastic.link(source, target) + Logger.info("COPYLINK: Linking %s to %s", filePath, targetDirectory) + linktastic.link(filePath, targetDirectory) except: - if os.path.isfile(target): + if os.path.isfile(targetDirectory): Logger.info("COPYLINK: Something went wrong in linktastic.link, but the destination file was created") else: Logger.info("COPYLINK: Something went wrong in linktastic.link, copying instead") - Logger.debug("COPYLINK: Copying %s to %s", source, target) - shutil.copy(source, target) + Logger.debug("COPYLINK: Copying %s to %s", filePath, targetDirectory) + shutil.copy(filePath, targetDirectory) else: - Logger.debug("Copying %s to %s", source, target) - shutil.copy(source, target) + Logger.debug("Copying %s to %s", filePath, targetDirectory) + shutil.copy(filePath, targetDirectory) return True