Skip to content

Commit 0372c22

Browse files
refactor: use parallel execution to fetch recommendations from tmdb
1 parent 51900ee commit 0372c22

File tree

3 files changed

+19
-23
lines changed

3 files changed

+19
-23
lines changed

app/api/endpoints/catalogs.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,6 @@ async def get_catalog(type: str, id: str, response: Response, token: str):
120120
token=token,
121121
library_data=library_items,
122122
)
123-
# Custom attribute for modularized exclusion logic if needed
124-
# In this refactor, RecommendationFiltering.get_exclusion_sets(stremio_service, library_data)
125-
# is called inside engine, and we pass bundle as stremio_service.
126123

127124
# Resolve per-catalog limits (min/max)
128125
def _get_limits() -> tuple[int, int]:

app/services/recommendation/engine.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,6 @@ async def get_recommendations_for_item(self, item_id: str, media_type: str = "mo
292292

293293
async def _fetch_raw_recommendations(self, item_id: str, media_type: str, limit: int) -> list[dict[str, Any]]:
294294
"""Fetch raw recommendations from TMDB (multiple pages)."""
295-
# Logic from _fetch_recommendations_from_tmdb
296295
mtype = "tv" if media_type in ("tv", "series") else "movie"
297296
tmdb_id = None
298297

@@ -313,25 +312,21 @@ async def _fetch_raw_recommendations(self, item_id: str, media_type: str, limit:
313312
return []
314313

315314
combined = {}
316-
for p in [1, 2, 3]:
317-
res = await self.tmdb_service.get_recommendations(tmdb_id, mtype, page=p)
318-
for it in res.get("results", []):
319-
if it.get("id"):
320-
combined[it["id"]] = it
321-
if len(combined) >= limit:
322-
break
315+
# fetch two pages of data
316+
for action in ["recommendations", "similar"]:
317+
method = getattr(self.tmdb_service, f"get_{action}")
318+
results = await asyncio.gather(*[method(tmdb_id, mtype, page=p) for p in [1, 2]], return_exceptions=True)
319+
320+
for res in results:
321+
if isinstance(res, Exception):
322+
logger.error(f"Error fetching {action} for {tmdb_id}: {res}")
323+
continue
324+
for it in res.get("results", []):
325+
if it.get("id"):
326+
combined[it["id"]] = it
323327

324-
if len(combined) < max(20, limit // 2):
325-
try:
326-
for p in [1, 2]:
327-
res = await self.tmdb_service.get_similar(tmdb_id, mtype, page=p)
328-
for it in res.get("results", []):
329-
if it.get("id"):
330-
combined[it["id"]] = it
331-
if len(combined) >= limit:
332-
break
333-
except Exception:
334-
pass
328+
if len(combined) >= max(20, limit // 2):
329+
break
335330

336331
return list(combined.values())
337332

app/services/recommendation/filtering.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ async def get_exclusion_sets(
4545
return set(), set()
4646
library_data = await stremio_service.library.get_library_items(auth_key)
4747

48-
all_items = library_data.get("loved", []) + library_data.get("watched", []) + library_data.get("removed", [])
48+
all_items = (
49+
library_data.get("loved", [])
50+
+ library_data.get("watched", [])
51+
+ library_data.get("removed", [] + library_data.get("liked"))
52+
)
4953

5054
imdb_ids = set()
5155
tmdb_ids = set()

0 commit comments

Comments
 (0)