Skip to content

Commit 2bb584a

Browse files
feat: enhance dynamic catalog building and improve error handling
1 parent b3ed5ba commit 2bb584a

File tree

3 files changed

+31
-40
lines changed

3 files changed

+31
-40
lines changed

app/api/endpoints/manifest.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,14 @@ def get_base_manifest():
4141
}
4242

4343

44-
async def build_dynamic_catalogs(bundle: StremioBundle, auth_key: str, user_settings: UserSettings) -> list[dict]:
44+
async def build_dynamic_catalogs(
45+
bundle: StremioBundle, auth_key: str, user_settings: UserSettings | None
46+
) -> list[dict]:
4547
# Fetch library using bundle directly
48+
if not user_settings:
49+
logger.error("User settings not found. Please reconfigure the addon.")
50+
raise HTTPException(status_code=401, detail="User settings not found. Please reconfigure the addon.")
51+
4652
library_items = await bundle.library.get_library_items(auth_key)
4753
dynamic_catalog_service = DynamicCatalogService(
4854
language=user_settings.language,

app/services/recommendation/all_based.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,13 @@ async def _fetch_recommendations_for_item(self, item_id: str, mtype: str) -> lis
166166
combined = {}
167167

168168
# Fetch 1 page each for recommendations
169-
for action in ["recommendations"]:
170-
method = getattr(self.tmdb_service, f"get_{action}")
171-
try:
172-
res = await method(tmdb_id, mtype, page=1)
173-
for item in res.get("results", []):
174-
item_id = item.get("id")
175-
if item_id:
176-
combined[item_id] = item
177-
except Exception as e:
178-
logger.debug(f"Error fetching {action} for {tmdb_id}: {e}")
179-
continue
169+
try:
170+
res = await self.tmdb_service.get_recommendations(tmdb_id, mtype, page=1)
171+
for item in res.get("results", []):
172+
item_id = item.get("id")
173+
if item_id:
174+
combined[item_id] = item
175+
except Exception as e:
176+
logger.debug(f"Error fetching recommendations for {tmdb_id}: {e}")
180177

181178
return list(combined.values())

app/services/recommendation/catalog_service.py

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from app.services.recommendation.item_based import ItemBasedService
1717
from app.services.recommendation.theme_based import ThemeBasedService
1818
from app.services.recommendation.top_picks import TopPicksService
19+
from app.services.recommendation.utils import pad_to_min
1920
from app.services.stremio.service import StremioBundle
2021
from app.services.tmdb.service import get_tmdb_service
2122
from app.services.token_store import token_store
@@ -98,16 +99,17 @@ async def get_catalog(
9899
)
99100

100101
# Pad if needed
101-
# if len(recommendations) < min_items:
102-
# recommendations = await pad_to_min(
103-
# content_type,
104-
# recommendations,
105-
# min_items,
106-
# services["tmdb"],
107-
# user_settings,
108-
# watched_tmdb,
109-
# watched_imdb,
110-
# )
102+
# TODO: This is risky because it can fetch too many unrelated items.
103+
if recommendations and len(recommendations) < 8:
104+
recommendations = await pad_to_min(
105+
content_type,
106+
recommendations,
107+
10, # only fetch 10 items if less than 8
108+
services["tmdb"],
109+
user_settings,
110+
watched_tmdb,
111+
watched_imdb,
112+
)
111113

112114
logger.info(f"Returning {len(recommendations)} items for {content_type}")
113115

@@ -304,22 +306,8 @@ async def _get_recommendations(
304306
logger.info(f"Found {len(recommendations)} top picks for {content_type}")
305307

306308
# Based on what you loved
307-
elif catalog_id == "watchly.all.loved":
308-
all_based_service: AllBasedService = services["all_based"]
309-
recommendations = await all_based_service.get_recommendations_from_all_items(
310-
library_items=library_items,
311-
content_type=content_type,
312-
watched_tmdb=watched_tmdb,
313-
watched_imdb=watched_imdb,
314-
whitelist=whitelist,
315-
limit=max_items,
316-
item_type="loved",
317-
profile=profile,
318-
)
319-
logger.info(f"Found {len(recommendations)} recommendations based on all loved items")
320-
321-
# Based on what you liked
322-
elif catalog_id == "watchly.liked.all":
309+
elif catalog_id in ("watchly.all.loved", "watchly.liked.all"):
310+
item_type = "loved" if catalog_id == "watchly.all.loved" else "liked"
323311
all_based_service: AllBasedService = services["all_based"]
324312
recommendations = await all_based_service.get_recommendations_from_all_items(
325313
library_items=library_items,
@@ -328,10 +316,10 @@ async def _get_recommendations(
328316
watched_imdb=watched_imdb,
329317
whitelist=whitelist,
330318
limit=max_items,
331-
item_type="liked",
319+
item_type=item_type,
332320
profile=profile,
333321
)
334-
logger.info(f"Found {len(recommendations)} recommendations based on all liked items")
322+
logger.info(f"Found {len(recommendations)} recommendations based on all {item_type} items")
335323

336324
else:
337325
logger.warning(f"Unknown catalog ID: {catalog_id}")

0 commit comments

Comments
 (0)