mirror of
https://github.com/clinton-hall/nzbToMedia.git
synced 2025-08-20 21:33:13 -07:00
Fix Torrent handling
Fix logging move def removeEmptyFolders to top Change extension matching to use simple tools Change os.link
This commit is contained in:
parent
d74fa9216e
commit
c2b7a26921
1 changed files with 46 additions and 42 deletions
|
@ -2,13 +2,31 @@
|
||||||
|
|
||||||
import autoProcessMovie
|
import autoProcessMovie
|
||||||
import autoProcessTV
|
import autoProcessTV
|
||||||
import sys, os, ConfigParser, shutil, re
|
import sys, os, ConfigParser, shutil
|
||||||
from subprocess import call
|
from subprocess import call
|
||||||
|
|
||||||
|
def removeEmptyFolders(path):
|
||||||
|
if not os.path.isdir(path):
|
||||||
|
return
|
||||||
|
|
||||||
|
# remove empty subfolders
|
||||||
|
files = os.listdir(path)
|
||||||
|
if len(files):
|
||||||
|
for f in files:
|
||||||
|
fullpath = os.path.join(path, f)
|
||||||
|
if os.path.isdir(fullpath):
|
||||||
|
removeEmptyFolders(fullpath)
|
||||||
|
|
||||||
|
# if folder empty, delete it
|
||||||
|
files = os.listdir(path)
|
||||||
|
if len(files) == 0:
|
||||||
|
print "INFO: Removing empty folder: %s" % (path)
|
||||||
|
os.rmdir(path)
|
||||||
|
|
||||||
old_stdout = sys.stdout #backup the default stdout
|
old_stdout = sys.stdout #backup the default stdout
|
||||||
log_file = open(os.path.join(os.path.dirname(sys.argv[0]), "postprocess.log"),"a+")
|
log_file = open(os.path.join(os.path.dirname(sys.argv[0]), "postprocess.log"),"a+")
|
||||||
sys.stdout = log_file #create a local log file, and direct all "print" to the log.
|
sys.stdout = log_file #create a local log file, and direct all "print" to the log.
|
||||||
print "INFO: TorrentToMedia V4.1"
|
print "INFO: TorrentToMedia V4.2"
|
||||||
if len(sys.argv) == 4:
|
if len(sys.argv) == 4:
|
||||||
##You can use the following parameters (UTORRENT):
|
##You can use the following parameters (UTORRENT):
|
||||||
##
|
##
|
||||||
|
@ -46,7 +64,7 @@ if len(sys.argv) == 4:
|
||||||
Category = sys.argv[3] ## %L -- Example output: tvseries ## This is the label in uTorrent
|
Category = sys.argv[3] ## %L -- Example output: tvseries ## This is the label in uTorrent
|
||||||
|
|
||||||
elif len(sys.argv) > 1: #Doesn't match Transmission (1) or uTorrent (4).
|
elif len(sys.argv) > 1: #Doesn't match Transmission (1) or uTorrent (4).
|
||||||
print ("Error: The number of arguments passed is %s. Unable to determin the arguments to use; Exiting" , len(sys.argv))
|
print "Error: The number of arguments passed is %s. Unable to determin the arguments to use; Exiting" % (len(sys.argv))
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
@ -75,7 +93,7 @@ packed = 0
|
||||||
config = ConfigParser.ConfigParser()
|
config = ConfigParser.ConfigParser()
|
||||||
configFilename = os.path.join(os.path.dirname(sys.argv[0]), "autoProcessMedia.cfg")
|
configFilename = os.path.join(os.path.dirname(sys.argv[0]), "autoProcessMedia.cfg")
|
||||||
|
|
||||||
print ("INFO: Loading config from %s", configFilename)
|
print "INFO: Loading config from ", configFilename
|
||||||
|
|
||||||
if not os.path.isfile(configFilename):
|
if not os.path.isfile(configFilename):
|
||||||
print "ERROR: You need an autoProcessMedia.cfg file - did you rename and edit the .sample?"
|
print "ERROR: You need an autoProcessMedia.cfg file - did you rename and edit the .sample?"
|
||||||
|
@ -95,28 +113,33 @@ if Category == Movie_Cat:
|
||||||
elif Category == TV_Cat:
|
elif Category == TV_Cat:
|
||||||
destination = os.path.join(TV_dest, Name)
|
destination = os.path.join(TV_dest, Name)
|
||||||
else:
|
else:
|
||||||
print ("INFO: Not assigned a label of either %s or %s. Exiting", Movie_Cat, TV_Cat)
|
print "INFO: Not assigned a label of either %s or %s: Exiting" %(Movie_Cat, TV_Cat)
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
|
|
||||||
test = re.compile('^(.*)\.((zip|rar|7z|gz|bz|tar|arj)|(r[0-9]{1,3})|([0-9]{1,3}))$', re.IGNORECASE|re.UNICODE);
|
test = ['.zip', '.rar', '.7z', '.gz', '.bz', '.tar', '.arj']
|
||||||
test2 = re.compile('^(.*)\.(mkv|avi|divx|xvid|mov|wmv)$', re.IGNORECASE|re.UNICODE);
|
test2 = ['.mkv', '.avi', '.divx', '.xvid', '.mov', '.wmv']
|
||||||
if test.match(Directory):
|
f = [filenames for dirpath, dirnames, filenames in os.walk(Directory)]
|
||||||
|
ext = [os.path.splitext(file)[1] for file in f[1]]
|
||||||
|
if set(ext).intersection(set(test)):
|
||||||
print "INFO: Found compressed archives, extracting"
|
print "INFO: Found compressed archives, extracting"
|
||||||
packed = 1
|
packed = 1
|
||||||
## Check that files actully is .mkv / .avi etc, and not packed files or anything else
|
## Check that files actully is .mkv / .avi etc, and not packed files or anything else
|
||||||
elif test2.match(Directory):
|
elif set(ext).intersection(set(test2)):
|
||||||
print "INFO: Found media files, moving"
|
print "INFO: Found media files, moving"
|
||||||
else:
|
else:
|
||||||
print "INFO: 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"
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
|
|
||||||
if useLink == 0 and packed == 0: ## copy
|
if useLink == 0 and packed == 0: ## copy
|
||||||
print ("INFO: Copying all files from %s to %s", Directory, destination)
|
print "INFO: Copying all files from %s to %s." % (Directory, destination)
|
||||||
shutil.copytree(Directory, destination)
|
shutil.copytree(Directory, destination)
|
||||||
|
|
||||||
elif useLink == 1 and packed == 0: ## hardlink
|
elif useLink == 1 and packed == 0: ## hardlink
|
||||||
print ("INFO: Creating hard link from %s to %s", Directory, destination)
|
print "INFO: Creating hard link from %s to %s." % (Directory, destination)
|
||||||
shutil.copytree(Directory, destination, copy_function=os.link)
|
os.mkdir(destination)
|
||||||
|
for dirpath, dirnames, filenames in os.walk(Directory):
|
||||||
|
for file in filenames:
|
||||||
|
os.link(os.path.join(dirpath, file), os.path.join(destination, file))
|
||||||
|
|
||||||
elif packed == 1: ## unpack
|
elif packed == 1: ## unpack
|
||||||
## Using Windows?
|
## Using Windows?
|
||||||
|
@ -159,7 +182,7 @@ elif packed == 1: ## unpack
|
||||||
if ext[1] in EXTRACT_COMMANDS:
|
if ext[1] in EXTRACT_COMMANDS:
|
||||||
cmd = EXTRACT_COMMANDS[ext[1]]
|
cmd = EXTRACT_COMMANDS[ext[1]]
|
||||||
else:
|
else:
|
||||||
print("ERROR: Unknown file type: %s", ext[1])
|
print "ERROR: Unknown file type: %s" % (ext[1])
|
||||||
continue
|
continue
|
||||||
|
|
||||||
## Create destination folder
|
## Create destination folder
|
||||||
|
@ -167,14 +190,13 @@ elif packed == 1: ## unpack
|
||||||
try:
|
try:
|
||||||
os.makedirs(destination)
|
os.makedirs(destination)
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
print("ERROR: Not possible to create destination folder: %s", e)
|
print "ERROR: Not possible to create destination folder: %s" % (e)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
print("INFO: Extracting to %s", destination)
|
print"INFO: Extracting to %s" % (destination)
|
||||||
|
|
||||||
|
|
||||||
## Running..
|
## Running..
|
||||||
print("INFO: Extracting %s %s %s %s", cmd[0], cmd[1], fp, destination)
|
print "INFO: Extracting %s %s %s %s" % (cmd[0], cmd[1], fp, destination)
|
||||||
pwd = os.getcwd() # Get our Present Working Directory
|
pwd = os.getcwd() # Get our Present Working Directory
|
||||||
os.chdir(destination) #not all unpack commands accept full paths, so just extract into this directory.
|
os.chdir(destination) #not all unpack commands accept full paths, so just extract into this directory.
|
||||||
if os.name == 'nt': #Windows needs quotes around directory structure
|
if os.name == 'nt': #Windows needs quotes around directory structure
|
||||||
|
@ -183,11 +205,11 @@ elif packed == 1: ## unpack
|
||||||
res = call(run)
|
res = call(run)
|
||||||
if res == 0:
|
if res == 0:
|
||||||
status = 0
|
status = 0
|
||||||
print ("INFO: Extraction was successful for %s to %s", fp, destination)
|
print "INFO: Extraction was successful for %s to %s" % (fp, destination)
|
||||||
else:
|
else:
|
||||||
print("ERROR: Extraction failed for %s. 7zip result was %s", fp, res)
|
print "ERROR: Extraction failed for %s. 7zip result was %s" % (fp, res)
|
||||||
except:
|
except:
|
||||||
print ("ERROR: Extraction failed for %s. Could not call command %s %s", fp, run)
|
print "ERROR: Extraction failed for %s. Could not call command %s %s" % (fp, run)
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
if cmd[1] == "": #if calling unzip, we dont want to pass the ""
|
if cmd[1] == "": #if calling unzip, we dont want to pass the ""
|
||||||
|
@ -196,11 +218,11 @@ elif packed == 1: ## unpack
|
||||||
res = call([cmd[0], cmd[1], fp])
|
res = call([cmd[0], cmd[1], fp])
|
||||||
if res == 0:
|
if res == 0:
|
||||||
status = 0
|
status = 0
|
||||||
print ("INFO: Extraction was successful for %s to %s", fp, destination)
|
print "INFO: Extraction was successful for %s to %s" % (fp, destination)
|
||||||
else:
|
else:
|
||||||
print("ERROR: Extraction failed for %s. 7zip result was %s", fp, res)
|
print "ERROR: Extraction failed for %s. 7zip result was %s" % (fp, res)
|
||||||
except:
|
except:
|
||||||
print ("ERROR: Extraction failed for %s. Could not call command %s %s %s %s", fp, cmd[0], cmd[1], fp)
|
print "ERROR: Extraction failed for %s. Could not call command %s %s %s %s" % (fp, cmd[0], cmd[1], fp)
|
||||||
os.chdir(pwd) # Go back to our Original Working Directory
|
os.chdir(pwd) # Go back to our Original Working Directory
|
||||||
|
|
||||||
for dirpath, dirnames, filenames in os.walk(destination): #flatten out the directory to make postprocessing easier.
|
for dirpath, dirnames, filenames in os.walk(destination): #flatten out the directory to make postprocessing easier.
|
||||||
|
@ -210,7 +232,7 @@ for dirpath, dirnames, filenames in os.walk(destination): #flatten out the direc
|
||||||
try:
|
try:
|
||||||
shutil.move(os.path.join(dirpath, filename), destination)
|
shutil.move(os.path.join(dirpath, filename), destination)
|
||||||
except OSError:
|
except OSError:
|
||||||
print ("INFO: Could not flatten %s ", os.path.join(dirpath, filename))
|
print "INFO: Could not flatten %s " % (os.path.join(dirpath, filename))
|
||||||
removeEmptyFolders(destination) #cleanup empty directories.
|
removeEmptyFolders(destination) #cleanup empty directories.
|
||||||
|
|
||||||
status = int(status)
|
status = int(status)
|
||||||
|
@ -222,21 +244,3 @@ elif Category == TV_Cat:
|
||||||
|
|
||||||
sys.stdout = old_stdout #reset our stdout
|
sys.stdout = old_stdout #reset our stdout
|
||||||
log_file.close() #close the log
|
log_file.close() #close the log
|
||||||
|
|
||||||
def removeEmptyFolders(path):
|
|
||||||
if not os.path.isdir(path):
|
|
||||||
return
|
|
||||||
|
|
||||||
# remove empty subfolders
|
|
||||||
files = os.listdir(path)
|
|
||||||
if len(files):
|
|
||||||
for f in files:
|
|
||||||
fullpath = os.path.join(path, f)
|
|
||||||
if os.path.isdir(fullpath):
|
|
||||||
removeEmptyFolders(fullpath)
|
|
||||||
|
|
||||||
# if folder empty, delete it
|
|
||||||
files = os.listdir(path)
|
|
||||||
if len(files) == 0:
|
|
||||||
print ("INFO: Removing empty folder: %s", path)
|
|
||||||
os.rmdir(path)
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue