# This script was taken almost entirely from Manders2600 Script with the use of the awesome ComicTagger. # modified very slightly so Mylar just passes it the IssueID for it to do it's magic. import os, errno import sys import re import glob import platform import shutil import time import zipfile import subprocess from subprocess import CalledProcessError, check_output import mylar from mylar import logger from mylar.helpers import cvapi_check def run (dirName, nzbName=None, issueid=None, manual=None, filename=None): logger.fdebug('[META-TAGGING] dirName:' + dirName) ## Set the directory in which comictagger and other external commands are located - IMPORTANT - ## # ( User may have to modify, depending on their setup, but these are some guesses for now ) if platform.system() == "Windows": #if it's a source install. if os.path.isdir(os.path.join(mylar.CMTAGGER_PATH, '.git')): comictagger_cmd = os.path.join(mylar.CMTAGGER_PATH, 'comictagger.py') else: #regardless of 32/64 bit install if 'comictagger.exe' in mylar.CMTAGGER_PATH: comictagger_cmd = mylar.CMTAGGER_PATH else: comictagger_cmd = os.path.join(mylar.CMTAGGER_PATH, 'comictagger.exe') unrar_cmd = "C:\Program Files\WinRAR\UnRAR.exe" # test for UnRAR if not os.path.isfile(unrar_cmd): unrar_cmd = "C:\Program Files (x86)\WinRAR\UnRAR.exe" if not os.path.isfile(unrar_cmd): logger.fdebug('[META-TAGGING] Unable to locate UnRAR.exe - make sure it is installed.') logger.fdebug('[META-TAGGING] Aborting meta-tagging.') return "fail" elif platform.system() == "Darwin": #Mac OS X comictagger_cmd = os.path.join(mylar.CMTAGGER_PATH, 'comictagger.py') unrar_cmd = "/usr/local/bin/unrar" else: #for the 'nix #check for dependencies here - configparser try: import configparser except ImportError: logger.fdebug('[META-TAGGING] configparser not found on system. Please install manually in order to write metadata') logger.fdebug('[META-TAGGING] continuing with PostProcessing, but I am not using metadata.') return "fail" #set this to the lib path (ie. '/lib') comictagger_cmd = os.path.join(mylar.CMTAGGER_PATH, 'comictagger.py') unrar_cmd = "/usr/bin/unrar" # if not os.path.exists( comictagger_cmd ): # print "ERROR: can't find the ComicTagger program: {0}".format( comictagger_cmd ) # print " You probably need to edit this script!" # sys.exit( 1 ) file_conversion = True file_extension_fixing = True if not os.path.exists( unrar_cmd ): logger.fdebug('[META-TAGGING] WARNING: cannot find the unrar command.') logger.fdebug('[META-TAGGING] File conversion and extension fixing not available') logger.fdebug('[META-TAGGING] You probably need to edit this script, or install the missing tool, or both!') file_conversion = False file_extension_fixing = False ## Sets up other directories ## scriptname = os.path.basename( sys.argv[0] ) downloadpath = os.path.abspath( dirName ) sabnzbdscriptpath = os.path.dirname( sys.argv[0] ) if manual is None: comicpath = os.path.join( downloadpath , "temp" ) else: comicpath = os.path.join( downloadpath, issueid ) unrar_folder = os.path.join( comicpath , "unrard" ) logger.fdebug('[META-TAGGING] ---directory settings.') logger.fdebug('[META-TAGGING] scriptname : ' + scriptname) logger.fdebug('[META-TAGGING] downloadpath : ' + downloadpath) logger.fdebug('[META-TAGGING] sabnzbdscriptpath : ' + sabnzbdscriptpath) logger.fdebug('[META-TAGGING] comicpath : ' + comicpath) logger.fdebug('[META-TAGGING] unrar_folder : ' + unrar_folder) logger.fdebug('[META-TAGGING] Running the Post-SabNZBd/Mylar script') if os.path.exists( comicpath ): shutil.rmtree( comicpath ) logger.fdebug('[META-TAGGING] Attempting to create directory @: ' + str(comicpath)) try: os.makedirs(comicpath) except OSError: raise logger.fdebug('[META-TAGGING] Created directory @ : ' + str(comicpath)) logger.fdebug('[META-TAGGING] Filename is : ' + str(filename)) if filename is None: filename_list = glob.glob( os.path.join( downloadpath, "*.cbz" ) ) filename_list.extend( glob.glob( os.path.join( downloadpath, "*.cbr" ) ) ) fcount = 1 for f in filename_list: if fcount > 1: logger.fdebug('[META-TAGGING] More than one cbr/cbz within path, performing Post-Process on first file detected: ' + f) break shutil.move( f, comicpath ) filename = f #just the filename itself fcount+=1 else: # if the filename is identical to the parent folder, the entire subfolder gets copied since it's the first match, instead of just the file shutil.move( filename, comicpath ) filename = os.path.split(filename)[1] # just the filename itself #print comicpath #print os.path.join( comicpath, filename ) if filename.endswith('.cbr'): f = os.path.join( comicpath, filename ) if zipfile.is_zipfile( f ): logger.fdebug('[META-TAGGING] zipfile detected') base = os.path.splitext( f )[0] shutil.move( f, base + ".cbz" ) logger.fdebug('[META-TAGGING] {0}: renaming {1} to be a cbz'.format( scriptname, os.path.basename( f ) )) if file_extension_fixing: if filename.endswith('.cbz'): f = os.path.join( comicpath, filename ) if os.path.isfile( f ): try: rar_test_cmd_output = "is not RAR archive" #default, in case of error rar_test_cmd_output = subprocess.check_output( [ unrar_cmd, "t", f ] ) except: pass if not "is not RAR archive" in rar_test_cmd_output: base = os.path.splitext( f )[0] shutil.move( f, base + ".cbr" ) logger.fdebug('[META-TAGGING] {0}: renaming {1} to be a cbr'.format( scriptname, os.path.basename( f ) )) # Now rename all CBR files to RAR if filename.endswith('.cbr'): #logger.fdebug('renaming .cbr to .rar') f = os.path.join( comicpath, filename) base = os.path.splitext( f )[0] baserar = base + ".rar" shutil.move( f, baserar ) ## Changes any cbr files to cbz files for insertion of metadata ## if file_conversion: f = os.path.join( comicpath, filename ) logger.fdebug('[META-TAGGING] {0}: converting {1} to be zip format'.format( scriptname, os.path.basename( f ) )) basename = os.path.splitext( f )[0] zipname = basename + ".cbz" # Move into the folder where we will be unrar-ing things os.makedirs( unrar_folder ) os.chdir( unrar_folder ) # Extract and zip up logger.fdebug('[META-TAGGING] {0}: Comicpath is ' + baserar) #os.path.join(comicpath,basename)) logger.fdebug('[META-TAGGING] {0}: Unrar is ' + unrar_folder ) try: #subprocess.Popen( [ unrar_cmd, "x", os.path.join(comicpath,basename) ] ).communicate() output = subprocess.check_output( [ unrar_cmd, 'x', baserar ] ) #os.path.join(comicpath,basename) ] ) except CalledProcessError as e: if e.returncode == 3: logger.fdebug('[META-TAGGING] [Unrar Error 3] - Broken Archive.') elif e.returncode == 1: logger.fdebug('[META-TAGGING] [Unrar Error 1] - No files to extract.') logger.fdebug('[META-TAGGING] Marking this as an incomplete download.') return "unrar error" shutil.make_archive( basename, "zip", unrar_folder ) # get out of unrar folder and clean up os.chdir( comicpath ) shutil.rmtree( unrar_folder ) ## Changes zip to cbz f = os.path.join( comicpath, os.path.splitext(filename)[0] + ".zip" ) #print "zipfile" + f try: with open(f): pass except: logger.warn('[META-TAGGING] No zip file present') return "fail" base = os.path.splitext( f )[0] shutil.move( f, base + ".cbz" ) nfilename = base + ".cbz" else: logger.fdebug('[META-TAGGING] Filename:' + filename) nfilename = filename if os.path.isfile( nfilename ): logger.fdebug('[META-TAGGING] File exists in given location already.') file_dir, file_n = os.path.split(nfilename) else: #remove the IssueID from the path file_dir = re.sub(issueid, '', comicpath) file_n = os.path.split(nfilename)[1] logger.fdebug('[META-TAGGING] Converted directory: ' + str(file_dir)) logger.fdebug('[META-TAGGING] Converted filename: ' + str(file_n)) logger.fdebug('[META-TAGGING] Destination path: ' + os.path.join(dirName,file_n)) logger.fdebug('[META-TAGGING] dirName: ' + dirName) logger.fdebug('[META-TAGGING] absDirName: ' + os.path.abspath(dirName)) ## check comictagger version - less than 1.15.beta - take your chances. ctversion = subprocess.check_output( [ comictagger_cmd, "--version" ] ) ctend = ctversion.find(':') ctcheck = re.sub("[^0-9]", "", ctversion[:ctend]) ctcheck = re.sub('\.', '', ctcheck).strip() if int(ctcheck) >= int('1115'): #(v1.1.15) if mylar.COMICVINE_API == mylar.DEFAULT_CVAPI: logger.fdebug('[META-TAGGING] ' + ctversion[:ctend] + ' being used - no personal ComicVine API Key supplied. Take your chances.') use_cvapi = "False" else: logger.fdebug('[META-TAGGING] ' + ctversion[:ctend] + ' being used - using personal ComicVine API key supplied via mylar.') use_cvapi = "True" else: logger.fdebug('[META-TAGGING] ' + ctversion[:ctend] + ' being used - personal ComicVine API key not supported in this version. Good luck.') use_cvapi = "False" i = 1 tagcnt = 0 if mylar.CT_TAG_CR: tagcnt = 1 logger.info('[META-TAGGING] CR Tagging enabled.') if mylar.CT_TAG_CBL: if not mylar.CT_TAG_CR: i = 2 #set the tag to start at cbl and end without doing another tagging. tagcnt = 2 logger.info('[META-TAGGING] CBL Tagging enabled.') if tagcnt == 0: logger.warn('[META-TAGGING] You have metatagging enabled, but you have not selected the type(s) of metadata to write. Please fix and re-run manually') return "fail" while ( i <= tagcnt ): if i == 1: tagtype = "cr" # CR meta-tagging cycle. tagdisp = 'ComicRack tagging' elif i == 2: tagtype = "cbl" #Cbl meta-tagging cycle tagdisp = 'Comicbooklover tagging' logger.info('[META-TAGGING] ' + tagdisp + ' meta-tagging processing started.') #CV API Check here. if mylar.CVAPI_COUNT == 0 or mylar.CVAPI_COUNT >= 200: cvapi_check() ## Tag each CBZ, and move it back to original directory ## if use_cvapi == "True": if issueid is None: subprocess.Popen( [ comictagger_cmd, "-s", "-t", tagtype, "--cv-api-key", mylar.COMICVINE_API, "-f", "-o", "--verbose", "--nooverwrite", nfilename ] ).communicate() else: subprocess.Popen( [ comictagger_cmd, "-s", "-t", tagtype, "--cv-api-key", mylar.COMICVINE_API, "-o", "--id", issueid, "--verbose", "--nooverwrite", nfilename ] ).communicate() logger.info('[META-TAGGING] ' + tagtype + ' meta-tagging complete') #increment CV API counter. mylar.CVAPI_COUNT +=1 else: if issueid is None: subprocess.Popen( [ comictagger_cmd, "-s", "-t", tagtype, "-f", "-o", "--verbose", "--nooverwrite", nfilename ] ).communicate() else: subprocess.Popen( [ comictagger_cmd, "-s", "-t", tagtype, "-o", "--id", issueid, "--verbose", "--nooverwrite", nfilename ] ).communicate() #increment CV API counter. mylar.CVAPI_COUNT +=1 i+=1 if os.path.exists(os.path.join(os.path.abspath(dirName),file_n)): logger.fdebug('[META-TAGGING] Unable to move - file already exists.') else: shutil.move( os.path.join(comicpath, nfilename), os.path.join(os.path.abspath(dirName),file_n)) #shutil.move( nfilename, os.path.join(os.path.abspath(dirName),file_n)) logger.fdebug('[META-TAGGING] Sucessfully moved file from temporary path.') i = 0 os.chdir( mylar.PROG_DIR ) while i < 10: try: logger.fdebug('[META-TAGGING] Attempting to remove: ' + comicpath) shutil.rmtree( comicpath ) except: time.sleep(.1) else: return os.path.join(os.path.abspath(dirName), file_n) i+=1 logger.fdebug('[META-TAGGING] Failed to remove temporary path : ' + str(comicpath)) return os.path.join(os.path.abspath(dirName),file_n)