From 406c9ad6c6205aee5ba03701a50d15bc7afb4058 Mon Sep 17 00:00:00 2001 From: Prinz23 Date: Tue, 31 Oct 2017 20:35:57 +0000 Subject: [PATCH] Change reduce number of DB calls for extra_info_no_name. Add parse repack, proper level to recent search flow. --- CHANGES.md | 2 + sickbeard/history.py | 2 +- sickbeard/name_parser/parser.py | 66 ++++++++++++++++----------------- sickbeard/postProcessor.py | 8 ++-- sickbeard/properFinder.py | 18 ++++----- sickbeard/providers/generic.py | 2 +- sickbeard/tvcache.py | 12 ++++++ tests/common_tests.py | 2 +- 8 files changed, 63 insertions(+), 49 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 13dc44ed..561b4f57 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -150,6 +150,8 @@ * Change prevent setting show/episode attr to None from indexer data * Fix article link color on some page were changed blue * Fix error after SG is updated and where Slack notifier is not enabled +* Change reduce number of DB calls for extra_info_no_name +* Add parse repack, proper level to recent search flow ### 0.12.35 (2017-10-27 20:30:00 UTC) diff --git a/sickbeard/history.py b/sickbeard/history.py index fd26f8a6..c25f63e0 100644 --- a/sickbeard/history.py +++ b/sickbeard/history.py @@ -155,7 +155,7 @@ def history_snatched_proper_fix(): pr = np.parse(r['resource']) except (StandardError, Exception): continue - if 0 < Quality.get_proper_level(pr.extra_info_no_name, pr.version, pr.is_anime): + if 0 < Quality.get_proper_level(pr.extra_info_no_name(), pr.version, pr.is_anime): cl.append(['UPDATE history SET action = ? WHERE rowid = ?', [Quality.compositeStatus(SNATCHED_PROPER, int(r['quality'])), r['rowid']]]) diff --git a/sickbeard/name_parser/parser.py b/sickbeard/name_parser/parser.py index 3ab7c8a5..f2357c6b 100644 --- a/sickbeard/name_parser/parser.py +++ b/sickbeard/name_parser/parser.py @@ -472,32 +472,6 @@ class NameParser(object): return number - @staticmethod - def _replace_ep_name_helper(e_i_n_n, n): - ep_regex = r'\W*%s\W*' % re.sub(r' ', r'\W', re.sub(r'[^a-zA-Z0-9 ]', r'\W?', - re.sub(r'\W+$', '', n.strip()))) - if None is regex: - return re.sub(ep_regex, '', e_i_n_n, flags=re.I) - - return regex.sub(r'(%s){e<=%d}' % ( - ep_regex, trunc(len(re.findall(r'\w', ep_regex)) / 5)), '', e_i_n_n, flags=regex.I | regex.B) - - def _extra_info_no_name(self, extra_info, show, season, episodes): - extra_info_no_name = extra_info - if isinstance(extra_info_no_name, basestring) and show and hasattr(show, 'indexer'): - for e in episodes: - if not hasattr(show, 'getEpisode'): - continue - ep = show.getEpisode(season, e) - if ep and isinstance(getattr(ep, 'name', None), basestring) and ep.name.strip(): - extra_info_no_name = self._replace_ep_name_helper(extra_info_no_name, ep.name) - if hasattr(show, 'getAllEpisodes'): - for e in [ep.name for ep in show.getAllEpisodes(check_related_eps=False) if getattr(ep, 'name', None) - and re.search(r'real|proper|repack', ep.name, re.I)]: - extra_info_no_name = self._replace_ep_name_helper(extra_info_no_name, e) - - return extra_info_no_name - def parse(self, name, cache_result=True): name = self._unicodify(name) @@ -558,10 +532,6 @@ class NameParser(object): final_result.show = self._combine_results(file_name_result, dir_name_result, 'show') final_result.quality = self._combine_results(file_name_result, dir_name_result, 'quality') - final_result.extra_info_no_name = self._extra_info_no_name(final_result.extra_info, final_result.show, - final_result.season_number, - final_result.episode_numbers) - if not final_result.show: if self.testing: pass @@ -594,8 +564,7 @@ class ParseResult(object): show=None, score=None, quality=None, - version=None, - extra_info_no_name=None): + version=None): self.original_name = original_name @@ -617,7 +586,7 @@ class ParseResult(object): self.quality = quality self.extra_info = extra_info - self.extra_info_no_name = extra_info_no_name + self._extra_info_no_name = None self.release_group = release_group self.air_date = air_date @@ -677,6 +646,37 @@ class ParseResult(object): return to_return.encode('utf-8') + @staticmethod + def _replace_ep_name_helper(e_i_n_n, n): + ep_regex = r'\W*%s\W*' % re.sub(r' ', r'\W', re.sub(r'[^a-zA-Z0-9 ]', r'\W?', + re.sub(r'\W+$', '', n.strip()))) + if None is regex: + return re.sub(ep_regex, '', e_i_n_n, flags=re.I) + + return regex.sub(r'(%s){e<=%d}' % ( + ep_regex, trunc(len(re.findall(r'\w', ep_regex)) / 5)), '', e_i_n_n, flags=regex.I | regex.B) + + def get_extra_info_no_name(self): + extra_info_no_name = self.extra_info + if isinstance(extra_info_no_name, basestring) and self.show and hasattr(self.show, 'indexer'): + for e in self.episode_numbers: + if not hasattr(self.show, 'getEpisode'): + continue + ep = self.show.getEpisode(self.season_number, e) + if ep and isinstance(getattr(ep, 'name', None), basestring) and ep.name.strip(): + extra_info_no_name = self._replace_ep_name_helper(extra_info_no_name, ep.name) + if hasattr(self.show, 'getAllEpisodes'): + for e in [ep.name for ep in self.show.getAllEpisodes(check_related_eps=False) if getattr(ep, 'name', None) + and re.search(r'real|proper|repack', ep.name, re.I)]: + extra_info_no_name = self._replace_ep_name_helper(extra_info_no_name, e) + + return extra_info_no_name + + def extra_info_no_name(self): + if None is self._extra_info_no_name and None is not self.extra_info: + self._extra_info_no_name = self.get_extra_info_no_name() + return self._extra_info_no_name + @property def is_air_by_date(self): if self.air_date: diff --git a/sickbeard/postProcessor.py b/sickbeard/postProcessor.py index 0f6c55e9..2a26856c 100644 --- a/sickbeard/postProcessor.py +++ b/sickbeard/postProcessor.py @@ -504,8 +504,8 @@ class PostProcessor(object): self.release_group = parse_result.release_group # remember whether it's a proper - if parse_result.extra_info_no_name: - self.is_proper = 0 < common.Quality.get_proper_level(parse_result.extra_info_no_name, parse_result.version, + if parse_result.extra_info_no_name(): + self.is_proper = 0 < common.Quality.get_proper_level(parse_result.extra_info_no_name(), parse_result.version, parse_result.is_anime) # if the result is complete then set release name @@ -780,7 +780,7 @@ class PostProcessor(object): cur_proper_level = 0 try: pr = np.parse(ep_obj.release_name) - cur_proper_level = common.Quality.get_proper_level(pr.extra_info_no_name, pr.version, pr.is_anime) + cur_proper_level = common.Quality.get_proper_level(pr.extra_info_no_name(), pr.version, pr.is_anime) except (StandardError, Exception): pass new_name = (('', self.file_name)[isinstance(self.file_name, basestring)], self.nzb_name)[isinstance( @@ -791,7 +791,7 @@ class PostProcessor(object): except (StandardError, Exception): npr = None if npr: - is_repack, new_proper_level = common.Quality.get_proper_level(npr.extra_info_no_name, npr.version, + is_repack, new_proper_level = common.Quality.get_proper_level(npr.extra_info_no_name(), npr.version, npr.is_anime, check_is_repack=True) if new_proper_level > cur_proper_level and \ (not is_repack or npr.release_group == ep_obj.release_group): diff --git a/sickbeard/properFinder.py b/sickbeard/properFinder.py index 48718679..606c845d 100644 --- a/sickbeard/properFinder.py +++ b/sickbeard/properFinder.py @@ -97,9 +97,9 @@ def get_old_proper_level(showObj, indexer, indexerid, season, episodes, old_stat p = np.parse(result[0]['resource']) except (StandardError, Exception): continue - level = Quality.get_proper_level(p.extra_info_no_name, p.version, showObj.is_anime) - is_internal = p.extra_info_no_name and re.search(r'\binternal\b', p.extra_info_no_name, flags=re.I) - codec = _get_codec(p.extra_info_no_name) + level = Quality.get_proper_level(p.extra_info_no_name(), p.version, showObj.is_anime) + is_internal = p.extra_info_no_name() and re.search(r'\binternal\b', p.extra_info_no_name(), flags=re.I) + codec = _get_codec(p.extra_info_no_name()) break return level, is_internal, codec @@ -158,13 +158,13 @@ def _get_proper_list(aired_since_shows, recent_shows, recent_anime): logger.log(u'Found new proper: ' + x.name, logger.DEBUG) x.show = parse_result.show.indexerid x.provider = cur_provider - x.is_repack, x.properlevel = Quality.get_proper_level(parse_result.extra_info_no_name, + x.is_repack, x.properlevel = Quality.get_proper_level(parse_result.extra_info_no_name(), parse_result.version, parse_result.is_anime, check_is_repack=True) - x.is_internal = parse_result.extra_info_no_name and \ - re.search(r'\binternal\b', parse_result.extra_info_no_name, flags=re.I) - x.codec = _get_codec(parse_result.extra_info_no_name) + x.is_internal = parse_result.extra_info_no_name() and \ + re.search(r'\binternal\b', parse_result.extra_info_no_name(), flags=re.I) + x.codec = _get_codec(parse_result.extra_info_no_name()) propers[name] = x count += 1 except (InvalidNameException, InvalidShowException): @@ -237,7 +237,7 @@ def _get_proper_list(aired_since_shows, recent_shows, recent_anime): # only keep the proper if we have already retrieved the same quality ep (don't get better/worse ones) # don't take proper of the same level we already downloaded old_status, old_quality = Quality.splitCompositeStatus(int(sql_results[0]['status'])) - cur_proper.is_repack, cur_proper.proper_level = Quality.get_proper_level(cur_proper.extra_info_no_name, + cur_proper.is_repack, cur_proper.proper_level = Quality.get_proper_level(cur_proper.extra_info_no_name(), cur_proper.version, cur_proper.is_anime, check_is_repack=True) @@ -252,7 +252,7 @@ def _get_proper_list(aired_since_shows, recent_shows, recent_anime): np = NameParser(False, try_scene_exceptions=True, showObj=parse_result.show, indexer_lookup=False) try: - extra_info = np.parse(sql_results[0]['release_name']).extra_info_no_name + extra_info = np.parse(sql_results[0]['release_name']).extra_info_no_name() except (StandardError, Exception): extra_info = None diff --git a/sickbeard/providers/generic.py b/sickbeard/providers/generic.py index 1692a843..a6dd5ec8 100644 --- a/sickbeard/providers/generic.py +++ b/sickbeard/providers/generic.py @@ -589,7 +589,7 @@ class GenericProvider: result.content = None result.version = version result.size, result.puid = self.get_size_uid(item, **kwargs) - result.is_repack, result.properlevel = Quality.get_proper_level(parse_result.extra_info_no_name, + result.is_repack, result.properlevel = Quality.get_proper_level(parse_result.extra_info_no_name(), parse_result.version, show_obj.is_anime, check_is_repack=True) diff --git a/sickbeard/tvcache.py b/sickbeard/tvcache.py index f2b7dd1b..d7ec58f6 100644 --- a/sickbeard/tvcache.py +++ b/sickbeard/tvcache.py @@ -342,6 +342,18 @@ class TVCache: result.release_group = curReleaseGroup result.version = curVersion result.content = None + np = NameParser(False, showObj=showObj) + try: + parsed_result = np.parse(title) + extra_info_no_name = parsed_result.extra_info_no_name() + version = parsed_result.version + is_anime = parsed_result.is_anime + except (StandardError, Exception): + extra_info_no_name = None + version = -1 + is_anime = False + result.is_repack, result.properlevel = Quality.get_proper_level(extra_info_no_name, version, is_anime, + check_is_repack=True) # add it to the list if epObj not in neededEps: diff --git a/tests/common_tests.py b/tests/common_tests.py index 1b178f4b..0fdd987d 100644 --- a/tests/common_tests.py +++ b/tests/common_tests.py @@ -19,7 +19,7 @@ class QualityTests(unittest.TestCase): np = NameParser(False, indexer_lookup=False, try_scene_exceptions=False, testing=True) for case, level in cases: p = np.parse(case) - second = common.Quality.get_proper_level(p.extra_info_no_name, p.version, is_anime) + second = common.Quality.get_proper_level(p.extra_info_no_name(), p.version, is_anime) self.assertEqual(level, second, 'fail %s != %s for case: %s' % (level, second, case)) # TODO: repack / proper ? air-by-date ? season rip? multi-ep?