|
1 | | -from fastapi import HTTPException |
2 | 1 | from fastapi.routing import APIRouter |
3 | | -from loguru import logger |
4 | 2 |
|
5 | | -from app.core.config import settings |
6 | | -from app.core.settings import UserSettings |
7 | | -from app.core.version import __version__ |
8 | | -from app.services.catalog import DynamicCatalogService |
9 | | -from app.services.catalog_updater import get_config_id |
10 | | -from app.services.stremio.service import StremioBundle |
11 | | -from app.services.token_store import token_store |
12 | | -from app.services.translation import translation_service |
| 3 | +from app.services.manifest import manifest_service |
13 | 4 |
|
14 | 5 | router = APIRouter() |
15 | 6 |
|
16 | 7 |
|
17 | | -def get_base_manifest(): |
18 | | - return { |
19 | | - "id": settings.ADDON_ID, |
20 | | - "version": __version__, |
21 | | - "name": settings.ADDON_NAME, |
22 | | - "description": "Movie and series recommendations based on your Stremio library.", |
23 | | - "logo": "https://raw.githubusercontent.com/TimilsinaBimal/Watchly/refs/heads/main/app/static/logo.png", |
24 | | - "background": ("https://raw.githubusercontent.com/TimilsinaBimal/Watchly/refs/heads/main/app/static/cover.png"), |
25 | | - "resources": ["catalog"], |
26 | | - "types": ["movie", "series"], |
27 | | - "idPrefixes": ["tt"], |
28 | | - "catalogs": [], |
29 | | - "behaviorHints": {"configurable": True, "configurationRequired": False}, |
30 | | - "stremioAddonsConfig": { |
31 | | - "issuer": "https://stremio-addons.net", |
32 | | - "signature": ( |
33 | | - "eyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2In0..WSrhzzlj1TuDycD6QoVLuA.Dzmxzr4y83uqQF15r4tC1bB9-vtZRh1Rvy4BqgDYxu91c2esiJuov9KnnI_cboQCgZS7hjwnIqRSlQ-jEyGwXHHRerh9QklyfdxpXqNUyBgTWFzDOVdVvDYJeM_tGMmR.sezAChlWGV7lNS-t9HWB6A" # noqa |
34 | | - ), |
35 | | - }, |
36 | | - } |
37 | | - |
38 | | - |
39 | | -async def build_dynamic_catalogs( |
40 | | - bundle: StremioBundle, auth_key: str, user_settings: UserSettings | None |
41 | | -) -> list[dict]: |
42 | | - # Fetch library using bundle directly |
43 | | - if not user_settings: |
44 | | - logger.error("User settings not found. Please reconfigure the addon.") |
45 | | - raise HTTPException(status_code=401, detail="User settings not found. Please reconfigure the addon.") |
46 | | - |
47 | | - library_items = await bundle.library.get_library_items(auth_key) |
48 | | - dynamic_catalog_service = DynamicCatalogService( |
49 | | - language=user_settings.language, |
50 | | - ) |
51 | | - return await dynamic_catalog_service.get_dynamic_catalogs(library_items, user_settings) |
52 | | - |
53 | | - |
54 | | -async def _manifest_handler(token: str): |
55 | | - # response.headers["Cache-Control"] = "public, max-age=300" # 5 minutes |
56 | | - if not token: |
57 | | - raise HTTPException(status_code=401, detail="Missing token. Please reconfigure the addon.") |
58 | | - |
59 | | - user_settings = None |
60 | | - try: |
61 | | - creds = await token_store.get_user_data(token) |
62 | | - if creds and creds.get("settings"): |
63 | | - user_settings = UserSettings(**creds["settings"]) |
64 | | - except Exception as e: |
65 | | - logger.error(f"[{token}] Error loading user data from token store: {e}") |
66 | | - raise HTTPException(status_code=401, detail="Invalid token session. Please reconfigure.") |
67 | | - |
68 | | - if not creds: |
69 | | - raise HTTPException(status_code=401, detail="Token not found. Please reconfigure the addon.") |
70 | | - |
71 | | - base_manifest = get_base_manifest() |
72 | | - |
73 | | - bundle = StremioBundle() |
74 | | - fetched_catalogs = [] |
75 | | - try: |
76 | | - # Resolve Auth Key (with potential fallback to login) |
77 | | - auth_key = creds.get("authKey") |
78 | | - email = creds.get("email") |
79 | | - password = creds.get("password") |
80 | | - |
81 | | - is_valid = False |
82 | | - if auth_key: |
83 | | - try: |
84 | | - await bundle.auth.get_user_info(auth_key) |
85 | | - is_valid = True |
86 | | - except Exception as e: |
87 | | - logger.debug(f"Auth key check failed for {email or 'unknown'}: {e}") |
88 | | - pass |
89 | | - |
90 | | - if not is_valid and email and password: |
91 | | - try: |
92 | | - auth_key = await bundle.auth.login(email, password) |
93 | | - # Update store |
94 | | - creds["authKey"] = auth_key |
95 | | - await token_store.update_user_data(token, creds) |
96 | | - except Exception as e: |
97 | | - logger.error(f"Failed to refresh auth key during manifest fetch: {e}") |
98 | | - |
99 | | - if auth_key: |
100 | | - fetched_catalogs = await build_dynamic_catalogs( |
101 | | - bundle, |
102 | | - auth_key, |
103 | | - user_settings, |
104 | | - ) |
105 | | - except Exception as e: |
106 | | - logger.exception(f"[{token}] Dynamic catalog build failed: {e}") |
107 | | - fetched_catalogs = [] |
108 | | - finally: |
109 | | - await bundle.close() |
110 | | - |
111 | | - all_catalogs = [c.copy() for c in base_manifest["catalogs"]] + [c.copy() for c in fetched_catalogs] |
112 | | - |
113 | | - translated_catalogs = [] |
114 | | - |
115 | | - # translate to target language |
116 | | - if user_settings and user_settings.language: |
117 | | - for cat in all_catalogs: |
118 | | - if cat.get("name"): |
119 | | - try: |
120 | | - cat["name"] = await translation_service.translate(cat["name"], user_settings.language) |
121 | | - except Exception as e: |
122 | | - logger.warning(f"Failed to translate catalog name '{cat.get('name')}': {e}") |
123 | | - translated_catalogs.append(cat) |
124 | | - else: |
125 | | - translated_catalogs = all_catalogs |
126 | | - |
127 | | - if user_settings: |
128 | | - order_map = {c.id: i for i, c in enumerate(user_settings.catalogs)} |
129 | | - translated_catalogs.sort(key=lambda x: order_map.get(get_config_id(x), 999)) |
130 | | - |
131 | | - if translated_catalogs: |
132 | | - base_manifest["catalogs"] = translated_catalogs |
133 | | - |
134 | | - return base_manifest |
135 | | - |
136 | | - |
137 | 8 | @router.get("/manifest.json") |
138 | 9 | async def manifest(): |
139 | | - manifest = get_base_manifest() |
| 10 | + """Get base manifest for unauthenticated users.""" |
| 11 | + manifest = manifest_service.get_base_manifest() |
140 | 12 | # since user is not logged in, return empty catalogs |
141 | 13 | manifest["catalogs"] = [] |
142 | 14 | return manifest |
143 | 15 |
|
144 | 16 |
|
145 | 17 | @router.get("/{token}/manifest.json") |
146 | 18 | async def manifest_token(token: str): |
147 | | - return await _manifest_handler(token) |
| 19 | + """Get manifest for authenticated user.""" |
| 20 | + return await manifest_service.get_manifest_for_token(token) |
0 commit comments