-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcontinue_watching.py
More file actions
executable file
·68 lines (61 loc) · 2.37 KB
/
Copy pathcontinue_watching.py
File metadata and controls
executable file
·68 lines (61 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""Continue Watching priority: query Jellyfin for in-progress series, find the
next unaired/missing episode, and bump it to the front of monitor's search queue.
"""
import logging
import requests
import config
import db
import monitor
import settings as _settings
log = logging.getLogger(__name__)
def _jellyfin_resume_items() -> list[dict]:
"""Items currently being watched across all users."""
jellyfin_url = (_settings.get("JELLYFIN_URL", config.JELLYFIN_URL) or "").strip()
jellyfin_key = (_settings.get("JELLYFIN_API_KEY", config.JELLYFIN_API_KEY) or "").strip()
if not (jellyfin_url and jellyfin_key):
return []
try:
r = requests.get(
f"{jellyfin_url.rstrip('/')}/Users/Me/Items/Resume",
params={"Limit": 50},
headers={"X-Emby-Token": jellyfin_key},
timeout=8,
)
r.raise_for_status()
return (r.json() or {}).get("Items") or []
except Exception as exc:
log.debug("Jellyfin Resume fetch failed: %s", exc)
return []
def prioritize_next_episodes() -> int:
"""For each in-progress series in Jellyfin, search for its next wanted episode."""
items = _jellyfin_resume_items()
if not items:
return 0
priorities = 0
series_items = [i for i in items if i.get("Type") == "Episode"]
# Use SeriesName to match our monitored series
monitored = {s["title"]: s for s in db.get_all_monitored_series()}
wanted = db.get_wanted_episodes()
for it in series_items:
series_name = it.get("SeriesName")
if not series_name or series_name not in monitored:
continue
m = monitored[series_name]
# Find lowest (season, episode) still wanted for this series
ours = sorted(
[w for w in wanted if w["imdb_id"] == m["imdb_id"]],
key=lambda w: (w["season"], w["episode"]),
)
if not ours:
continue
next_ep = ours[0]
log.info("ContinueWatching: prioritizing %s S%02dE%02d",
series_name, next_ep["season"], next_ep["episode"])
try:
monitor.search_episode_now(
m["imdb_id"], series_name, next_ep["season"], next_ep["episode"],
)
priorities += 1
except Exception as exc:
log.warning("ContinueWatching: search failed for %s: %s", series_name, exc)
return priorities