Improve request handling and qBittorrent categories
This commit is contained in:
@@ -11,9 +11,14 @@ from ..clients.radarr import RadarrClient
|
||||
from ..clients.prowlarr import ProwlarrClient
|
||||
from ..clients.qbittorrent import QBittorrentClient
|
||||
from ..runtime import get_runtime_settings
|
||||
from ..db import save_snapshot, get_request_cache_payload
|
||||
from ..db import save_snapshot, get_request_cache_payload, get_recent_snapshots, get_setting, set_setting
|
||||
from ..models import ActionOption, NormalizedState, RequestType, Snapshot, TimelineHop
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
JELLYFIN_SCAN_COOLDOWN_SECONDS = 300
|
||||
_jellyfin_scan_key = "jellyfin_scan_last_at"
|
||||
|
||||
|
||||
STATUS_LABELS = {
|
||||
1: "Waiting for approval",
|
||||
@@ -41,6 +46,35 @@ def _pick_first(value: Any) -> Optional[Dict[str, Any]]:
|
||||
return None
|
||||
|
||||
|
||||
async def _maybe_refresh_jellyfin(snapshot: Snapshot) -> None:
|
||||
if snapshot.state not in {NormalizedState.available, NormalizedState.completed}:
|
||||
return
|
||||
runtime = get_runtime_settings()
|
||||
client = JellyfinClient(runtime.jellyfin_base_url, runtime.jellyfin_api_key)
|
||||
if not client.configured():
|
||||
return
|
||||
last_scan = get_setting(_jellyfin_scan_key)
|
||||
if last_scan:
|
||||
try:
|
||||
parsed = datetime.fromisoformat(last_scan.replace("Z", "+00:00"))
|
||||
if (datetime.now(timezone.utc) - parsed).total_seconds() < JELLYFIN_SCAN_COOLDOWN_SECONDS:
|
||||
return
|
||||
except ValueError:
|
||||
pass
|
||||
previous = await asyncio.to_thread(get_recent_snapshots, snapshot.request_id, 1)
|
||||
if previous:
|
||||
prev_state = previous[0].get("state")
|
||||
if prev_state in {NormalizedState.available.value, NormalizedState.completed.value}:
|
||||
return
|
||||
try:
|
||||
await client.refresh_library()
|
||||
except Exception as exc:
|
||||
logger.warning("Jellyfin library refresh failed: %s", exc)
|
||||
return
|
||||
set_setting(_jellyfin_scan_key, datetime.now(timezone.utc).isoformat())
|
||||
logger.info("Jellyfin library refresh triggered: request_id=%s", snapshot.request_id)
|
||||
|
||||
|
||||
def _queue_records(queue: Any) -> List[Dict[str, Any]]:
|
||||
if isinstance(queue, dict):
|
||||
records = queue.get("records")
|
||||
@@ -381,10 +415,6 @@ async def build_snapshot(request_id: str) -> Snapshot:
|
||||
|
||||
if arr_state is None:
|
||||
arr_state = "unknown"
|
||||
if arr_state == "missing" and media_status_code in {4}:
|
||||
arr_state = "available"
|
||||
elif arr_state == "missing" and media_status_code in {6}:
|
||||
arr_state = "added"
|
||||
|
||||
timeline.append(TimelineHop(service="Sonarr/Radarr", status=arr_state, details=arr_details))
|
||||
|
||||
@@ -524,7 +554,7 @@ async def build_snapshot(request_id: str) -> Snapshot:
|
||||
snapshot.state_reason = "Waiting for download to start in qBittorrent."
|
||||
elif arr_state == "missing" and derived_approved:
|
||||
snapshot.state = NormalizedState.needs_add
|
||||
snapshot.state_reason = "Approved, but not added to the library yet."
|
||||
snapshot.state_reason = "Approved, but not yet added to Sonarr/Radarr."
|
||||
elif arr_state == "searching":
|
||||
snapshot.state = NormalizedState.searching
|
||||
snapshot.state_reason = "Searching for a matching release."
|
||||
@@ -548,7 +578,7 @@ async def build_snapshot(request_id: str) -> Snapshot:
|
||||
actions.append(
|
||||
ActionOption(
|
||||
id="readd_to_arr",
|
||||
label="Add to the library queue (Sonarr/Radarr)",
|
||||
label="Push to Sonarr/Radarr",
|
||||
risk="medium",
|
||||
)
|
||||
)
|
||||
@@ -604,5 +634,6 @@ async def build_snapshot(request_id: str) -> Snapshot:
|
||||
},
|
||||
}
|
||||
|
||||
await _maybe_refresh_jellyfin(snapshot)
|
||||
await asyncio.to_thread(save_snapshot, snapshot)
|
||||
return snapshot
|
||||
|
||||
Reference in New Issue
Block a user