# Author: Nic Wolfe # URL: http://code.google.com/p/sickbeard/ # # This file is part of SickGear. # # SickGear is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # SickGear is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with SickGear. If not, see . import re import time import threading import datetime import sickbeard import traceback from collections import defaultdict from lib import adba from sickbeard import helpers from sickbeard import name_cache from sickbeard import logger from sickbeard import db from sickbeard.classes import OrderedDefaultdict exception_dict = {} anidb_exception_dict = {} xem_exception_dict = {} xem_ids_list = defaultdict(list) exceptionsCache = {} exceptionsSeasonCache = {} exceptionLock = threading.Lock() def shouldRefresh(list): max_refresh_age_secs = 86400 # 1 day my_db = db.DBConnection() rows = my_db.select('SELECT last_refreshed FROM scene_exceptions_refresh WHERE list = ?', [list]) if rows: last_refresh = int(rows[0]['last_refreshed']) return int(time.mktime(datetime.datetime.today().timetuple())) > last_refresh + max_refresh_age_secs else: return True def setLastRefresh(list): my_db = db.DBConnection() my_db.upsert('scene_exceptions_refresh', {'last_refreshed': int(time.mktime(datetime.datetime.today().timetuple()))}, {'list': list}) def get_scene_exceptions(indexer_id, season=-1): """ Given a indexer_id, return a list of all the scene exceptions. """ global exceptionsCache exceptions_list = [] if indexer_id not in exceptionsCache or season not in exceptionsCache[indexer_id]: my_db = db.DBConnection() exceptions = my_db.select('SELECT show_name FROM scene_exceptions WHERE indexer_id = ? and season = ?', [indexer_id, season]) if exceptions: exceptions_list = list(set([cur_exception['show_name'] for cur_exception in exceptions])) if indexer_id not in exceptionsCache: exceptionsCache[indexer_id] = {} exceptionsCache[indexer_id][season] = exceptions_list else: exceptions_list = exceptionsCache[indexer_id][season] if 1 == season: # if we where looking for season 1 we can add generic names exceptions_list += get_scene_exceptions(indexer_id, season=-1) return exceptions_list def get_all_scene_exceptions(indexer_id): exceptions_dict = OrderedDefaultdict(list) my_db = db.DBConnection() exceptions = my_db.select('SELECT show_name,season FROM scene_exceptions WHERE indexer_id = ? ORDER BY season', [indexer_id]) if exceptions: for cur_exception in exceptions: exceptions_dict[cur_exception['season']].append(cur_exception['show_name']) return exceptions_dict def get_scene_seasons(indexer_id): """ return a list of season numbers that have scene exceptions """ global exceptionsSeasonCache exception_sseason_list = [] if indexer_id not in exceptionsSeasonCache: my_db = db.DBConnection() sql_results = my_db.select('SELECT DISTINCT(season) as season FROM scene_exceptions WHERE indexer_id = ?', [indexer_id]) if sql_results: exception_sseason_list = list(set([int(x['season']) for x in sql_results])) if indexer_id not in exceptionsSeasonCache: exceptionsSeasonCache[indexer_id] = {} exceptionsSeasonCache[indexer_id] = exception_sseason_list else: exception_sseason_list = exceptionsSeasonCache[indexer_id] return exception_sseason_list def get_scene_exception_by_name(show_name): return get_scene_exception_by_name_multiple(show_name)[0] 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 retrieve_exceptions(): """ Looks up the exceptions on github, parses them into a dict, and inserts them into the scene_exceptions table in cache.db. Also clears the scene name cache. """ global exception_dict, anidb_exception_dict, xem_exception_dict # exceptions are stored on github pages for indexer in sickbeard.indexerApi().indexers: if shouldRefresh(sickbeard.indexerApi(indexer).name): logger.log(u'Checking for scene exception updates for %s' % sickbeard.indexerApi(indexer).name) url = sickbeard.indexerApi(indexer).config['scene_url'] url_data = helpers.getURL(url) if None is url_data: # When None is urlData, trouble connecting to github logger.log(u'Check scene exceptions update failed. Unable to get URL: %s' % url, logger.ERROR) continue else: setLastRefresh(sickbeard.indexerApi(indexer).name) # each exception is on one line with the format indexer_id: 'show name 1', 'show name 2', etc for cur_line in url_data.splitlines(): cur_line = cur_line.decode('utf-8') indexer_id, sep, aliases = cur_line.partition(':') # @UnusedVariable if not aliases: continue indexer_id = int(indexer_id) # regex out the list of shows, taking \' into account # alias_list = [re.sub(r'\\(.)', r'\1', x) for x in re.findall(r"'(.*?)(?