mirror of
https://github.com/clinton-hall/nzbToMedia.git
synced 2025-08-19 21:03:14 -07:00
more cleanup, fixes
This commit is contained in:
parent
42ba5b11e1
commit
e322c81392
3 changed files with 55 additions and 48 deletions
|
@ -21,12 +21,10 @@ from utorrent.client import UTorrentClient
|
||||||
|
|
||||||
def main(inputDirectory, inputName, inputCategory, inputHash)
|
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)
|
root = int(0)
|
||||||
video = int(0)
|
video = int(0)
|
||||||
video2 = 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)
|
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)
|
Logger.debug("MAIN: Future versions of this script might do something for Category: %s. Keep updating ;)", inputCategory)
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
|
|
||||||
Logger.debug("MAIN: Scanning files in directory: %s", inputDirectory)
|
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.")
|
|
||||||
|
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
for dirpath, dirnames, filenames in os.walk(inputDirectory):
|
for dirpath, dirnames, filenames in os.walk(inputDirectory):
|
||||||
for file in filenames:
|
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:
|
if root == 1:
|
||||||
|
Logger.debug("MAIN: Looking for %s in filename", inputName)
|
||||||
if (inputName in file) or (os.path.splitext(file)[0] in inputName):
|
if (inputName in file) or (os.path.splitext(file)[0] in inputName):
|
||||||
pass # This file does match the Torrent name
|
pass # This file does match the Torrent name
|
||||||
Logger.debug("Found file %s that matches Torrent Name %s", file, inputName)
|
Logger.debug("Found file %s that matches Torrent Name %s", file, inputName)
|
||||||
else:
|
else:
|
||||||
continue # This file does not match the Torrent name, skip it
|
continue # This file does not match the Torrent name, skip it
|
||||||
|
|
||||||
if root == 2:
|
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)))
|
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)))
|
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)):
|
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)
|
Logger.debug("Found file %s with date modifed/created less than 5 minutes ago.", file)
|
||||||
else:
|
else:
|
||||||
continue # This file has not been recently moved or created, skip it
|
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 fileExtention in mediaContainer: # If the file is a video file
|
||||||
if is_sample(filePath, inputName, minSampleSize): # Ignore samples
|
if is_sample(filePath, inputName, minSampleSize): # Ignore samples
|
||||||
Logger.info("MAIN: Ignoring sample file: %s ", filePath)
|
Logger.info("MAIN: Ignoring sample file: %s ", filePath)
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
video = video + 1
|
video = video + 1
|
||||||
source = filePath
|
|
||||||
target = os.path.join(outputDestination, file)
|
|
||||||
Logger.info("MAIN: Found video file %s in %s", fileExtention, filePath)
|
Logger.info("MAIN: Found video file %s in %s", fileExtention, filePath)
|
||||||
state = copy_link(source, target, useLink, outputDestination)
|
try:
|
||||||
if state == False:
|
copy_link(filePath, targetDirectory, useLink, outputDestination)
|
||||||
|
except Exception as e:
|
||||||
Logger.error("MAIN: Failed to link file %s", file)
|
Logger.error("MAIN: Failed to link file %s", file)
|
||||||
failed_link = 1
|
Logger.debug e
|
||||||
|
linkFailed = True
|
||||||
|
|
||||||
elif fileExtention in metaContainer:
|
elif fileExtention in metaContainer:
|
||||||
source = filePath
|
|
||||||
target = os.path.join(outputDestination, file)
|
|
||||||
Logger.info("MAIN: Found metadata file %s for file %s", fileExtention, filePath)
|
Logger.info("MAIN: Found metadata file %s for file %s", fileExtention, filePath)
|
||||||
state = copy_link(source, target, useLink, outputDestination)
|
try:
|
||||||
if state == False:
|
copy_link(filePath, targetDirectory, useLink, outputDestination)
|
||||||
|
except Exception as e:
|
||||||
Logger.error("MAIN: Failed to link file %s", file)
|
Logger.error("MAIN: Failed to link file %s", file)
|
||||||
failed_link = 1
|
Logger.debug e
|
||||||
|
linkFailed = True
|
||||||
|
|
||||||
elif fileExtention in compressedContainer:
|
elif fileExtention in compressedContainer:
|
||||||
Logger.info("MAIN: Found compressed archive %s for file %s", fileExtention, filePath)
|
Logger.info("MAIN: Found compressed archive %s for file %s", fileExtention, filePath)
|
||||||
source = filePath
|
|
||||||
target = os.path.join(outputDestination, file)
|
|
||||||
try:
|
try:
|
||||||
extractor.extract(dirpath, file, outputDestination)
|
extractor.extract(filePath, outputDestination)
|
||||||
except:
|
except Exception as e:
|
||||||
Logger.warn("Extraction failed for %s", file)
|
Logger.warn("Extraction failed for: %s", file)
|
||||||
|
Logger.debug e
|
||||||
|
extractFailed = True
|
||||||
|
|
||||||
else:
|
else:
|
||||||
Logger.debug("MAIN: Ignoring unknown filetype %s for file %s", fileExtention, filePath)
|
Logger.debug("MAIN: Ignoring unknown filetype %s for file %s", fileExtention, filePath)
|
||||||
continue
|
continue
|
||||||
|
@ -117,7 +121,7 @@ def main(inputDirectory, inputName, inputCategory, inputHash)
|
||||||
if status == 0: #### Maybe we should move this to a more appropriate place?
|
if status == 0: #### Maybe we should move this to a more appropriate place?
|
||||||
Logger.info("MAIN: Successful run")
|
Logger.info("MAIN: Successful run")
|
||||||
Logger.debug("MAIN: Calling autoProcess script for successful download.")
|
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.info("MAIN: Failed to extract a compressed archive")
|
||||||
Logger.debug("MAIN: Assume this to be password protected file.")
|
Logger.debug("MAIN: Assume this to be password protected file.")
|
||||||
Logger.debug("MAIN: Calling autoProcess script for failed download.")
|
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")
|
Logger.error("MAIN: Something failed! Please check logs. Exiting")
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
|
|
||||||
# Hardlink solution with uTorrent
|
#### quick 'n dirt hardlink solution for uTorrent, need to implent support for deluge, transmission
|
||||||
if inputHash and useLink:
|
if inputHash and useLink and clientAgent == 'utorrent':
|
||||||
try:
|
try:
|
||||||
Logger.debug("MAIN: Connecting to uTorrent: %s", uTorrentWEBui)
|
Logger.debug("MAIN: Connecting to uTorrent: %s", uTorrentWEBui)
|
||||||
utorrentClass = UTorrentClient(uTorrentWEBui, uTorrentUSR, uTorrentPWD)
|
utorrentClass = UTorrentClient(uTorrentWEBui, uTorrentUSR, uTorrentPWD)
|
||||||
except:
|
except Exception as e:
|
||||||
Logger.error("MAIN: Failed to connect to uTorrent")
|
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)
|
utorrentClass.stop(inputHash)
|
||||||
time.sleep(5) # Give uTorrent some time to catch up with the change
|
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
|
# Now we pass off to CouchPotato or Sick-Beard
|
||||||
if inputCategory == movieCategory:
|
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)
|
autoProcessMovie.process(outputDestination, inputName, status)
|
||||||
elif inputCategory == tvCategory:
|
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)
|
autoProcessTV.processEpisode(outputDestination, inputName, status)
|
||||||
|
|
||||||
# Check if the file still exists in the post-process directory
|
# Check if the file still exists in the post-process directory
|
||||||
now = datetime.datetime.now() # set time for timeout
|
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
|
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
|
break
|
||||||
time.sleep(10) #Just stop this looping infinitely and hogging resources for 3 minutes ;)
|
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.
|
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
|
#### quick 'n dirt hardlink solution for uTorrent, need to implent support for deluge, transmission
|
||||||
if inputHash and useLink:
|
if inputHash and useLink and clientAgent == 'utorrent':
|
||||||
Logger.debug("MAIN: Starting torrent %s in uTorrent", inputName)
|
Logger.debug("MAIN: Starting torrent %s in uTorrent", inputName)
|
||||||
utorrentClass.start(inputHash)
|
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__":
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
|
|
@ -36,7 +36,7 @@ def which(program):
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def extract(dirpath, file, outputDestination):
|
def extract(filePath, outputDestination):
|
||||||
# Using Windows
|
# Using Windows
|
||||||
if os.name == 'nt':
|
if os.name == 'nt':
|
||||||
if os_platform() == 'AMD64':
|
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")
|
Logger.warn("EXTRACTOR: No archive extracting programs found, plugin will be disabled")
|
||||||
|
|
||||||
ext = os.path.splitext(file)
|
ext = os.path.splitext(file)
|
||||||
filePath = os.path.join(dirpath, file)
|
|
||||||
if ext[1] in (".gz", ".bz2", ".lzma"):
|
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])[1] == ".tar":
|
if os.path.splitext(ext[0])[1] == ".tar":
|
||||||
|
|
|
@ -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)
|
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)
|
create_destination(outputDestination)
|
||||||
if useLink:
|
if useLink:
|
||||||
try:
|
try:
|
||||||
Logger.info("COPYLINK: Linking %s to %s", source, target)
|
Logger.info("COPYLINK: Linking %s to %s", filePath, targetDirectory)
|
||||||
linktastic.link(source, target)
|
linktastic.link(filePath, targetDirectory)
|
||||||
except:
|
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")
|
Logger.info("COPYLINK: Something went wrong in linktastic.link, but the destination file was created")
|
||||||
else:
|
else:
|
||||||
Logger.info("COPYLINK: Something went wrong in linktastic.link, copying instead")
|
Logger.info("COPYLINK: Something went wrong in linktastic.link, copying instead")
|
||||||
Logger.debug("COPYLINK: Copying %s to %s", source, target)
|
Logger.debug("COPYLINK: Copying %s to %s", filePath, targetDirectory)
|
||||||
shutil.copy(source, target)
|
shutil.copy(filePath, targetDirectory)
|
||||||
else:
|
else:
|
||||||
Logger.debug("Copying %s to %s", source, target)
|
Logger.debug("Copying %s to %s", filePath, targetDirectory)
|
||||||
shutil.copy(source, target)
|
shutil.copy(filePath, targetDirectory)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue