def index(self, **data): ''' See latest log file ''' fileAbs = self.logFile() filename = file if data.get('nr') and int( data.get('nr')) > 0 and os.path.isfile(fileAbs + '.' + data.get('nr')): fileAbs += '.' + data.get('nr') filename += '.' + data.get('nr') # Reverse f = open(fileAbs, 'r') lines = [] for line in f.readlines(): lines.insert(0, line) log = '' for line in lines: log += line return self.render({ 'file': filename, 'log': escape(latinToAscii(log)) })
def log(self, msg='', severity=logging.INFO): try: self.logger.error(msg=app.latinToAscii(msg), context=self.context, severity=severity) except: self.logger.error(msg=msg, context=self.context, severity=severity)
def getInfo(self, filePath): key = md5(latinToAscii(filePath)).digest() if not self.hashes.get(key): self.hashes[key] = { 'moviehash': hashFile(filePath), 'moviebytesize': str(os.path.getsize(filePath)) } return self.hashes.get(key)
def toSearchString(self, string): string = latinToAscii(string) string = ''.join( (c for c in unicodedata.normalize('NFD', unicode(string)) if unicodedata.category(c) != 'Mn')) safe_chars = ascii_letters + digits + ' \'' r = ''.join([char if char in safe_chars else ' ' for char in string]) return re.sub('\s+', ' ', r).replace('\'s', 's').replace('\'', ' ')
def addToHistory(self, movie, file, key, data): key = self.getKey(key) movieId = 0 if not movie else movie.id new = SubtitleHistory() new.movie = movieId new.file = latinToAscii(file) new.subtitle = key new.status = u'downloaded' new.data = str(data) Db.add(new) Db.flush()
def determineMovie(self, files): ''' Try find movie based on folder names and MovieQueue table ''' for file in files['files']: dirnames = file['path'].split(os.path.sep) dirnames.append(file['filename']) dirnames.reverse() for dir in dirnames: dir = latinToAscii(dir) # check and see if name is in queue queue = Db.query(MovieQueue).filter_by(name = dir).first() if queue: log.info('Found movie via MovieQueue.') return { 'queue': queue, 'movie': queue.Movie } # last resort, match every word in path to db lastResort = {} dir = dir.replace('.', ' ').lower() dirSplit = dir.split(' ') for s in dirSplit: if s: results = Db.query(Movie).filter(Movie.name.like('%' + s + '%')).all() for r in results: lastResort[r.id] = r for x in lastResort: l = lastResort[x] wordCount = 0 words = l.name.lower().split(' ') for word in words: if word.lower() in dir: wordCount += 1 if wordCount == len(words) and len(words) > 0: log.info('Found via last resort searching.') return { 'queue': None, 'movie': l } # Try finding movie on theMovieDB # more checking here.. return False
def alreadyDownloaded(self, movie, file, key): key = self.getKey(key) movieId = 0 if not movie else movie.id file = latinToAscii(file) results = Db.query(SubtitleHistory).filter(and_(SubtitleHistory.subtitle == key, SubtitleHistory.movie == movieId, SubtitleHistory.file == file)).all() if cherrypy.config.get('debug'): log.debug('%s already downloaded.' % results) return False else: for h in results: h.status = u'ignored' Db.flush() return len(results) > 0
def cleanName(self, text): cleaned = " ".join(re.split("\W+", latinToAscii(text).lower())) cleaned = re.sub(self.clean, " ", cleaned) year = self.findYear(cleaned) if year: # Split name on year try: movieName = cleaned.split(year).pop(0).strip() return movieName except: pass else: # Split name on multiple spaces try: movieName = cleaned.split(" ").pop(0).strip() return movieName except: pass return None
def cleanName(self, text): cleaned = ' '.join(re.split('\W+', latinToAscii(text).lower())) cleaned = re.sub(self.clean, ' ', cleaned) year = self.findYear(cleaned) if year: # Split name on year try: movieName = cleaned.split(year).pop(0).strip() return movieName except: pass else: # Split name on multiple spaces try: movieName = cleaned.split(' ').pop(0).strip() return movieName except: pass return None
def index(self, **data): ''' See latest log file ''' fileAbs = self.logFile() filename = file if data.get('nr') and int(data.get('nr')) > 0 and os.path.isfile(fileAbs + '.' + data.get('nr')): fileAbs += '.' + data.get('nr') filename += '.' + data.get('nr') # Reverse f = open(fileAbs, 'r') lines = [] for line in f.readlines(): lines.insert(0, line) log = '' for line in lines: log += line return self.render({'file':filename, 'log':escape(latinToAscii(log))})
def determineMovie(self, movie): for file in movie['files']: dirnames = movie['path'].split(os.path.sep) dirnames.append(file['filename']) dirnames.reverse() for dir in dirnames: dir = latinToAscii(dir) # check and see if name is in queue queue = Db.query(MovieQueue).filter_by(name = dir).first() if queue: log.info('Found movie via MovieQueue.') return queue.Movie # last resort, match every word in path to db lastResort = {} dirSplit = re.split('\W+', dir.lower()) for s in dirSplit: if s: results = Db.query(Movie).filter(Movie.name.like('%' + s + '%')).all() for r in results: lastResort[r.id] = r for l in lastResort.itervalues(): wordCount = 0 words = re.split('\W+', l.name.lower()) for word in words: if word in dir.lower(): wordCount += 1 if wordCount == len(words) and len(words) > 0 and str(l.year) in dir: return l return None
def search(self, movie): if not self.isEnabled(): return for provider in self.providers: log.info('Search %s for subtitles: %s.' % (provider.name, latinToAscii(movie['folder']))) result = provider.find(movie) if(result.get('subtitles')): subtitleDownloads = result['download'](result) if(subtitleDownloads): # Add subtitle to history for sub in result.get('subtitles'): provider.addToHistory(movie['movie'], sub['forFile'], sub['id'], sub) # Move subtitles to movie directory for movieFile in movie['files']: subtitleFile = subtitleDownloads.pop() subtitlePath = os.path.join(movie['path'], movieFile['filename'][:-len(movieFile['ext'])]) subtitleExt = os.path.splitext(subtitleFile)[1].lower()[1:] lang = result['language'] + '.' if self.conf('addLanguage') else '' shutil.move(subtitleFile, subtitlePath + lang + subtitleExt) break
def _search(self, movie, force = False): # Stop caching ffs! Db.expire_all() # Check release date and search for appropriate qualities preReleaseSearch = False dvdReleaseSearch = False now = int(time.time()) # Search all if ETA is unknow, but try update ETA for next time. log.debug('Calculate ETA') checkETA = False if not movie.eta: checkETA = True preReleaseSearch = True dvdReleaseSearch = True else: # Prerelease 1 week before theaters if movie.eta.theater <= now + 604800: preReleaseSearch = True # dvdRelease 6 weeks before dvd release if movie.eta.dvd <= now + 3628800: preReleaseSearch = True dvdReleaseSearch = True # Dvd date is unknown but movie is in theater already if movie.eta.dvd == 0 and movie.eta.theater > now: dvdReleaseSearch = False # Force ETA check if movie.eta.lastCheck < now: checkETA = True # Minimal week interval for ETA check if checkETA and self.config.get('MovieETA', 'enabled'): cherrypy.config.get('searchers').get('etaQueue').put({'id':movie.id}) for queue in movie.queue: # Movie already found, don't search further if queue.completed: log.debug('%s already completed for "%s". Not searching for any qualities below.' % (queue.qualityType, movie.name)) return True # only search for active and not completed, minimal 1 min since last search if queue.active and not queue.completed and not self.abort and not self.stop: #skip if no search is set log.debug('Needs a search?') if (not ((preReleaseSearch and queue.qualityType in Qualities.preReleases) or (dvdReleaseSearch and not queue.qualityType in Qualities.preReleases))) and not queue.lastCheck < (now - int(self.config.get('Intervals', 'search')) * 7200): continue log.debug('Start searching for movie: %s' % movie.name) highest = self.provider.find(movie, queue) log.debug('End searching for movie: %s' % movie.name) #send highest to SABnzbd & mark as snatched if highest: log.debug('Found highest') #update what I found queue.name = latinToAscii(highest.name) queue.link = highest.detailUrl Db.flush() waitFor = queue.waitFor * (60 * 60 * 24) if queue.markComplete or (not queue.markComplete and highest.date + waitFor < time.time()): time.sleep(10) # Give these APIs air! if self.config.get('NZB', 'sendTo') == 'Sabnzbd' and highest.type == 'nzb': success = self.sabNzbd.send(highest, movie.imdb) elif self.config.get('NZB', 'sendTo') == 'Nzbget' and highest.type == 'nzb': success = self.nzbGet.send(highest) elif self.config.get('Torrents', 'sendTo') == 'Transmission' and highest.type == 'torrent': success = self.transmission.send(highest, movie.imdb) else: success = self.blackHole(highest) else: success = False log.info('Found %s but waiting for %d hours.' % (highest.name, ((highest.date + waitFor) - time.time()) / (60 * 60))) # Set status if success: log.debug('Success') movie.status = u'snatched' if queue.markComplete else u'waiting' movie.dateChanged = datetime.datetime.now() queue.lastCheck = now queue.completed = True Db.flush() # Add to history h = History() h.movie = movie.id h.value = str(highest.id) + '-' + str(highest.size) h.status = u'snatched' Db.add(h) Db.flush() # Notify PROWL if self.config.get('PROWL', 'onSnatch'): log.debug('PROWL') prowl = PROWL() prowl.notify(highest.name, 'Download Started') # Notify XBMC if self.config.get('XBMC', 'onSnatch'): log.debug('XBMC') xbmc = XBMC() xbmc.notify('Snatched %s' % highest.name) # Notify GROWL if self.config.get('GROWL', 'onSnatch'): log.debug('GROWL') growl = GROWL() growl.notify('Snatched %s' % highest.name, 'Download Started') # Notify Notifo if self.config.get('Notifo', 'onSnatch'): log.debug('Notifo') notifo = Notifo() notifo.notify('%s' % highest.name, "Snatched:") # Notify NotifyMyAndroid if self.config.get('NMA', 'onSnatch'): log.debug('NotifyMyAndroid') nma = NMA() nma.notify('Download Started', 'Snatched %s' % highest.name) # Notify Twitter if self.config.get('Twitter', 'onSnatch'): log.debug('Twitter') twitter = Twitter() twitter.notify('Download Started', 'Snatched %s' % highest.name) return True queue.lastCheck = now Db.flush() return False
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 }
def _search(self, movie, force = False): # Stop caching ffs! Db.expire_all() # Check release date and search for appropriate qualities preReleaseSearch = False dvdReleaseSearch = False now = int(time.time()) # Search all if ETA is unknow, but try update ETA for next time. checkETA = False if not movie.eta or force: checkETA = True preReleaseSearch = True dvdReleaseSearch = True else: # Prerelease 1 week before theaters if movie.eta.theater <= now + 604800: preReleaseSearch = True # dvdRelease 6 weeks before dvd release if movie.eta.dvd <= now + 3628800: preReleaseSearch = True dvdReleaseSearch = True # Dvd date is unknown but movie is in theater already if movie.eta.dvd == 0 and movie.eta.theater > now: checkETA = True dvdReleaseSearch = False # Force ETA check once a week or 3 weeks if ((movie.eta.dvd == 0 or movie.eta.theater == 0) and movie.eta.lastCheck < now - 604800) or (movie.eta.lastCheck < now - 1814400): checkETA = True # Minimal week interval for ETA check if checkETA: cherrypy.config.get('searchers').get('etaQueue').put({'id':movie.id}) for queue in movie.queue: # Movie already found, don't search further if queue.completed: log.debug('%s already completed for "%s". Not searching for any qualities below.' % (queue.qualityType, movie.name)) return True # only search for active and not completed, minimal 1 min since last search if queue.active and not queue.completed and not self.abort and not self.stop: #skip if no search is set if (not ((preReleaseSearch and queue.qualityType in Qualities.preReleases) or (dvdReleaseSearch and not queue.qualityType in Qualities.preReleases))) and not queue.lastCheck < (now - int(self.config.get('Intervals', 'search')) * 7200): continue highest = self.provider.find(movie, queue) #send highest to SABnzbd & mark as snatched if highest: #update what I found queue.name = latinToAscii(highest.name) queue.link = highest.detailUrl Db.flush() waitFor = queue.waitFor * (60 * 60 * 24) if queue.markComplete or (not queue.markComplete and highest.date + waitFor < time.time()): time.sleep(10) # Give these APIs air! if self.config.get('NZB', 'sendTo') == 'Sabnzbd' and highest.type == 'nzb': success = self.sabNzbd.send(highest, movie.imdb) else: success = self.blackHole(highest) else: success = False log.info('Found %s but waiting for %d hours.' % (highest.name, ((highest.date + waitFor) - time.time()) / (60 * 60))) # Set status if success: movie.status = u'snatched' if queue.markComplete else u'waiting' movie.dateChanged = datetime.datetime.now() queue.lastCheck = now queue.completed = True Db.flush() # Add to history h = History() h.movie = movie.id h.value = str(highest.id) + '-' + str(highest.size) h.status = u'snatched' Db.add(h) Db.flush() return True queue.lastCheck = now Db.flush() return False
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 _search(self, movie, force=False): # Stop caching ffs! Db.expire_all() # Check release date and search for appropriate qualities preReleaseSearch = False dvdReleaseSearch = False now = int(time.time()) # Search all if ETA is unknow, but try update ETA for next time. log.debug('Calculate ETA') checkETA = False if not movie.eta: checkETA = True preReleaseSearch = True dvdReleaseSearch = True else: # Prerelease 1 week before theaters if movie.eta.theater <= now + 604800: preReleaseSearch = True # dvdRelease 6 weeks before dvd release if movie.eta.dvd <= now + 3628800: preReleaseSearch = True dvdReleaseSearch = True # Dvd date is unknown but movie is in theater already if movie.eta.dvd == 0 and movie.eta.theater > now: dvdReleaseSearch = False # Force ETA check if movie.eta.lastCheck < now: checkETA = True # Minimal week interval for ETA check if checkETA and self.config.get('MovieETA', 'enabled'): cherrypy.config.get('searchers').get('etaQueue').put( {'id': movie.id}) for queue in movie.queue: # Movie already found, don't search further if queue.completed: log.debug( '%s already completed for "%s". Not searching for any qualities below.' % (queue.qualityType, movie.name)) return True # only search for active and not completed, minimal 1 min since last search if queue.active and not queue.completed and not self.abort and not self.stop: #skip if no search is set log.debug('Needs a search?') if (not ((preReleaseSearch and queue.qualityType in Qualities.preReleases) or (dvdReleaseSearch and not queue.qualityType in Qualities.preReleases)) ) and not queue.lastCheck < (now - int( self.config.get('Intervals', 'search')) * 7200): continue log.debug('Start searching for movie: %s' % movie.name) highest = self.provider.find(movie, queue) log.debug('End searching for movie: %s' % movie.name) #send highest to SABnzbd & mark as snatched if highest: log.debug('Found highest') #update what I found queue.name = latinToAscii(highest.name) queue.link = highest.detailUrl Db.flush() waitFor = queue.waitFor * (60 * 60 * 24) if queue.markComplete or ( not queue.markComplete and highest.date + waitFor < time.time()): time.sleep(10) # Give these APIs air! if self.config.get( 'NZB', 'sendTo' ) == 'Sabnzbd' and highest.type == 'nzb': success = self.sabNzbd.send(highest, movie.imdb) elif self.config.get( 'NZB', 'sendTo' ) == 'Nzbget' and highest.type == 'nzb': success = self.nzbGet.send(highest) elif self.config.get( 'Torrents', 'sendTo' ) == 'Transmission' and highest.type == 'torrent': success = self.transmission.send( highest, movie.imdb) else: success = self.blackHole(highest) else: success = False log.info('Found %s but waiting for %d hours.' % (highest.name, ((highest.date + waitFor) - time.time()) / (60 * 60))) # Set status if success: log.debug('Success') movie.status = u'snatched' if queue.markComplete else u'waiting' movie.dateChanged = datetime.datetime.now() queue.lastCheck = now queue.completed = True Db.flush() # Add to history h = History() h.movie = movie.id h.value = str(highest.id) + '-' + str(highest.size) h.status = u'snatched' Db.add(h) Db.flush() # Notify PROWL if self.config.get('PROWL', 'onSnatch'): log.debug('PROWL') prowl = PROWL() prowl.notify(highest.name, 'Download Started') # Notify XBMC if self.config.get('XBMC', 'onSnatch'): log.debug('XBMC') xbmc = XBMC() xbmc.notify('Snatched %s' % highest.name) # Notify GROWL if self.config.get('GROWL', 'onSnatch'): log.debug('GROWL') growl = GROWL() growl.notify('Snatched %s' % highest.name, 'Download Started') # Notify Notifo if self.config.get('Notifo', 'onSnatch'): log.debug('Notifo') notifo = Notifo() notifo.notify('%s' % highest.name, "Snatched:") # Notify SNS if self.config.get('SNS', 'onSnatch'): log.debug('SNS') sns = SNS() sns.notify('%s' % highest.name, "Snatched:") # Notify Boxcar if self.config.get('Boxcar', 'onSnatch'): log.debug('Boxcar') boxcar = Boxcar() boxcar.notify('%s' % highest.name, "Snatched:") # Notify NotifyMyAndroid if self.config.get('NMA', 'onSnatch'): log.debug('NotifyMyAndroid') nma = NMA() nma.notify('Download Started', 'Snatched %s' % highest.name) # Notify NotifyMyWindowsPhone if self.config.get('NMWP', 'onSnatch'): log.debug('NotifyMyWindowsPhone') nmwp = NMWP() nmwp.notify('Download Started', 'Snatched %s' % highest.name) # Notify Twitter if self.config.get('Twitter', 'onSnatch'): log.debug('Twitter') twitter = Twitter() twitter.notify('Download Started', 'Snatched %s' % highest.name) return True queue.lastCheck = now Db.flush() return False
def log(self, msg = '', severity = logging.INFO): try: self.logger.error(msg = app.latinToAscii(msg), context = self.context, severity = severity) except: self.logger.error(msg = msg, context = self.context, severity = severity)
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 toSearchString(self, string): string = latinToAscii(string) string = ''.join((c for c in unicodedata.normalize('NFD', unicode(string)) if unicodedata.category(c) != 'Mn')) safe_chars = ascii_letters + digits + ' \'' r = ''.join([char if char in safe_chars else ' ' for char in string]) return re.sub('\s+' , ' ', r).replace('\'s', 's').replace('\'', ' ')
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 toSearchString(self, string): string = latinToAscii(string) string = "".join((c for c in unicodedata.normalize("NFD", unicode(string)) if unicodedata.category(c) != "Mn")) safe_chars = ascii_letters + digits + " '" r = "".join([char if char in safe_chars else " " for char in string]) return re.sub("\s+", " ", r).replace("'s", "s").replace("'", " ")
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, 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 }