Skip to content

Commit

Permalink
refactor: optimize function gathering to avoid 429 error from Trakt API
Browse files Browse the repository at this point in the history
+ fix: only_upcoming sensor (#90)
  • Loading branch information
Anrolosia authored Jan 3, 2024
1 parent 9cdccda commit 2728985
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
48 changes: 33 additions & 15 deletions custom_components/trakt_tv/apis/trakt.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ def is_show_finished(self, show) -> bool:
except KeyError:
return False

async def fetch_watched(self, excluded_shows: list):
async def fetch_watched(
self, excluded_shows: list, excluded_finished: bool = False
):
"""First, let's retrieve hidden items from user as a workaround for a potential bug in show progress_watch API"""
cache_key = f"user_hidden_shows"

Expand Down Expand Up @@ -176,7 +178,8 @@ async def fetch_watched(self, excluded_shows: list):
raw_show_progress = await self.fetch_show_progress(ids["trakt"])
is_finished = self.is_show_finished(raw_show_progress)

if is_finished:
"""aired date and completed date will always be the same for next to watch tvshows if you're up-to-date"""
if excluded_finished and is_finished:
continue

raw_next_episode = await self.fetch_show_informations(
Expand Down Expand Up @@ -262,7 +265,7 @@ async def fetch_upcoming(

if next_to_watch:
excluded_shows = configuration.get_exclude_shows(identifier)
raw_medias = await self.fetch_watched(excluded_shows)
raw_medias = await self.fetch_watched(excluded_shows, not only_upcoming)
else:
days_to_fetch = configuration.get_upcoming_days_to_fetch(
identifier, all_medias
Expand Down Expand Up @@ -357,22 +360,37 @@ async def fetch_recommendations(self):

async def retrieve_data(self):
async with timeout(1800):
titles = [
titles = []
data_function = []
configuration = Configuration(data=self.hass.data)
source_function = {
"upcoming": self.fetch_upcomings(all_medias=False),
"all_upcoming": self.fetch_upcomings(all_medias=True),
"recommendation": self.fetch_recommendations(),
"all": self.fetch_next_to_watch(),
"only_aired": self.fetch_next_to_watch(only_aired=True),
"only_upcoming": self.fetch_next_to_watch(only_upcoming=True),
}

"""First, let's configure which sensors we need depending on configuration"""
for source in [
"upcoming",
"all_upcoming",
"recommendation",
]:
if configuration.source_exists(source):
titles.append(source)
data_function.append(source_function.get(source))

"""Then, let's add the next to watch sensors if needed"""
for identifier in [
"all",
"only_aired",
"only_upcoming",
]
data = await gather(
*[
self.fetch_upcomings(all_medias=False),
self.fetch_upcomings(all_medias=True),
self.fetch_recommendations(),
self.fetch_next_to_watch(),
self.fetch_next_to_watch(only_aired=True),
self.fetch_next_to_watch(only_upcoming=True),
]
)
]:
if configuration.next_to_watch_identifier_exists(identifier):
titles.append(identifier)
data_function.append(source_function.get(identifier))

data = await gather(*data_function)
return {title: medias for title, medias in zip(titles, data)}
7 changes: 7 additions & 0 deletions custom_components/trakt_tv/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,10 @@ def recommendation_identifier_exists(self, identifier: str) -> bool:

def get_recommendation_max_medias(self, identifier: str) -> int:
return self.get_max_medias(identifier, "recommendation")

def source_exists(self, source: str) -> bool:
try:
self.conf["sensors"][source]
return True
except KeyError:
return False

0 comments on commit 2728985

Please sign in to comment.