From 7c32dc12e4202c919b2333c5471117a325e4a07b Mon Sep 17 00:00:00 2001 From: Prinz23 Date: Tue, 28 Feb 2023 18:12:11 +0000 Subject: [PATCH] Change support languages in show search. Add display all id links for persons. Fix ensure overview in search results are type str. Change pickle_protocol to 5 which is supported by 3.8+ Change update mock data to pickle protocol 5. Fix for if mock data files are corrupted. Change add property has_text to Images. Change set has_text from includesText data field (currently unused on API). Fix rebase errors. Change use Walrus for cache keys and get_data. --- gui/slick/interfaces/default/cast_person.tmpl | 2 +- lib/api_tvdb/tvdb_api_v4.py | 55 +++++++++---------- lib/tvinfo_base/base.py | 5 +- sickgear/indexers/indexer_api.py | 10 +++- tests/tvinfo_api_tests.py | 4 +- 5 files changed, 42 insertions(+), 34 deletions(-) diff --git a/gui/slick/interfaces/default/cast_person.tmpl b/gui/slick/interfaces/default/cast_person.tmpl index 30bf5b0c..b60de076 100644 --- a/gui/slick/interfaces/default/cast_person.tmpl +++ b/gui/slick/interfaces/default/cast_person.tmpl @@ -186,7 +186,7 @@ def param(visible=True, rid=None, cache_person=None, cache_char=None, person=Non #end if #set $section_links = False -#set $all_sources = $TVInfoAPI().all_sources +#set $all_sources = $TVInfoAPI().all_non_fallback_sources #for $cur_src, $cur_sid in sorted(iteritems($person.ids)) #if $cur_src not in $all_sources: #continue diff --git a/lib/api_tvdb/tvdb_api_v4.py b/lib/api_tvdb/tvdb_api_v4.py index 43326712..92b65b05 100644 --- a/lib/api_tvdb/tvdb_api_v4.py +++ b/lib/api_tvdb/tvdb_api_v4.py @@ -41,6 +41,7 @@ log.addHandler(logging.NullHandler()) TVDB_API_CONFIG = {} +NoneType = type(None) # always use https in cases of redirects # noinspection PyUnusedLocal,HttpUrlsUsage @@ -248,10 +249,7 @@ class TvdbAPIv4(TVInfoBase): def _get_data(self, endpoint, **kwargs): # type: (string_types, Any) -> Any - is_series_info, retry = endpoint.startswith('/series/'), kwargs.pop('token_retry', 1) - if retry > 3: - raise TvdbTokenFailure('Failed to get new token') - if is_series_info: + if is_series_info := endpoint.startswith('/series/'): self.show_not_found = False try: return tvdb_endpoint_get(url='%s%s' % (self.base_url, endpoint), params=kwargs, parse_json=True, @@ -368,8 +366,7 @@ class TvdbAPIv4(TVInfoBase): """ if not p_id: return - cache_key_name = 'p-v4-%s' % p_id - is_none, people_obj = self._get_cache_entry(cache_key_name) + is_none, people_obj = self._get_cache_entry(cache_key_name := 'p-v4-%s' % p_id) if None is people_obj and not is_none: resp = self._get_data('/people/%s/extended' % p_id) self._set_cache_entry(cache_key_name, resp) @@ -396,8 +393,7 @@ class TvdbAPIv4(TVInfoBase): result.append(r) if tv_src in (TVINFO_IMDB, TVINFO_TMDB, TVINFO_TVMAZE): _src = tv_src - cache_id_key = 'p-v4-id-%s-%s' % (_src, ids[_src]) - is_none, shows = self._get_cache_entry(cache_id_key) + is_none, shows = self._get_cache_entry(cache_id_key := 'p-v4-id-%s-%s' % (_src, ids[_src])) d_m = None if not self.config.get('cache_search') or (None is shows and not is_none): try: @@ -426,8 +422,7 @@ class TvdbAPIv4(TVInfoBase): result.extend(self._convert_person(r['people'], ids)) break if name: - cache_key_name = 'p-v4-src-text-%s' % name - is_none, people_objs = self._get_cache_entry(cache_key_name) + is_none, people_objs = self._get_cache_entry(cache_key_name := 'p-v4-src-text-%s' % name) if None is people_objs and not is_none: resp = self._get_data('/search', query=name, type='people') self._set_cache_entry(cache_key_name, resp) @@ -496,10 +491,13 @@ class TvdbAPIv4(TVInfoBase): def _get_series_name(self, show_data, language=None): # type: (Dict, AnyStr) -> Tuple[Optional[AnyStr], List] - series_name = clean_data( - next(filter(lambda l: language and language == l['language'], - show_data.get('translations', {}).get('nameTranslations', [])), - {'name': show_data['name']})['name']) + if 'nameTranslations' in show_data.get('translations', {}): + series_name = clean_data( + next(filter(lambda l: language and language == l['language'], + show_data.get('translations', {}).get('nameTranslations', [])), + {'name': show_data['name']})['name']) + else: + series_name = clean_data(show_data.get('translations', {}).get(language, show_data['name'])) series_aliases = self._get_aliases(show_data) if not series_name: if isinstance(series_aliases, list) and 0 < len(series_aliases): @@ -627,11 +625,11 @@ class TvdbAPIv4(TVInfoBase): elif TVINFO_WIKIDATA == src_type or 'wikidata' in src_name: social_ids['wikidata'] = src_value elif TVINFO_TIKTOK == src_type or 'tiktok' in src_name: - social_ids[TVINFO_TIKTOK] = src_value + social_ids['tiktok'] = src_value elif TVINFO_LINKEDIN == src_type: - social_ids[TVINFO_LINKEDIN] = src_value + social_ids['linkedin'] = src_value elif TVINFO_FANSITE == src_type: - social_ids[TVINFO_FANSITE] = src_value + social_ids['fansite'] = src_value ti_show.ids = TVInfoIDs(tvdb=show_data['id'], **ids) if social_ids: @@ -657,7 +655,8 @@ class TvdbAPIv4(TVInfoBase): img_id=artwork['id'], lang=artwork['language'], rating=artwork['score'], - updated_at=artwork['updatedAt'] or None + updated_at=artwork['updatedAt'] or None, + has_text=enforce_type(artwork.get('includesText'), (bool, NoneType), None) ) ) @@ -1100,7 +1099,7 @@ class TvdbAPIv4(TVInfoBase): # noinspection DuplicatedCode if not self.config.get('cache_search') or (None is shows and not is_none): try: - d_m = self._get_show_data(ids.get(TVINFO_TVDB), self.config['language'], direct_data=True) + d_m = self._get_show_data(ids.get(TVINFO_TVDB), lang, direct_data=True) self._set_cache_entry(cache_id_key, d_m, expire=self.search_cache_expire) except (BaseException, Exception): d_m = None @@ -1110,12 +1109,10 @@ class TvdbAPIv4(TVInfoBase): and isinstance(d_m['data'], dict): results.extend(_make_result_dict(d_m['data'])) - if ids.get(TVINFO_IMDB): - cache_id_key = 's-v4-id-%s-%s' % (TVINFO_IMDB, ids[TVINFO_IMDB]) - is_none, shows = self._get_cache_entry(cache_id_key) + is_none, shows = self._get_cache_entry(cache_id_key := 's-v4-id-%s-%s' % (cur_tvinfo, ids[cur_tvinfo])) if not self.config.get('cache_search') or (None is shows and not is_none): try: - d_m = self._get_data('search', remote_id='tt%07d' % ids.get(TVINFO_IMDB), + d_m = self._get_data('search?meta=translations', remote_id='tt%07d' % ids.get(TVINFO_IMDB), query='tt%07d' % ids.get(TVINFO_IMDB), type='series') self._set_cache_entry(cache_id_key, d_m, expire=self.search_cache_expire) except (BaseException, Exception): @@ -1139,7 +1136,7 @@ class TvdbAPIv4(TVInfoBase): is_none, shows = self._get_cache_entry(cache_id_key) if not self.config.get('cache_search') or (None is shows and not is_none): try: - d_m = self._get_data('search', remote_id='%s' % ids.get(TVINFO_TMDB), + d_m = self._get_data('search?meta=translations', remote_id='%s' % ids.get(TVINFO_TMDB), query='%s' % ids.get(TVINFO_TMDB), type='series') self._set_cache_entry(cache_id_key, d_m, expire=self.search_cache_expire) except (BaseException, Exception): @@ -1163,7 +1160,7 @@ class TvdbAPIv4(TVInfoBase): is_none, shows = self._get_cache_entry(cache_id_key) if not self.config.get('cache_search') or (None is shows and not is_none): try: - d_m = self._get_data('search', remote_id='%s' % ids.get(TVINFO_TVMAZE), + d_m = self._get_data('search?meta=translations', remote_id='%s' % ids.get(TVINFO_TVMAZE), query='%s' % ids.get(TVINFO_TVMAZE), type='series') self._set_cache_entry(cache_id_key, d_m, expire=self.search_cache_expire) except (BaseException, Exception): @@ -1183,11 +1180,11 @@ class TvdbAPIv4(TVInfoBase): pass if ids.get(TVINFO_TVDB_SLUG) and isinstance(ids.get(TVINFO_TVDB_SLUG), string_types): - cache_id_key = 's-id-%s-%s' % (TVINFO_TVDB, ids[TVINFO_TVDB_SLUG]) - is_none, shows = self._get_cache_entry(cache_id_key) + is_none, shows = self._get_cache_entry(cache_id_key := + 's-id-%s-%s' % (TVINFO_TVDB, ids[TVINFO_TVDB_SLUG])) if not self.config.get('cache_search') or (None is shows and not is_none): try: - d_m = self._get_data('/series/slug/%s' % ids.get(TVINFO_TVDB_SLUG)) + d_m = self._get_data('/series/slug/%s?meta=translations' % ids.get(TVINFO_TVDB_SLUG)) self._set_cache_entry(cache_id_key, d_m, expire=self.search_cache_expire) except (BaseException, Exception): d_m = None @@ -1202,7 +1199,7 @@ class TvdbAPIv4(TVInfoBase): cache_name_key = 's-v4-name-%s' % n is_none, shows = self._get_cache_entry(cache_name_key) if not self.config.get('cache_search') or (None is shows and not is_none): - resp = self._get_data('search', query=n, type='series') + resp = self._get_data('search?meta=translations', query=n, type='series') self._set_cache_entry(cache_name_key, resp, expire=self.search_cache_expire) else: resp = shows diff --git a/lib/tvinfo_base/base.py b/lib/tvinfo_base/base.py index 9b148c44..4662a227 100644 --- a/lib/tvinfo_base/base.py +++ b/lib/tvinfo_base/base.py @@ -329,7 +329,7 @@ class TVInfoImageSize(object): class TVInfoImage(object): def __init__(self, image_type, sizes, img_id=None, main_image=False, type_str='', rating=None, votes=None, - lang=None, height=None, width=None, aspect_ratio=None, updated_at=None): + lang=None, height=None, width=None, aspect_ratio=None, updated_at=None, has_text=None): self.img_id = img_id # type: Optional[integer_types] self.image_type = image_type # type: integer_types self.sizes = sizes # type: Union[TVInfoImageSize, Dict] @@ -341,6 +341,7 @@ class TVInfoImage(object): self.height = height # type: Optional[integer_types] self.width = width # type: Optional[integer_types] self.aspect_ratio = aspect_ratio # type: Optional[Union[float, integer_types]] + self.has_text = has_text # type: Optional[bool] self.updated_at = updated_at # type: Optional[integer_types] def __eq__(self, other): @@ -1370,7 +1371,7 @@ class TVInfoBase(object): if None is lang: if self.config.get('language'): lang = self.config['language'] - lang = self.map_languages.get(lang, lang) + lang = self.map_languages.get(lang, lang) if not name and not ids: log.debug('Nothing to search') raise BaseTVinfoShownotfound('Nothing to search') diff --git a/sickgear/indexers/indexer_api.py b/sickgear/indexers/indexer_api.py index d87c0602..ef618b8a 100644 --- a/sickgear/indexers/indexer_api.py +++ b/sickgear/indexers/indexer_api.py @@ -100,11 +100,19 @@ class TVInfoAPI(object): def all_sources(self): # type: () -> Dict[int, AnyStr] """ - :return: return all indexers including mapped only indexers excluding fallback indexers + :return: return all indexers for show data including mapped only indexers excluding fallback indexers """ return self._filter(lambda x: True is not x.get('fallback') and True is not x.get('people_only')) + @property + def all_non_fallback_sources(self): + # type: (...) -> Dict[int, AnyStr] + """ + return all sources with the exclusion of fallback indexer + """ + return self._filter(lambda x: True is not x.get('fallback')) + @property def fallback_sources(self): # type: () -> Dict[int, AnyStr] diff --git a/tests/tvinfo_api_tests.py b/tests/tvinfo_api_tests.py index 0d2bc556..80a41947 100644 --- a/tests/tvinfo_api_tests.py +++ b/tests/tvinfo_api_tests.py @@ -52,7 +52,7 @@ only_new_urls_data_creation = True delete_unused_mock_files = False # other settings -pickle_protocol = 3 # needed for python 3.7 compatibility +pickle_protocol = 5 # needed for python 3.8 compatibility used_files = {'browse_start_date.data'} @@ -153,6 +153,8 @@ def _load_pickle_file(f_name): try: with lzma.open(full_filename, 'rb') as f: return pickle.load(f) + except (BaseException, Exception): + return finally: datetime.date = _FakeDate datetime.datetime = _FakeDateTime