def buildIndexerCache(): logger.log(u"Updating internal scene name cache", logger.MESSAGE) global exceptionIndexerCache exceptionIndexerCache = {} for show in sickbeard.showList: for curSeason in [-1] + sickbeard.scene_exceptions.get_scene_seasons(show.indexerid): exceptionIndexerCache[helpers.full_sanitizeSceneName(show.name)] = show.indexerid for name in get_scene_exceptions(show.indexerid, season=curSeason): exceptionIndexerCache[name] = show.indexerid exceptionIndexerCache[helpers.full_sanitizeSceneName(name)] = show.indexerid logger.log(u"Updated internal scene name cache", logger.MESSAGE) logger.log(u"Internal scene name cache set to: " + str(exceptionIndexerCache), logger.DEBUG)
def can_reject(release_name): """ Check if a release name should be rejected at external services. If any site reports result as a valid scene release, then return None, None. If predb reports result as nuked, then return nuke reason and url attempted. If fail to find result at all services, return reject and url details for each site. :param release_name: Release title :type release_name: String :return: None, None if release has no issue otherwise True/Nuke reason, URLs that rejected :rtype: Tuple (None, None or True/String, String) """ rej_urls = [] srrdb_url = 'https://www.srrdb.com/api/search/r:%s/order:date-desc' % re.sub( '\]\[', '', release_name) resp = helpers.getURL(srrdb_url, json=True) if not resp: srrdb_rej = True rej_urls += ['Failed contact \'%s\'' % srrdb_url] else: srrdb_rej = (not len(resp.get('results', [])) or release_name.lower() != resp.get( 'results', [{}])[0].get('release', '').lower()) rej_urls += ([], ['\'%s\'' % srrdb_url])[srrdb_rej] sane_name = helpers.full_sanitizeSceneName(release_name) predb_url = 'https://predb.ovh/api/v1/?q=@name "%s"' % sane_name resp = helpers.getURL(predb_url, json=True) predb_rej = True if not resp: rej_urls += ['Failed contact \'%s\'' % predb_url] elif 'success' == resp.get('status', '').lower(): rows = resp and (resp.get('data') or {}).get('rows') or [] for data in rows: if sane_name == helpers.full_sanitizeSceneName( (data.get('name', '') or '').strip()): nuke_type = (data.get('nuke') or {}).get('type') if not nuke_type: predb_rej = not helpers.tryInt(data.get('preAt')) else: predb_rej = 'un' not in nuke_type and data.get( 'nuke', {}).get('reason', 'Reason not set') break rej_urls += ([], ['\'%s\'' % predb_url])[bool(predb_rej)] pred = any([not srrdb_rej, not predb_rej]) return pred and (None, None) or (predb_rej or True, ', '.join(rej_urls))
def buil_name_set(): logger.log(u"Updating internal scene name cache", logger.MESSAGE) _excpetionDots = [] global exception_tvdb exception_tvdb = {} for show in sickbeard.showList: for curSeason in [-1] + sickbeard.scene_exceptions.get_scene_seasons(show.tvdbid): exception_tvdb[helpers.full_sanitizeSceneName(show.name)] = show.tvdbid _excpetionDots.append(".") for name in get_scene_exceptions(show.tvdbid, season=curSeason): exception_tvdb[name] = show.tvdbid exception_tvdb[helpers.full_sanitizeSceneName(name)] = show.tvdbid _excpetionDots.append(".") logger.log(u"Updated internal scene name cache " + "".join(_excpetionDots), logger.MESSAGE) logger.log(u"Internal scene name cache set to: " + str(exception_tvdb), logger.DEBUG)
def can_reject(release_name): """ Check if a release name should be rejected at external services. If any site reports result as a valid scene release, then return None, None. If predb reports result as nuked, then return nuke reason and url attempted. If fail to find result at all services, return reject and url details for each site. :param release_name: Release title :type release_name: String :return: None, None if release has no issue otherwise True/Nuke reason, URLs that rejected :rtype: Tuple (None, None or True/String, String) """ rej_urls = [] srrdb_url = 'https://www.srrdb.com/api/search/r:%s/order:date-desc' % re.sub('\]\[', '', release_name) resp = helpers.getURL(srrdb_url, json=True) if not resp: srrdb_rej = True rej_urls += ['Failed contact \'%s\'' % srrdb_url] else: srrdb_rej = (not len(resp.get('results', [])) or release_name.lower() != resp.get('results', [{}])[0].get('release', '').lower()) rej_urls += ([], ['\'%s\'' % srrdb_url])[srrdb_rej] sane_name = helpers.full_sanitizeSceneName(release_name) predb_url = 'https://predb.ovh/api/v1/?q=@name "%s"' % sane_name resp = helpers.getURL(predb_url, json=True) predb_rej = True if not resp: rej_urls += ['Failed contact \'%s\'' % predb_url] elif 'success' == resp.get('status', '').lower(): rows = resp and (resp.get('data') or {}).get('rows') or [] for data in rows: if sane_name == helpers.full_sanitizeSceneName((data.get('name', '') or '').strip()): nuke_type = (data.get('nuke') or {}).get('type') if not nuke_type: predb_rej = not helpers.tryInt(data.get('preAt')) else: predb_rej = 'un' not in nuke_type and data.get('nuke', {}).get('reason', 'Reason not set') break rej_urls += ([], ['\'%s\'' % predb_url])[bool(predb_rej)] pred = any([not srrdb_rej, not predb_rej]) return pred and (None, None) or (predb_rej or True, ', '.join(rej_urls))
def retrieveNameFromCache(name): """ Looks up the given name in the scene_names table in cache.db. :param name: The show name to look up. :return: the TVDB id that resulted from the cache lookup or None if the show wasn't found in the cache """ name = full_sanitizeSceneName(name) if name in nameCache: return int(nameCache[name])
def get_scene_exception_by_name_multiple(show_name): """ Given a show name, return the indexerid of the exception, None if no exception is present. """ try: exception_result = name_cache.nameCache[helpers.full_sanitizeSceneName(show_name)] return [exception_result] except: return [[None, None]]
def get_show_by_name(self, name, showList, useTvdb=False): if not name: self._log(u"Not trying to get the tvdbid. No name given", logger.DEBUG) return None self._log(u"Trying to get the tvdbid for " + name, logger.DEBUG) name = helpers.full_sanitizeSceneName(name) cacheResult = sickbeard.name_cache.retrieveNameFromCache(name) if cacheResult: return helpers.findCertainShow(sickbeard.showList, cacheResult) if name in sickbeard.scene_exceptions.exception_tvdb: self._log(u"Found " + name + " in the exception list", logger.DEBUG) return helpers.findCertainShow( showList, sickbeard.scene_exceptions.exception_tvdb[name]) else: self._log(u"Did NOT find " + name + " in the exception list", logger.DEBUG) if useTvdb: try: t = tvdb_api.Tvdb(custom_ui=classes.ShowListUI, **sickbeard.TVDB_API_PARMS) showObj = t[name] except (tvdb_exceptions.tvdb_exception): # if none found, search on all languages try: # There's gotta be a better way of doing this but we don't wanna # change the language value elsewhere ltvdb_api_parms = sickbeard.TVDB_API_PARMS.copy() ltvdb_api_parms['search_all_languages'] = True t = tvdb_api.Tvdb(custom_ui=classes.ShowListUI, **ltvdb_api_parms) showObj = t[name] except (tvdb_exceptions.tvdb_exception, IOError): pass return None except (IOError): return None else: show = helpers.findCertainShow(sickbeard.showList, int(showObj["id"])) if show: return show return None
def addNameToCache(name, indexer_id=0): """ Adds the show & tvdb id to the scene_names table in cache.db. :param name: The show name to cache :param indexer_id: the TVDB id that this show should be cached with (can be None/0 for unknown) """ cache_db_con = db.DBConnection('cache.db') # standardize the name we're using to account for small differences in providers name = full_sanitizeSceneName(name) if name not in nameCache: nameCache[name] = int(indexer_id) cache_db_con.action("INSERT OR REPLACE INTO scene_names (indexer_id, name) VALUES (?, ?)", [indexer_id, name])
def get_show_by_name(self, name, showList, useTvdb=False): if not name: self._log(u"Not trying to get the tvdbid. No name given", logger.DEBUG) return None self._log(u"Trying to get the tvdbid for " + name, logger.DEBUG) name = helpers.full_sanitizeSceneName(name) cacheResult = sickbeard.name_cache.retrieveNameFromCache(name) if cacheResult: return helpers.findCertainShow(sickbeard.showList, cacheResult) if name in sickbeard.scene_exceptions.exception_tvdb: self._log(u"Found " + name + " in the exception list", logger.DEBUG) return helpers.findCertainShow(showList, sickbeard.scene_exceptions.exception_tvdb[name]) else: self._log(u"Did NOT find " + name + " in the exception list", logger.DEBUG) if useTvdb: try: t = tvdb_api.Tvdb(custom_ui=classes.ShowListUI, **sickbeard.TVDB_API_PARMS) showObj = t[name] except (tvdb_exceptions.tvdb_exception): # if none found, search on all languages try: # There's gotta be a better way of doing this but we don't wanna # change the language value elsewhere ltvdb_api_parms = sickbeard.TVDB_API_PARMS.copy() ltvdb_api_parms['search_all_languages'] = True t = tvdb_api.Tvdb(custom_ui=classes.ShowListUI, **ltvdb_api_parms) showObj = t[name] except (tvdb_exceptions.tvdb_exception, IOError): pass return None except (IOError): return None else: show = helpers.findCertainShow(sickbeard.showList, int(showObj["id"])) if show: return show return None
def update_scene_exceptions(indexer_id, scene_exceptions): """ Given a indexer_id, and a list of all show scene exceptions, update the db. """ global exceptionIndexerCache myDB = db.DBConnection('cache.db') myDB.action('DELETE FROM scene_exceptions WHERE indexer_id=? and custom=1', [indexer_id]) logger.log(u"Updating internal scene name cache", logger.MESSAGE) for cur_season in [-1] + sickbeard.scene_exceptions.get_scene_seasons(indexer_id): for cur_exception in scene_exceptions: exceptionIndexerCache[helpers.full_sanitizeSceneName(cur_exception)] = indexer_id myDB.action("INSERT INTO scene_exceptions (indexer_id, show_name, season, custom) VALUES (?,?,?,?)", [indexer_id, cur_exception, cur_season, 1]) name_cache.clearCache()
def update_scene_exceptions(indexer_id, scene_exceptions): """ Given a indexer_id, and a list of all show scene exceptions, update the db. """ global exceptionIndexerCache myDB = db.DBConnection("cache.db") myDB.action('DELETE FROM scene_exceptions WHERE indexer_id=? and custom=1', [indexer_id]) logger.log(u"Updating internal scene name cache", logger.MESSAGE) for cur_season in [-1] + sickbeard.scene_exceptions.get_scene_seasons(indexer_id): for cur_exception in scene_exceptions: exceptionIndexerCache[helpers.full_sanitizeSceneName(cur_exception)] = indexer_id myDB.action("INSERT INTO scene_exceptions (indexer_id, show_name, season, custom) VALUES (?,?,?,?)", [indexer_id, cur_exception, cur_season, 1]) name_cache.clearCache()
def buildNameCache(show=None): """Build internal name cache :param show: Specify show to build name cache for, if None, just do all shows """ with nameCacheLock: retrieve_exceptions() if not show: # logger.log(u"Building internal name cache for all shows", logger.INFO) for show in sickbeard.showList: buildNameCache(show) else: # logger.log(u"Building internal name cache for " + show.name, logger.DEBUG) clearCache(show.indexerid) for curSeason in [-1] + get_scene_seasons(show.indexerid): for name in set(get_scene_exceptions(show.indexerid, season=curSeason) + [show.name]): name = full_sanitizeSceneName(name) if name in nameCache: continue nameCache[name] = int(show.indexerid) logger.log(u"Internal name cache for " + show.name + " set to: [ " + u', '.join([key for key, value in iteritems(nameCache) if value == show.indexerid]) + " ]", logger.DEBUG)