Skip to content

Commit d2b5df8

Browse files
feat: check and only update tokens that are installed from the same system
1 parent 4924c38 commit d2b5df8

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

app/services/catalog_updater.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ async def refresh_catalogs_for_credentials(token: str, credentials: dict[str, An
2929
try:
3030
addon_installed = await stremio_service.is_addon_installed(auth_key)
3131
if not addon_installed:
32-
logger.info("User has not installed addon. Removing token from redis")
32+
logger.info(f"[{redact_token(token)}] User has not installed addon. Removing token from redis")
3333
await token_store.delete_token(key=token)
3434
return True
3535
except Exception as e:
36-
logger.exception(f"Failed to check if addon is installed: {e}")
36+
logger.exception(f"[{redact_token(token)}] Failed to check if addon is installed: {e}")
3737

3838
try:
3939
library_items = await stremio_service.get_library_items()
@@ -45,15 +45,15 @@ async def refresh_catalogs_for_credentials(token: str, credentials: dict[str, An
4545
user_settings = UserSettings(**credentials["settings"])
4646
except Exception as e:
4747
user_settings = get_default_settings()
48-
logger.warning(f"Failed to parse user settings from credentials: {e}")
48+
logger.warning(f"[{redact_token(token)}] Failed to parse user settings from credentials: {e}")
4949

5050
catalogs = await dynamic_catalog_service.get_dynamic_catalogs(
5151
library_items=library_items, user_settings=user_settings
5252
)
5353
logger.info(f"[{redact_token(token)}] Prepared {len(catalogs)} catalogs")
5454
return await stremio_service.update_catalogs(catalogs, auth_key)
5555
except Exception as e:
56-
logger.exception(f"Failed to update catalogs: {e}", exc_info=True)
56+
logger.exception(f"[{redact_token(token)}] Failed to update catalogs: {e}", exc_info=True)
5757
raise e
5858
finally:
5959
await stremio_service.close()

app/services/stremio_service.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
from urllib.parse import urlparse
23

34
import httpx
45
from async_lru import alru_cache
@@ -12,6 +13,14 @@
1213
]
1314

1415

16+
def match_hostname(url: str, hostname: str) -> bool:
17+
"""
18+
Checks if the hostname extracted from a URL matches a given hostname string.
19+
"""
20+
parsed_url = urlparse(url)
21+
return parsed_url.hostname == hostname
22+
23+
1524
class StremioService:
1625
"""Service for interacting with Stremio API to fetch user library."""
1726

@@ -340,7 +349,9 @@ async def update_catalogs(self, catalogs: list[dict], auth_key: str | None = Non
340349
logger.info(f"Found {len(addons)} addons")
341350
# find addon with id "com.watchly"
342351
for addon in addons:
343-
if addon.get("manifest", {}).get("id") == settings.ADDON_ID:
352+
if addon.get("manifest", {}).get("id") == settings.ADDON_ID and match_hostname(
353+
addon.get("transportUrl"), settings.HOST_NAME
354+
):
344355
logger.info(f"Found addon with id {settings.ADDON_ID}")
345356
addon["manifest"]["catalogs"] = catalogs
346357
break
@@ -350,6 +361,8 @@ async def is_addon_installed(self, auth_key: str | None = None):
350361
auth_key = auth_key or await self.get_auth_key()
351362
addons = await self.get_addons(auth_key)
352363
for addon in addons:
353-
if addon.get("manifest", {}).get("id") == settings.ADDON_ID:
364+
if addon.get("manifest", {}).get("id") == settings.ADDON_ID and match_hostname(
365+
addon.get("transportUrl"), settings.HOST_NAME
366+
):
354367
return True
355368
return False

0 commit comments

Comments
 (0)