Skip to content

Commit 487121e

Browse files
opt: parallelize data fetch
1 parent 0372c22 commit 487121e

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

app/core/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class Settings(BaseSettings):
3838
HOST_NAME: str = "https://1ccea4301587-watchly.baby-beamup.club"
3939

4040
RECOMMENDATION_SOURCE_ITEMS_LIMIT: int = 10
41+
LIBRARY_ITEMS_LIMIT: int = 20
4142

4243
# AI
4344
DEFAULT_GEMINI_MODEL: str = "gemma-3-27b-it"

app/services/recommendation/engine.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from loguru import logger
66

7+
from app.core.config import settings
78
from app.core.settings import UserSettings
89
from app.services.discovery import DiscoveryEngine
910
from app.services.profile.service import UserProfileService
@@ -334,7 +335,6 @@ async def _get_genre_whitelist(self, content_type: str, scored_objects: list | N
334335
if content_type in self._whitelist_cache:
335336
return self._whitelist_cache[content_type]
336337

337-
# Logic from _get_top_genre_whitelist
338338
try:
339339
if scored_objects is None:
340340
if self._library_data is None:
@@ -346,6 +346,7 @@ async def _get_genre_whitelist(self, content_type: str, scored_objects: list | N
346346
self._library_data.get("loved", [])
347347
+ self._library_data.get("watched", [])
348348
+ self._library_data.get("added", [])
349+
+ self._library_data.get("liked", [])
349350
)
350351
typed = [
351352
it
@@ -357,7 +358,9 @@ async def _get_genre_whitelist(self, content_type: str, scored_objects: list | N
357358
key=lambda x: x.get("state", {}).get("lastWatched") or "",
358359
reverse=True,
359360
)
360-
scored_objects = [self.scoring_service.process_item(it) for it in sorted_hist[:10]]
361+
scored_objects = [
362+
self.scoring_service.process_item(it) for it in sorted_hist[: settings.LIBRARY_ITEMS_LIMIT]
363+
]
361364

362365
prof_type = "series" if content_type in ("tv", "series") else "movie"
363366
temp_profile = await self.user_profile_service.build_user_profile(scored_objects, content_type=prof_type)
@@ -634,11 +637,13 @@ async def get_recommendations_for_theme(self, theme_id: str, content_type: str,
634637
whitelist = await self._get_genre_whitelist(content_type)
635638
candidates = []
636639
try:
637-
for p in [1, 2, 3]:
638-
res = await self.tmdb_service.get_discover(content_type, page=p, **params)
640+
discover_tasks = [self.tmdb_service.get_discover(content_type, page=p, **params) for p in [1, 2, 3]]
641+
discover_results = await asyncio.gather(*discover_tasks, return_exceptions=True)
642+
for res in discover_results:
643+
if isinstance(res, Exception):
644+
logger.error(f"Error fetching discover for {content_type}: {res}")
645+
continue
639646
candidates.extend(res.get("results", []))
640-
if len(candidates) >= limit * 2:
641-
break
642647
except Exception:
643648
pass
644649

0 commit comments

Comments
 (0)