def renameFiles(self, movie): ''' rename files based on movie data & conf ''' multiple = False if len(movie['files']) > 1: multiple = True destination = self.conf('destination') folderNaming = self.conf('foldernaming') fileNaming = self.conf('filenaming') # Remove weird chars from moviename moviename = re.sub(r"[\x00\/\\:\*\?\"<>\|]", '', movie['info'].get('name')) # Put 'The' at the end namethe = moviename if moviename[:4].lower() == 'the ': namethe = moviename[4:] + ', The' replacements = { 'cd': '', 'cdNr': '', 'ext': 'mkv', 'namethe': namethe.strip(), 'thename': moviename.strip(), 'year': movie['info']['year'], 'first': namethe[0].upper(), 'quality': movie['info']['quality'], 'video': movie['info']['codec']['video'], 'audio': movie['info']['codec']['audio'], 'group': movie['info']['group'], 'resolution': movie['info']['resolution'], 'sourcemedia': movie['info']['sourcemedia'] } if multiple: cd = 1 justAdded = [] finalDestination = None #clean up post-processing script ppScriptName = movie['info'].get('ppScriptName') ppDirName = self.config.get('Sabnzbd', 'ppDir') if ppScriptName: if ppDirName: ppPath = os.path.join(ppDirName, ppScriptName) try: os.remove(ppPath) log.info("Removed post-processing script: %s" % ppPath) except: log.info("Couldn't remove post-processing script: %s" % ppPath) else: log.info("Don't know where the post processing script is located, not removing %s" % ppScriptName) filenames = [] for file in movie['files']: log.info('Trying to find a home for: %s' % latinToAscii(file['filename'])) replacements['ext'] = file['ext'] if multiple: replacements['cd'] = ' cd' + str(cd) replacements['cdNr'] = ' ' + str(cd) replacements['original'] = file['filename'] folder = self.doReplace(folderNaming, replacements) filename = self.doReplace(fileNaming, replacements) filenames.append(filename) old = os.path.join(movie['path'], file['filename']) dest = os.path.join(destination, folder, filename) finalDestination = os.path.dirname(dest) if not os.path.isdir(finalDestination): try: log.info('Creating directory %s' % finalDestination) os.makedirs(finalDestination) shutil.copymode(destination, finalDestination) except OSError: log.error('Failed changing permissions %s' % finalDestination) # Remove old if better quality removed = self.removeOld(os.path.join(destination, folder), justAdded, movie['info']['size']) if not os.path.isfile(dest) and removed: log.info('Moving file "%s" to %s.' % (latinToAscii(old), dest)) if not _move(old, dest): break justAdded.append(dest) else: try: log.error('File %s already exists or not better.' % latinToAscii(filename)) path = movie['path'].split(os.sep) path.extend(['_EXISTS_' + path.pop()]) old = movie['path'] dest = os.sep.join(path) _move(old, dest) except: log.error('Could not extend path name.') break #get subtitle if any & move for type in movie['subtitles']: if len(movie['subtitles'][type]) > 0: log.info('Moving matching subtitle.') subtitle = movie['subtitles'][type].pop(0) replacements['original'] = subtitle['filename'] replacements['ext'] = subtitle['ext'] subDest = os.path.join(destination, folder, self.doReplace(fileNaming, replacements)) old = os.path.join(movie['path'], subtitle['filename']) if not _move(old, subDest): break justAdded.append(subDest) # Add to ignore list when removing stuff. # Add to renaming history log.debug('renamehistory start') h = RenameHistory() Db.add(h) try: h.movieQueue = movie['queue'].id except: h.movieQueue = 0 h.old = unicode(old.decode('utf-8')) h.new = unicode(dest.decode('utf-8')) Db.flush() log.debug('renamehistory end') if multiple: cd += 1 # Mark movie downloaded log.debug('queue downloaded start') if movie['queue'] and movie['queue'].id > 0: if movie['queue'].markComplete: movie['movie'].status = u'downloaded' movie['queue'].completed = True Db.flush() log.debug('queue downloaded end') return { 'directory': finalDestination, 'filenames': filenames }
def renameFiles(self, movie): """ rename files based on movie data & conf """ multiple = False if len(movie["files"]) > 1: multiple = True destination = self.conf("destination") folderNaming = self.conf("foldernaming") fileNaming = self.conf("filenaming") # Remove weird chars from moviename moviename = re.sub(r"[\x00\/\\:\*\?\"<>\|]", "", movie["info"].get("name")) # Put 'The' at the end namethe = moviename if moviename[:4].lower() == "the ": namethe = moviename[4:] + ", The" replacements = { "cd": "", "cdNr": "", "ext": ".mkv", "namethe": namethe.strip(), "thename": moviename.strip(), "year": movie["info"]["year"], "first": namethe[0].upper(), "quality": movie["info"]["quality"], "video": movie["info"]["codec"]["video"], "audio": movie["info"]["codec"]["audio"], "group": movie["info"]["group"], "resolution": movie["info"]["resolution"], "sourcemedia": movie["info"]["sourcemedia"], } if multiple: cd = 1 justAdded = [] finalDestination = None finalFilename = self.doReplace(fileNaming, replacements) for file in movie["files"]: log.info("Trying to find a home for: %s" % latinToAscii(file["filename"])) replacements["ext"] = file["ext"] if multiple: replacements["cd"] = " cd" + str(cd) replacements["cdNr"] = " " + str(cd) replacements["original"] = file["filename"] folder = self.doReplace(folderNaming, replacements) filename = self.doReplace(fileNaming, replacements) old = os.path.join(movie["path"], file["filename"]) dest = os.path.join(destination, folder, filename) finalDestination = os.path.dirname(dest) if not os.path.isdir(finalDestination): try: log.info("Creating directory %s" % finalDestination) os.makedirs(finalDestination) shutil.copymode(destination, finalDestination) except OSError: log.error("Failed changing permissions %s" % finalDestination) # Remove old if better quality removed = self.removeOld(os.path.join(destination, folder), justAdded, movie["info"]["size"]) if not os.path.isfile(dest) and removed: log.info('Moving file "%s" to %s.' % (latinToAscii(old), dest)) shutil.move(old, dest) justAdded.append(dest) else: try: path = file["path"].split(os.sep) path.extend(["_EXISTS_" + path.pop()]) shutil.move(file["path"], os.sep.join(path)) except IOError: pass log.error("File %s already exists or not better." % latinToAscii(filename)) break # get subtitle if any & move for type in movie["subtitles"]: if len(movie["subtitles"][type]) > 0: log.info("Moving matching subtitle.") subtitle = movie["subtitles"][type].pop(0) replacements["ext"] = subtitle["ext"] subDest = os.path.join(destination, folder, self.doReplace(fileNaming, replacements)) shutil.move(os.path.join(movie["path"], subtitle["filename"]), subDest) justAdded.append(subDest) # Add to ignore list when removing stuff. # Add to renaming history h = RenameHistory() Db.add(h) h.movieQueue = movie["queue"].id if movie["queue"] else 0 h.old = unicode(old.decode("utf-8")) h.new = unicode(dest.decode("utf-8")) Db.flush() if multiple: cd += 1 # Mark movie downloaded if movie["queue"] and movie["queue"].id > 0: if movie["queue"].markComplete: movie["movie"].status = u"downloaded" movie["queue"].completed = True Db.flush() return {"directory": finalDestination, "filename": finalFilename}
def renameFiles(self, files, movie, queue = None): ''' rename files based on movie data & conf ''' multiple = False if len(files['files']) > 1: multiple = True destination = self.conf('destination') folderNaming = self.conf('foldernaming') fileNaming = self.conf('filenaming') # Remove weird chars from moviename moviename = re.sub(r"[\x00\/\\:\*\?\"<>\|]", '', movie.name) # Put 'The' at the end namethe = moviename if moviename[:3].lower() == 'the': namethe = moviename[3:] + ', The' #quality if not queue: queue = self.getQueue(movie) if not queue: quality = Qualities().guess(files['files']) queueId = 0 else: quality = Qualities.types[queue.qualityType]['label'] queueId = queue.id replacements = { 'cd': '', 'cdNr': '', 'ext': '.mkv', 'namethe': namethe.strip(), 'thename': moviename.strip(), 'year': movie.year, 'first': namethe[0].upper(), 'quality': quality, } if multiple: cd = 1 justAdded = [] totalSize = 0 for file in files['files']: fullPath = os.path.join(file['path'], file['filename']) totalSize += os.path.getsize(fullPath) # Do something with ISO, as they should be between DVDRip and BRRIP ext = os.path.splitext(file['filename'])[1].lower()[1:] if ext == 'iso': totalSize -= (os.path.getsize(fullPath) / 1.6) log.info('Total size of new files is %s.' % int(totalSize / 1024 / 1024)) finalDestination = None finalFilename = self.doReplace(fileNaming, replacements) for file in sorted(files['files']): log.info('Trying to find a home for: %s' % latinToAscii(file['filename'])) replacements['ext'] = file['ext'] if multiple: replacements['cd'] = ' cd' + str(cd) replacements['cdNr'] = ' ' + str(cd) replacements['original'] = file['root'] replacements['video'] = self.getCodec(file['filename'], RenamerCron.videoCodecs) replacements['audio'] = self.getCodec(file['filename'], RenamerCron.audioCodecs) replacements['group'] = self.getGroup(file['root']) folder = self.doReplace(folderNaming, replacements) filename = self.doReplace(fileNaming, replacements) old = os.path.join(file['path'], file['filename']) dest = os.path.join(destination, folder, filename) finalDestination = os.path.dirname(dest) if not os.path.isdir(finalDestination): # Use same permissions as conf('destination') folder try: #mode = os.stat(destination) #chmod = mode[ST_MODE] & 07777 log.info('Creating directory %s' % finalDestination) os.makedirs(finalDestination) shutil.copymode(destination, finalDestination) #os.chmod(finalDestination, chmod) except OSError: log.error('Failed setting permissions for %s' % finalDestination) os.makedirs(finalDestination) # Remove old if better quality removed = self.removeOld(os.path.join(destination, folder), justAdded, totalSize) if not os.path.isfile(dest) and removed: log.info('Moving file "%s" to %s.' % (latinToAscii(old), dest)) shutil.move(old, dest) justAdded.append(dest) else: try: path = file['path'].split(os.sep) path.extend(['_EXISTS_' + path.pop()]) shutil.move(file['path'], os.sep.join(path)) except IOError: pass log.error('File %s already exists or not better.' % latinToAscii(filename)) break #get subtitle if any & move if len(files['subtitles']) > 0: log.info('Moving matching subtitle.') subtitle = files['subtitles'].pop(0) replacements['ext'] = subtitle['ext'] subFilename = self.doReplace(fileNaming, replacements) shutil.move(os.path.join(subtitle['path'], subtitle['filename']), os.path.join(destination, folder, subFilename)) # Add to renaming history h = RenameHistory() h.movieId = movie.id h.movieQueue = queueId h.old = unicode(old.decode('utf-8')) h.new = unicode(dest.decode('utf-8')) Db.add(h) Db.flush() if multiple: cd += 1 # Mark movie downloaded if queueId > 0: if queue.markComplete: movie.status = u'downloaded' queue.completed = True Db.flush() return { 'directory': finalDestination, 'filename': finalFilename }
def renameFiles(self, movie): ''' rename files based on movie data & conf ''' multiple = False if len(movie['files']) > 1: multiple = True destination = self.conf('destination') folderNaming = self.conf('foldernaming') fileNaming = self.conf('filenaming') # Remove weird chars from moviename moviename = re.sub(r"[\x00\/\\:\*\?\"<>\|]", '', movie['info'].get('name')) # Put 'The' at the end namethe = moviename if moviename[:4].lower() == 'the ': namethe = moviename[4:] + ', The' replacements = { 'cd': '', 'cdNr': '', 'ext': 'mkv', 'namethe': namethe.strip(), 'thename': moviename.strip(), 'year': movie['info']['year'], 'first': namethe[0].upper(), 'quality': movie['info']['quality'], 'video': movie['info']['codec']['video'], 'audio': movie['info']['codec']['audio'], 'group': movie['info']['group'], 'resolution': movie['info']['resolution'], 'sourcemedia': movie['info']['sourcemedia'] } if multiple: cd = 1 justAdded = [] finalDestination = None #clean up post-processing script ppScriptName = movie['info'].get('ppScriptName') ppDirName = self.config.get('Sabnzbd', 'ppDir') if ppScriptName: if ppDirName: ppPath = os.path.join(ppDirName, ppScriptName) try: os.remove(ppPath) log.info("Removed post-processing script: %s" % ppPath) except: log.info("Couldn't remove post-processing script: %s" % ppPath) else: log.info("Don't know where the post processing script is located, not removing %s" % ppScriptName) filenames = [] for file in movie['files']: log.info('Trying to find a home for: %s' % latinToAscii(file['filename'])) replacements['ext'] = file['ext'] if multiple: replacements['cd'] = ' cd' + str(cd) replacements['cdNr'] = ' ' + str(cd) replacements['original'] = file['filename'] folder = self.doReplace(folderNaming, replacements) filename = self.doReplace(fileNaming, replacements) filenames.append(filename) old = os.path.join(movie['path'], file['filename']) dest = os.path.join(destination, folder, filename) finalDestination = os.path.dirname(dest) if not os.path.isdir(finalDestination): try: log.info('Creating directory %s' % finalDestination) os.makedirs(finalDestination) shutil.copymode(destination, finalDestination) except OSError: log.error('Failed changing permissions %s' % finalDestination) # Remove old if better quality removed = self.removeOld(os.path.join(destination, folder), justAdded, movie['info']['size']) if not os.path.isfile(dest) and removed: log.info('Moving file "%s" to %s.' % (latinToAscii(old), dest)) if not _move(old, dest): break justAdded.append(dest) else: try: log.error('File %s already exists or not better.' % latinToAscii(filename)) path = file['path'].split(os.sep) path.extend(['_EXISTS_' + path.pop()]) old = file['path'] dest = os.sep.join(path) _move(old, dest) except: log.error('Could not extend path name.') break #get subtitle if any & move for type in movie['subtitles']: if len(movie['subtitles'][type]) > 0: log.info('Moving matching subtitle.') subtitle = movie['subtitles'][type].pop(0) replacements['original'] = subtitle['filename'] replacements['ext'] = subtitle['ext'] subDest = os.path.join(destination, folder, self.doReplace(fileNaming, replacements)) old = os.path.join(movie['path'], subtitle['filename']) if not _move(old, subDest): break justAdded.append(subDest) # Add to ignore list when removing stuff. # Add to renaming history log.debug('renamehistory start') h = RenameHistory() Db.add(h) try: h.movieQueue = movie['queue'].id except: h.movieQueue = 0 h.old = unicode(old.decode('utf-8')) h.new = unicode(dest.decode('utf-8')) Db.flush() log.debug('renamehistory end') if multiple: cd += 1 # Mark movie downloaded log.debug('queue downloaded start') if movie['queue'] and movie['queue'].id > 0: if movie['queue'].markComplete: movie['movie'].status = u'downloaded' movie['queue'].completed = True Db.flush() log.debug('queue downloaded end') return { 'directory': finalDestination, 'filenames': filenames }
def renameFiles(self, movie): """ rename files based on movie data & conf """ multiple = False if len(movie["files"]) > 1: multiple = True destination = self.conf("destination") folderNaming = self.conf("foldernaming") fileNaming = self.conf("filenaming") # Remove weird chars from moviename moviename = re.sub(r"[\x00\/\\:\*\?\"<>\|]", "", movie["info"].get("name")) # Put 'The' at the end namethe = moviename if moviename[:4].lower() == "the ": namethe = moviename[4:] + ", The" replacements = { "cd": "", "cdNr": "", "ext": "mkv", "namethe": namethe.strip(), "thename": moviename.strip(), "year": movie["info"]["year"], "first": namethe[0].upper(), "quality": movie["info"]["quality"], "video": movie["info"]["codec"]["video"], "audio": movie["info"]["codec"]["audio"], "group": movie["info"]["group"], "resolution": movie["info"]["resolution"], "sourcemedia": movie["info"]["sourcemedia"], } if multiple: cd = 1 justAdded = [] finalDestination = None # clean up post-processing script ppScriptName = movie["info"].get("ppScriptName") ppDirName = self.config.get("Sabnzbd", "ppDir") if ppScriptName: if ppDirName: ppPath = os.path.join(ppDirName, ppScriptName) try: os.remove(ppPath) log.info("Removed post-processing script: %s" % ppPath) except: log.info("Couldn't remove post-processing script: %s" % ppPath) else: log.info("Don't know where the post processing script is located, not removing %s" % ppScriptName) filenames = [] for file in movie["files"]: log.info("Trying to find a home for: %s" % latinToAscii(file["filename"])) replacements["ext"] = file["ext"] if multiple: replacements["cd"] = " cd" + str(cd) replacements["cdNr"] = " " + str(cd) replacements["original"] = file["filename"] folder = self.doReplace(folderNaming, replacements) filename = self.doReplace(fileNaming, replacements) filenames.append(filename) old = os.path.join(movie["path"], file["filename"]) dest = os.path.join(destination, folder, filename) finalDestination = os.path.dirname(dest) if not os.path.isdir(finalDestination): try: log.info("Creating directory %s" % finalDestination) os.makedirs(finalDestination) shutil.copymode(destination, finalDestination) except OSError: log.error("Failed changing permissions %s" % finalDestination) # Remove old if better quality removed = self.removeOld(os.path.join(destination, folder), justAdded, movie["info"]["size"]) if not os.path.isfile(dest) and removed: log.info('Moving file "%s" to %s.' % (latinToAscii(old), dest)) if not _move(old, dest): break justAdded.append(dest) else: log.error("File %s already exists or not better." % latinToAscii(filename)) path = file["path"].split(os.sep) path.extend(["_EXISTS_" + path.pop()]) old = file["path"] dest = os.sep.join(path) _move(old, dest) # Break in any case, why did you do that Ruud? break # get subtitle if any & move for type in movie["subtitles"]: if len(movie["subtitles"][type]) > 0: log.info("Moving matching subtitle.") subtitle = movie["subtitles"][type].pop(0) replacements["original"] = subtitle["filename"] replacements["ext"] = subtitle["ext"] subDest = os.path.join(destination, folder, self.doReplace(fileNaming, replacements)) old = os.path.join(movie["path"], subtitle["filename"]) if not _move(old, subDest): break justAdded.append(subDest) # Add to ignore list when removing stuff. # Add to renaming history log.debug("renamehistory start") h = RenameHistory() Db.add(h) try: h.movieQueue = movie["queue"].id except: h.movieQueue = 0 h.old = unicode(old.decode("utf-8")) h.new = unicode(dest.decode("utf-8")) Db.flush() log.debug("renamehistory end") if multiple: cd += 1 # Mark movie downloaded log.debug("queue downloaded start") if movie["queue"] and movie["queue"].id > 0: if movie["queue"].markComplete: movie["movie"].status = u"downloaded" movie["queue"].completed = True Db.flush() log.debug("queue downloaded end") return {"directory": finalDestination, "filenames": filenames}
def renameFiles(self, movie): ''' rename files based on movie data & conf ''' multiple = False if len(movie['files']) > 1: multiple = True destination = self.conf('destination') folderNaming = self.conf('foldernaming') fileNaming = self.conf('filenaming') # Remove weird chars from moviename moviename = re.sub(r"[\x00\/\\:\*\?\"<>\|]", '', movie['info'].get('name')) # Put 'The' at the end namethe = moviename if moviename[:3].lower() == 'the': namethe = moviename[3:] + ', The' replacements = { 'cd': '', 'cdNr': '', 'ext': '.mkv', 'namethe': namethe.strip(), 'thename': moviename.strip(), 'year': movie['info']['year'], 'first': namethe[0].upper(), 'quality': movie['info']['quality'], 'video': movie['info']['codec']['video'], 'audio': movie['info']['codec']['audio'], 'group': movie['info']['group'], 'resolution': movie['info']['resolution'], 'sourcemedia': movie['info']['sourcemedia'] } if multiple: cd = 1 justAdded = [] finalDestination = None finalFilename = self.doReplace(fileNaming, replacements) for file in movie['files']: log.info('Trying to find a home for: %s' % latinToAscii(file['filename'])) replacements['ext'] = file['ext'] if multiple: replacements['cd'] = ' cd' + str(cd) replacements['cdNr'] = ' ' + str(cd) replacements['original'] = file['filename'] folder = self.doReplace(folderNaming, replacements) filename = self.doReplace(fileNaming, replacements) old = os.path.join(movie['path'], file['filename']) dest = os.path.join(destination, folder, filename) finalDestination = os.path.dirname(dest) if not os.path.isdir(finalDestination): try: log.info('Creating directory %s' % finalDestination) os.makedirs(finalDestination) shutil.copymode(destination, finalDestination) except OSError: log.error('Failed changing permissions %s' % finalDestination) # Remove old if better quality removed = self.removeOld(os.path.join(destination, folder), justAdded, movie['info']['size']) if not os.path.isfile(dest) and removed: log.info('Moving file "%s" to %s.' % (latinToAscii(old), dest)) shutil.move(old, dest) justAdded.append(dest) else: try: path = file['path'].split(os.sep) path.extend(['_EXISTS_' + path.pop()]) shutil.move(file['path'], os.sep.join(path)) except IOError: pass log.error('File %s already exists or not better.' % latinToAscii(filename)) break #get subtitle if any & move for type in movie['subtitles']: if len(movie['subtitles'][type]) > 0: log.info('Moving matching subtitle.') subtitle = movie['subtitles'][type].pop(0) replacements['ext'] = subtitle['ext'] subDest = os.path.join(destination, folder, self.doReplace(fileNaming, replacements)) shutil.move(os.path.join(movie['path'], subtitle['filename']), subDest) justAdded.append(subDest) # Add to ignore list when removing stuff. # Add to renaming history h = RenameHistory() h.movieId = movie['movie'].id h.movieQueue = movie['history'].movieQueue if movie['history'] else 0 h.old = unicode(old.decode('utf-8')) h.new = unicode(dest.decode('utf-8')) Db.add(h) Db.flush() if multiple: cd += 1 # Mark movie downloaded if movie['queue'] and movie['queue'].id > 0: if movie['queue'].markComplete: movie['movie'].status = u'downloaded' movie['queue'].completed = True Db.flush() return { 'directory': finalDestination, 'filename': finalFilename }