From 2094f0d1d94e9cdad9f9b4ef4f13e215d2bf9d9b Mon Sep 17 00:00:00 2001 From: miigotu <miigotu@gmail.com> Date: Fri, 11 Dec 2015 08:41:46 -0800 Subject: [PATCH] Make sure subtitle last_search for an episode is convertible to datetime --- sickbeard/subtitles.py | 44 +++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/sickbeard/subtitles.py b/sickbeard/subtitles.py index 4a054bdcf..1cea7c106 100644 --- a/sickbeard/subtitles.py +++ b/sickbeard/subtitles.py @@ -396,7 +396,7 @@ class SubtitlesFinder(object): logger.log(u"Starting post-process with default settings now that we found subtitles") processTV.processDir(sickbeard.TV_DOWNLOAD_DIR) - def run(self, force=False): # pylint: disable=unused-argument + def run(self, force=False): # pylint: disable=unused-argument,too-many-statements,too-many-branches if not sickbeard.USE_SUBTITLES: return @@ -420,6 +420,13 @@ class SubtitlesFinder(object): # - search count < 2 and diff(airdate, now) > 1 week : now -> 1d # - search count < 7 and diff(airdate, now) <= 1 week : now -> 4h -> 8h -> 16h -> 1d -> 1d -> 1d + """ + Defines the hours to wait between 2 subtitles search depending on: + - the episode: new or old + - the number of searches done so far (searchcount), represented by the index of the list + """ + rules = {'old': [0, 24], 'new': [0, 4, 8, 4, 16, 24, 24]} + today = datetime.date.today().toordinal() database = db.DBConnection() @@ -437,7 +444,6 @@ class SubtitlesFinder(object): self.amActive = False return - rules = self._get_rules() now = datetime.datetime.now() for ep_to_sub in sql_results: @@ -453,36 +459,39 @@ class SubtitlesFinder(object): continue try: + try: + lastsearched = datetime.datetime.strptime(ep_to_sub['lastsearch'], dateTimeFormat) + except ValueError: + lastsearched = datetime.datetime.min + if ((ep_to_sub['airdate_daydiff'] > 7 and ep_to_sub['searchcount'] < 2 and - now - datetime.datetime.strptime(ep_to_sub['lastsearch'], dateTimeFormat) > - datetime.timedelta(hours=rules['old'][ep_to_sub['searchcount']])) or + now - lastsearched > datetime.timedelta(hours=rules['old'][ep_to_sub['searchcount']])) or (ep_to_sub['airdate_daydiff'] <= 7 and ep_to_sub['searchcount'] < 7 and - now - datetime.datetime.strptime(ep_to_sub['lastsearch'], dateTimeFormat) > - datetime.timedelta(hours=rules['new'][ep_to_sub['searchcount']]))): - + now - lastsearched > datetime.timedelta(hours=rules['new'][ep_to_sub['searchcount']]))): + logger.log(u'Downloading subtitles for %s S%02dE%02d' % (ep_to_sub['show_name'], ep_to_sub['season'], ep_to_sub['episode']), logger.DEBUG) - + show_object = Show.find(sickbeard.showList, int(ep_to_sub['showid'])) if not show_object: logger.log(u'Show with ID %s not found in the database' % ep_to_sub['showid'], logger.DEBUG) continue - + episode_object = show_object.getEpisode(int(ep_to_sub["season"]), int(ep_to_sub["episode"])) if isinstance(episode_object, str): logger.log(u'%s S%02dE%02d not found in the database' % (ep_to_sub['show_name'], ep_to_sub['season'], ep_to_sub['episode']), logger.DEBUG) continue - + existing_subtitles = episode_object.subtitles - + try: episode_object.download_subtitles() except Exception as error: logger.log(u'Unable to find subtitles for %s S%02dE%02d. Error: %r' % (ep_to_sub['show_name'], ep_to_sub['season'], ep_to_sub['episode'], ex(error)), logger.ERROR) continue - + new_subtitles = frozenset(episode_object.subtitles).difference(existing_subtitles) if new_subtitles: logger.log(u'Downloaded %s subtitles for %s S%02dE%02d' @@ -490,20 +499,11 @@ class SubtitlesFinder(object): ep_to_sub["season"], ep_to_sub["episode"])) except Exception as error: logger.log(u'Error while searching subtitles for %s S%02dE%02d. Error: %r' - % (ep_to_sub['show_name'], ep_to_sub['season'], ep_to_sub['episode'], ex(error)), logger.ERROR) + % (ep_to_sub['show_name'], ep_to_sub['season'], ep_to_sub['episode'], ex(error)), logger.ERROR) continue self.amActive = False - @staticmethod - def _get_rules(): - """ - Define the hours to wait between 2 subtitles search depending on: - - the episode: new or old - - the number of searches done so far (searchcount), represented by the index of the list - """ - return {'old': [0, 24], 'new': [0, 4, 8, 4, 16, 24, 24]} - def run_subs_extra_scripts(episode_object, found_subtitles, video, single=False): -- GitLab