def put(self, 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) """ # standardize the name we're using to account for small differences in providers name = full_sanitizeSceneName(name) if name not in self.cache: self.cache[name] = int(indexer_id) try: dbData = [ x['doc'] for x in CacheDB().db.get_many( 'scene_names', name, with_doc=True) if x['doc']['indexer_id'] == indexer_id ] if not len(dbData): # insert name into cache CacheDB().db.insert({ '_t': 'scene_names', 'indexer_id': indexer_id, 'name': name }) except RecordNotFound: # insert name into cache CacheDB().db.insert({ '_t': 'scene_names', 'indexer_id': indexer_id, 'name': name })
def test_sceneExceptionsResetNameCache(self): # clear the exceptions [CacheDB().db.delete(x['doc']) for x in CacheDB().db.all('scene_exceptions', with_doc=True)] # put something in the cache sickrage.srCore.NAMECACHE.put('Cached Name', 0) # updating should not clear the cache this time since our exceptions didn't change self.assertEqual(sickrage.srCore.NAMECACHE.get('Cached Name'), 0)
def clear(self, indexerid=0): """ Deletes all "unknown" entries from the cache (names with indexer_id of 0). """ [CacheDB().db.delete(x['doc']) for x in CacheDB().db.all('scene_names', with_doc=True) if x['doc']['indexer_id'] in [indexerid, 0]] for item in [self.cache[key] for key, value in self.cache.items() if value == 0 or value == indexerid]: del item
def run(self, force=False): if self.amActive: return self.amActive = True # set thread name threading.currentThread().setName(self.name) update_timestamp = time.mktime(datetime.datetime.now().timetuple()) try: dbData = CacheDB().db.get('lastUpdate', 'theTVDB', with_doc=True)['doc'] last_update = dbData['time'] except RecordNotFound: last_update = time.mktime(datetime.datetime.min.timetuple()) dbData = CacheDB().db.insert({ '_t': 'lastUpdate', 'provider': 'theTVDB', 'time': long(last_update) }) # get indexer updated show ids updated_shows = srIndexerApi().indexer( **srIndexerApi().api_params.copy()).updated(long(last_update)) # start update process piList = [] for curShow in sickrage.srCore.SHOWLIST: try: curShow.nextEpisode() if not os.path.isdir(curShow.location): continue if curShow.indexerid in set(d["id"] for d in updated_shows or {}): piList.append( sickrage.srCore.SHOWQUEUE.updateShow(curShow, True)) elif datetime.date.fromordinal( curShow.last_refresh) > datetime.timedelta(days=1): piList.append( sickrage.srCore.SHOWQUEUE.refreshShow(curShow, False)) except (CantUpdateShowException, CantRefreshShowException): continue ProgressIndicators.setIndicator( 'dailyShowUpdates', QueueProgressIndicator("Daily Show Updates", piList)) dbData['time'] = long(update_timestamp) CacheDB().db.update(dbData) self.amActive = False
def update_network_dict(): """Update timezone information from SR repositories""" url = 'http://sickragetv.github.io/network_timezones/network_timezones.txt' try: url_data = sickrage.srCore.srWebSession.get(url).text except Exception: sickrage.srCore.srLogger.warning( 'Updating network timezones failed, this can happen from time to time. URL: %s' % url) return d = {} try: for line in url_data.splitlines(): (key, val) = line.strip().rsplit(':', 1) if key is None or val is None: continue d[key] = val except (IOError, OSError): pass network_timezones = load_network_dict() queries = [] for network, timezone in d.items(): existing = network in network_timezones if not existing: CacheDB().db.insert({ '_t': 'network_timezones', 'network_name': network, 'timezone': timezone }) elif network_timezones[network] is not timezone: try: dbData = CacheDB().db.get('network_timezones', network, with_doc=True)['doc'] dbData['timezone'] = timezone CacheDB().db.update(dbData) except RecordNotFound: continue if existing: del network_timezones[network] if network_timezones: for x in network_timezones: try: CacheDB().db.delete(CacheDB().db.get('network_timezones', x, with_doc=True)['doc']) except RecordNotFound: continue
def last_search(self, toDate): try: dbData = CacheDB().db.get('lastSearch', self.providerID, with_doc=True)['doc'] dbData['time'] = int(time.mktime(toDate.timetuple())) CacheDB().db.update(dbData) except RecordNotFound: CacheDB().db.insert({ '_t': 'lastUpdate', 'provider': self.providerID, 'time': int(time.mktime(toDate.timetuple())) })
def add(self, ahash, title, description, image, date): session = sickrage.app.cache_db.session() self._announcements[ahash] = Announcement(title, description, image, date, ahash) if not session.query(CacheDB.Announcements).filter_by(hash=ahash).count(): sickrage.app.log.debug('Adding new announcement to Web-UI') session.add(CacheDB.Announcements(**{'hash': ahash})) session.commit()
def test_allPossibleShowNames(self): session = sickrage.app.cache_db.session() session.add( CacheDB.SceneException(**{ 'indexer_id': 1, 'show_name': 'Exception Test', 'season': -1 })) session.commit() exceptionsCache[-1] = ['Exception Test'] countryList['Full Country Name'] = 'FCN' self._test_allPossibleShowNames('Show Name', expected=['Show Name']) self._test_allPossibleShowNames( 'Show Name', 1, expected=['Show Name', 'Exception Test']) self._test_allPossibleShowNames( 'Show Name FCN', expected=['Show Name FCN', 'Show Name (Full Country Name)']) self._test_allPossibleShowNames( 'Show Name (FCN)', expected=['Show Name (FCN)', 'Show Name (Full Country Name)']) self._test_allPossibleShowNames( 'Show Name Full Country Name', expected=['Show Name Full Country Name', 'Show Name (FCN)']) self._test_allPossibleShowNames( 'Show Name (Full Country Name)', expected=['Show Name (Full Country Name)', 'Show Name (FCN)'])
def get_scene_exceptions(indexer_id, season=-1): """ Given a indexer_id, return a list of all the scene exceptions. """ exceptionsList = [] if indexer_id not in exceptionsCache or season not in exceptionsCache[ indexer_id]: try: exceptionsList = list( set([ cur_exception['show_name'] for cur_exception in [ x['doc'] for x in CacheDB().db.get_many( 'scene_exceptions', indexer_id, with_doc=True) ] if cur_exception['season'] == season ])) if not indexer_id in exceptionsCache: exceptionsCache[indexer_id] = {} exceptionsCache[indexer_id][season] = exceptionsList except: pass else: exceptionsList = exceptionsCache[indexer_id][season] if season == 1: # if we where looking for season 1 we can add generic names exceptionsList += get_scene_exceptions(indexer_id, season=-1) return exceptionsList
def setLastUpdate(self, toDate=None): if not toDate: toDate = datetime.datetime.today() try: dbData = CacheDB().db.get('lastUpdate', self.providerID, with_doc=True)['doc'] dbData['time'] = int(time.mktime(toDate.timetuple())) CacheDB().db.update(dbData) except RecordNotFound: CacheDB().db.insert({ '_t': 'lastUpdate', 'provider': self.providerID, 'time': int(time.mktime(toDate.timetuple())) })
def test_allPossibleShowNames(self): CacheDB().db.insert({ '_t': 'scene_exceptions', 'indexer_id': -1, 'show_name': 'Exception Test', 'season': -1 }) exceptionsCache[-1] = ['Exception Test'] countryList['Full Country Name'] = 'FCN' self._test_allPossibleShowNames('Show Name', expected=['Show Name']) self._test_allPossibleShowNames( 'Show Name', -1, expected=['Show Name', 'Exception Test']) self._test_allPossibleShowNames( 'Show Name FCN', expected=['Show Name FCN', 'Show Name (Full Country Name)']) self._test_allPossibleShowNames( 'Show Name (FCN)', expected=['Show Name (FCN)', 'Show Name (Full Country Name)']) self._test_allPossibleShowNames( 'Show Name Full Country Name', expected=['Show Name Full Country Name', 'Show Name (FCN)']) self._test_allPossibleShowNames( 'Show Name (Full Country Name)', expected=['Show Name (Full Country Name)', 'Show Name (FCN)'])
def save(self): """Commit cache to database file""" for name, indexer_id in self.cache.items(): try: dbData = [x['doc'] for x in CacheDB().db.get_many('scene_names', name, with_doc=True) if x['doc']['indexer_id'] == indexer_id] if len(dbData): continue except RecordNotFound: pass # insert name into cache CacheDB().db.insert({ '_t': 'scene_names', 'indexer_id': indexer_id, 'name': name })
def token(self, value): new_token = { 'access_token': value.get('access_token'), 'refresh_token': value.get('refresh_token'), 'expires_in': value.get('expires_in'), 'expires_at': value.get('expires_at', int(time.time() + value.get('expires_in'))), 'scope': value.scope if isinstance(value, OAuth2Token) else value.get('scope') } session = sickrage.app.cache_db.session() try: token = session.query(CacheDB.OAuth2Token).one() token.update(**new_token) except orm.exc.NoResultFound: session.add(CacheDB.OAuth2Token(**new_token)) finally: session.commit() self._session = None
def load_network_dict(): """ Return network timezones from db """ return dict([(x['doc']['network_name'], x['doc']['timezone']) for x in CacheDB().db.all('network_timezones', with_doc=True) ])
def list_propers(self, date=None): return [ x['doc'] for x in CacheDB().db.get_many( 'providers', self.providerID, with_doc=True) if ('.PROPER.' in x['doc']['name'] or '.REPACK.' in x['doc']['name']) and x['doc']['time'] >= str(int(time.mktime(date.timetuple()))) and x['doc']['indexerid'] ]
def last_search(self, toDate, session=None): try: dbData = session.query(CacheDB.LastSearch).filter_by(provider=self.providerID).one() dbData.time = int(time.mktime(toDate.timetuple())) except orm.exc.NoResultFound: session.add(CacheDB.LastSearch(**{ 'provider': self.providerID, 'time': int(time.mktime(toDate.timetuple())) }))
def setUp(self): super(SiCKRAGETestDBCase, self).setUp() sickrage.app.main_db = MainDB() sickrage.app.cache_db = CacheDB() sickrage.app.failed_db = FailedDB() for db in [ sickrage.app.main_db, sickrage.app.cache_db, sickrage.app.failed_db ]: db.initialize()
def add_show(self, indexer_id): session = sickrage.app.cache_db.session() show = find_show(indexer_id) if indexer_id not in self.cache['shows']: sickrage.app.log.debug( "Adding show {} to QuickSearch cache".format(show.name)) qsData = { 'category': 'shows', 'showid': indexer_id, 'seasons': len(set([s.season for s in show.episodes])), 'name': show.name, 'img': sickrage.app.config.web_root + showImage(indexer_id, 'poster_thumb').url } self.cache['shows'][indexer_id] = qsData session.add(CacheDB.QuickSearchShow(**qsData)) session.commit() sql_t = [] for e in show.episodes: qsData = { 'category': 'episodes', 'showid': e.showid, 'episodeid': e.indexer_id, 'season': e.season, 'episode': e.episode, 'name': e.name, 'showname': show.name, 'img': sickrage.app.config.web_root + showImage(e.showid, 'poster_thumb').url } sql_t.append(qsData) self.cache['episodes'][e.indexer_id] = qsData session.bulk_insert_mappings(CacheDB.QuickSearchEpisode, sql_t) session.commit()
def last_search(self): try: dbData = CacheDB().db.get('lastSearch', self.providerID, with_doc=True)['doc'] lastTime = int(dbData["time"]) if lastTime > int( time.mktime(datetime.datetime.today().timetuple())): lastTime = 0 except RecordNotFound: lastTime = 0 return datetime.datetime.fromtimestamp(lastTime)
def setLastRefresh(exList): """ Update last cache update time for shows in list :param exList: exception list to set refresh time """ try: dbData = CacheDB().db.get('scene_exceptions_refresh', exList, with_doc=True)['doc'] dbData['last_refreshed'] = int( time.mktime(datetime.datetime.today().timetuple())) CacheDB().db.update(dbData) except RecordNotFound: CacheDB().db.insert({ '_t': 'scene_exceptions_refresh', 'last_refreshed': int(time.mktime(datetime.datetime.today().timetuple())), 'list': exList })
def last_search(self, toDate): session = sickrage.app.cache_db.session() try: dbData = session.query(CacheDB.LastSearch).filter_by(provider=self.providerID).one() dbData.time = int(time.mktime(toDate.timetuple())) except orm.exc.NoResultFound: session.add(CacheDB.LastSearch(**{ 'provider': self.providerID, 'time': int(time.mktime(toDate.timetuple())) })) finally: session.commit()
def set_last_refresh(ex_list, session=None): """ Update last cache update time for shows in list :param ex_list: exception list to set refresh time """ try: dbData = session.query(CacheDB.SceneExceptionRefresh).filter_by(exception_list=ex_list).one() dbData.last_refreshed = int(time.mktime(datetime.datetime.today().timetuple())) except orm.exc.NoResultFound: session.add(CacheDB.SceneExceptionRefresh(**{ 'last_refreshed': int(time.mktime(datetime.datetime.today().timetuple())), 'exception_list': ex_list }))
def setUp_test_db(force=False): """upgrades the db to the latest version """ # remove old db files tearDown_test_db() # upgrade main MainDB().initialize() # upgrade cache CacheDB().initialize() # upgrade failed FailedDB().initialize()
def update_scene_exceptions(indexer_id, scene_exceptions, season=-1): """ Given a indexer_id, and a list of all show scene exceptions, update the db. """ [ CacheDB().db.delete(x['doc']) for x in CacheDB().db.get_many( 'scene_exceptions', indexer_id, with_doc=True) if x['doc']['season'] == season ] sickrage.srCore.srLogger.info("Updating scene exceptions") # A change has been made to the scene exception list. Let's clear the cache, to make this visible if indexer_id in exceptionsCache: exceptionsCache[indexer_id] = {} exceptionsCache[indexer_id][season] = scene_exceptions for cur_exception in scene_exceptions: CacheDB().db.insert({ '_t': 'scene_exceptions', 'indexer_id': indexer_id, 'show_name': cur_exception, 'season': season })
def update_network_timezone(self, network, timezone): session = sickrage.app.cache_db.session() try: dbData = session.query( CacheDB.NetworkTimezone).filter_by(network_name=network).one() if dbData.timezone != timezone: dbData.timezone = timezone except orm.exc.NoResultFound: session.add( CacheDB.NetworkTimezone(**{ 'network_name': network, 'timezone': timezone })) finally: session.commit()
def shouldRefresh(exList): """ Check if we should refresh cache for items in exList :param exList: exception list to check if needs a refresh :return: True if refresh is needed """ MAX_REFRESH_AGE_SECS = 86400 # 1 day try: dbData = CacheDB().db.get('scene_exceptions_refresh', exList, with_doc=True)['doc'] lastRefresh = int(dbData['last_refreshed']) return int(time.mktime(datetime.datetime.today().timetuple()) ) > lastRefresh + MAX_REFRESH_AGE_SECS except RecordNotFound: return True
def get_all_scene_exceptions(indexer_id): """ Get all scene exceptions for a show ID :param indexer_id: ID to check :return: dict of exceptions """ exceptionsDict = {} for cur_exception in [ x['doc'] for x in CacheDB().db.get_many( 'scene_exceptions', indexer_id, with_doc=True) ]: if not cur_exception['season'] in exceptionsDict: exceptionsDict[cur_exception['season']] = [] exceptionsDict[cur_exception['season']].append( cur_exception['show_name']) return exceptionsDict
def update_network_timezones(self): """Update timezone information from SR repositories""" session = sickrage.app.cache_db.session() network_timezones = {} try: url_data = WebSession().get('https://cdn.sickrage.ca/network_timezones/').text except Exception: sickrage.app.log.warning('Updating network timezones failed.') return try: for line in url_data.splitlines(): (key, val) = line.strip().rsplit(':', 1) if all([key, val]): network_timezones[key] = val except (IOError, OSError): pass for x in session.query(CacheDB.NetworkTimezone): if x.network_name not in network_timezones: session.query(CacheDB.NetworkTimezone).filter_by(network_name=x.network_name).delete() session.commit() for network, timezone in network_timezones.items(): try: dbData = session.query(CacheDB.NetworkTimezone).filter_by(network_name=network).one() if dbData.timezone != timezone: dbData.timezone = timezone except orm.exc.NoResultFound: session.add(CacheDB.NetworkTimezone(**{ 'network_name': network, 'timezone': timezone })) finally: session.commit() # cleanup del network_timezones
def get_scene_seasons(indexer_id): """ return a list of season numbers that have scene exceptions """ exceptionsSeasonList = [] if indexer_id not in exceptionsSeasonCache: dbData = [ x['doc'] for x in CacheDB().db.get_many( 'scene_exceptions', indexer_id, with_doc=True) ] exceptionsSeasonList = list(set([int(x['season']) for x in dbData])) if not indexer_id in exceptionsSeasonCache: exceptionsSeasonCache[indexer_id] = {} exceptionsSeasonCache[indexer_id] = exceptionsSeasonList else: exceptionsSeasonList = exceptionsSeasonCache[indexer_id] return exceptionsSeasonList
def put(self, name, indexer_id=0, session=None): """ 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) """ # standardize the name we're using to account for small differences in providers name = full_sanitize_scene_name(name) self.cache[name] = int(indexer_id) try: session.query(CacheDB.SceneName).filter_by( name=name, indexer_id=indexer_id).one() except orm.exc.NoResultFound: session.add( CacheDB.SceneName(**{ 'indexer_id': indexer_id, 'name': name }))