Skip to content

Commit b1ebaa7

Browse files
refactor: enhance recommendation services by consolidating utility functions and improving filtering logic
1 parent 9731be4 commit b1ebaa7

File tree

8 files changed

+142
-1179
lines changed

8 files changed

+142
-1179
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,4 @@ logs/
4747
*/ipynb_checkpoints/
4848
*.ipynb
4949
.vercel
50+
migration.py

app/services/recommendation/catalog_service.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,6 @@ async def get_catalog(
4545
"""
4646
Get catalog recommendations.
4747
48-
Args:
49-
token: User token
50-
content_type: Content type (movie/series)
51-
catalog_id: Catalog ID (watchly.rec, watchly.creators, watchly.theme.*, etc.)
52-
53-
Returns:
54-
Tuple of (recommendations list, response headers dict)
55-
"""
56-
"""
57-
Get catalog recommendations.
58-
5948
Args:
6049
token: User token
6150
content_type: Content type (movie/series)
@@ -193,9 +182,8 @@ async def _resolve_auth(self, bundle: StremioBundle, credentials: dict, token: s
193182
try:
194183
auth_key = await bundle.auth.login(email, password)
195184
credentials["authKey"] = auth_key
196-
# Note: token is not stored in credentials, we'd need to pass it separately
197-
# For now, this is handled by the caller if needed
198-
pass
185+
# Update token store with refreshed credentials
186+
await token_store.update_user_data(token, credentials)
199187
except Exception as e:
200188
logger.error(f"Failed to refresh auth key during catalog fetch: {e}")
201189

app/services/recommendation/creators.py

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from app.models.taste_profile import TasteProfile
1515
from app.services.recommendation.filtering import RecommendationFiltering
1616
from app.services.recommendation.metadata import RecommendationMetadata
17+
from app.services.recommendation.utils import content_type_to_mtype, filter_watched_by_imdb, resolve_tmdb_id
1718

1819

1920
class CreatorsService:
@@ -65,7 +66,7 @@ async def get_recommendations_from_creators(
6566
Returns:
6667
List of recommended items
6768
"""
68-
mtype = "tv" if content_type in ("tv", "series") else "movie"
69+
mtype = content_type_to_mtype(content_type)
6970

7071
# Get top directors and cast from profile
7172
top_directors = profile.get_top_directors(limit=20)
@@ -171,13 +172,7 @@ async def get_recommendations_from_creators(
171172
)
172173

173174
# Final filter (remove watched by IMDB ID)
174-
final = []
175-
for item in enriched:
176-
if item.get("id") in watched_imdb:
177-
continue
178-
if item.get("_external_ids", {}).get("imdb_id") in watched_imdb:
179-
continue
180-
final.append(item)
175+
final = filter_watched_by_imdb(enriched, watched_imdb)
181176

182177
return final[:limit]
183178

@@ -209,15 +204,7 @@ async def count_creators(item: dict):
209204
try:
210205
# Resolve TMDB ID
211206
item_id = item.get("_id", "")
212-
tmdb_id = None
213-
214-
if item_id.startswith("tmdb:"):
215-
try:
216-
tmdb_id = int(item_id.split(":")[1])
217-
except (ValueError, IndexError):
218-
return
219-
elif item_id.startswith("tt"):
220-
tmdb_id, _ = await self.tmdb_service.find_by_imdb_id(item_id)
207+
tmdb_id = await resolve_tmdb_id(item_id, self.tmdb_service)
221208

222209
if not tmdb_id:
223210
return

0 commit comments

Comments
 (0)