From 2c1c76885a6d708a2864f17e4624cb2a674436a7 Mon Sep 17 00:00:00 2001 From: Prinz23 Date: Sun, 21 May 2023 17:38:27 +0200 Subject: [PATCH] Fix detect broken api episode endpoint and use fallback. Fix detect correct old api endpoint pagination and use it. --- CHANGES.md | 1 + lib/api_tvdb/tvdb_api.py | 34 ++++++++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 778d6bb6..4f93d648 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -25,6 +25,7 @@ * Add menu Shows/"TMDB Cards" * Add a persons available socials (Youtube, LinkedIn, Reddit, Fansite, TikTok, Wikidata) * Change use TVDb genres on view-show if config/General/Interface/"Enable IMDb info" is disabled +* Fix TVDb api episode issues * Change remove Python 3.7 from CI diff --git a/lib/api_tvdb/tvdb_api.py b/lib/api_tvdb/tvdb_api.py index 5936a480..e365fb7b 100644 --- a/lib/api_tvdb/tvdb_api.py +++ b/lib/api_tvdb/tvdb_api.py @@ -1106,14 +1106,27 @@ class Tvdb(TVInfoBase): page = 0 # type: int episodes = [] # type: list episode_data_found = False # type: bool + episode_data_broken = False # type: bool + page_count = 0 # type: int + pages_loaded = 0 # type: int + start_page = 0 # type: int while page <= 400: episode_data = {} - if self.is_apikey(): + if self.is_apikey() and not episode_data_broken: episode_data = self._getetsrc( self.config['url_series_episodes_info'] % (sid, page), language=language) - episode_data_found |= 0 == page and bool(episode_data) + # fallback to correct old pagination + if 0 == page and None is episode_data: + page = 1 + continue + if episode_data: + if 1 == page and not bool(episodes): + start_page = 1 + pages_loaded += 1 + episode_data_found |= start_page == page and bool(episode_data) - if not episode_data_found and isinstance(show_data, dict) and 'slug' in show_data: + if episode_data_broken or \ + (not episode_data_found and isinstance(show_data, dict) and 'slug' in show_data): response = {'data': None} items_found = False # fallback to page 'all' if dvd is enabled and response has no items @@ -1176,12 +1189,16 @@ class Tvdb(TVInfoBase): continue if episode_data_found and not episode_data: - break + if pages_loaded < page_count or 0 == page_count: + episode_data_broken = True + continue + else: + break if None is episode_data and not bool(episodes) and not episode_data_found: raise TvdbError('Exception retrieving episodes for show') if isinstance(episode_data, dict) and not episode_data.get('data', []): - if 0 != page: + if start_page != page: self.not_found = False break if not getattr(self, 'not_found', False) and None is not episode_data.get('data'): @@ -1190,11 +1207,16 @@ class Tvdb(TVInfoBase): # check if page is a valid following page if not isinstance(next_link, integer_types) or next_link <= page: next_link = None + if isinstance(episode_data, dict) and 'links' in episode_data \ + and isinstance(episode_data['links'], dict) and 'last' in episode_data['links'] \ + and isinstance(episode_data['links']['last'], int) \ + and episode_data['links']['last'] > page_count: + page_count = episode_data['links']['last'] if not next_link and isinstance(episode_data, dict) \ and isinstance(episode_data.get('data', []), list) and \ (100 > len(episode_data.get('data', [])) or episode_data.get('fallback')): break - if next_link: + if isinstance(next_link, int) and page + 1 == next_link: page = next_link else: page += 1