57 lines
2.1 KiB
Python
57 lines
2.1 KiB
Python
import asyncio
|
|
import logging
|
|
from typing import Any
|
|
|
|
from fastapi import HTTPException
|
|
|
|
from ..clients.jellyseerr import JellyseerrClient
|
|
from ..db import create_user_if_missing, upsert_user_contact
|
|
from ..runtime import get_runtime_settings
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
async def sync_jellyseerr_users() -> int:
|
|
runtime = get_runtime_settings()
|
|
if not runtime.jellyseerr_base_url or not runtime.jellyseerr_api_key:
|
|
raise HTTPException(status_code=400, detail="Jellyseerr not configured")
|
|
client = JellyseerrClient(runtime.jellyseerr_base_url, runtime.jellyseerr_api_key)
|
|
take = 50
|
|
skip = 0
|
|
imported = 0
|
|
while True:
|
|
data = await client.get_users(take=take, skip=skip)
|
|
if not isinstance(data, dict):
|
|
break
|
|
results = data.get("results")
|
|
if not isinstance(results, list) or not results:
|
|
break
|
|
for user in results:
|
|
if not isinstance(user, dict):
|
|
continue
|
|
username = user.get("username") or user.get("email") or user.get("displayName")
|
|
if not isinstance(username, str) or not username.strip():
|
|
continue
|
|
email = user.get("email") if isinstance(user.get("email"), str) else None
|
|
if create_user_if_missing(username.strip(), "jellyseerr-user", role="user", auth_provider="jellyseerr"):
|
|
imported += 1
|
|
if email:
|
|
upsert_user_contact(username.strip(), email=email.strip())
|
|
skip += take
|
|
return imported
|
|
|
|
|
|
async def run_jellyseerr_sync_loop() -> None:
|
|
while True:
|
|
runtime = get_runtime_settings()
|
|
if runtime.jellyseerr_sync_users:
|
|
try:
|
|
imported = await sync_jellyseerr_users()
|
|
logger.info("Jellyseerr sync complete: imported=%s", imported)
|
|
except HTTPException as exc:
|
|
logger.warning("Jellyseerr sync skipped: %s", exc.detail)
|
|
except Exception:
|
|
logger.exception("Jellyseerr sync failed")
|
|
delay = max(60, int(runtime.jellyseerr_sync_interval_minutes or 1440) * 60)
|
|
await asyncio.sleep(delay)
|