[STAGE2] Adapters: Audit360 + Jellyfin + Seerr
This commit is contained in:
1 parent
2f0017a663
commit
e6fc2cb3aa
3 files changed
+115
-3
No files matched your search
@@ -18,7 +18,7 @@ Gate G2: повтор события 100 раз = один эффект; пот
|
||||
| S2-02 | Outbox schema + enqueue | done |
|
||||
| S2-03 | Inbox + effects idempotency | done |
|
||||
| S2-04 | NATS JetStream relay | done |
|
||||
| S2-05 | Adapters: Audit360 done; Jellyfin/Seerr open | in_progress |
|
||||
| S2-05 | Adapters: Audit360/Jellyfin/Seerr | done |
|
||||
| S2-06 | pgvector pilot | done (synthetic infrastructure) |
|
||||
| S2-07 | Gate G2 proof | done |
|
||||
|
||||
@@ -97,6 +97,12 @@ inbox = 46
|
||||
effects = 45
|
||||
```
|
||||
|
||||
## Adapters
|
||||
|
||||
- Audit 360 — real;
|
||||
- Jellyfin — real, 7 events emitted and consumed;
|
||||
- Seerr — real read-only session adapter (0 requests currently, valid empty).
|
||||
|
||||
Примечание: `rag` в SQL_ASCII, поэтому кириллица в payload передаётся
|
||||
как `title_utf8_b64`; ASCII-поле `title_ascii` остаётся для совместимости.
|
||||
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
"""Polling adapters for the Stage 2 event fabric."""
|
||||
import base64
|
||||
import http.cookiejar
|
||||
import json
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
|
||||
from event_store import enqueue, event
|
||||
|
||||
@@ -19,6 +24,107 @@ def _iso(dt):
|
||||
return dt.astimezone(timezone.utc).isoformat()
|
||||
|
||||
|
||||
def seerr_requests(conn):
|
||||
"""Read-only Seerr request adapter using the local breakglass session."""
|
||||
base = "http://192.168.1.121:5055"
|
||||
lines = Path("/root/.jellyfin-breakglass").read_text().splitlines()
|
||||
user = next(x.split("=", 1)[1] for x in lines if x.startswith("username="))
|
||||
pw = next(x.split("=", 1)[1] for x in lines if x.startswith("password="))
|
||||
cj = http.cookiejar.CookieJar()
|
||||
op = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
|
||||
login = urllib.request.Request(
|
||||
base + "/api/v1/auth/jellyfin",
|
||||
data=json.dumps({"username": user, "password": pw}).encode(),
|
||||
headers={"Content-Type": "application/json"}, method="POST")
|
||||
try:
|
||||
with op.open(login, timeout=8) as r:
|
||||
if r.status != 200:
|
||||
return 0
|
||||
req = urllib.request.Request(base + "/api/v1/request?take=50",
|
||||
headers={"Accept": "application/json"})
|
||||
with op.open(req, timeout=8) as r:
|
||||
data = json.load(r)
|
||||
except Exception:
|
||||
return 0
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("select cursor from events.adapter_state where adapter='seerr'")
|
||||
row = cur.fetchone()
|
||||
cursor = row[0] if row and row[0] else "1970-01-01T00:00:00Z"
|
||||
emitted = 0
|
||||
last = cursor
|
||||
for x in data.get("results") or []:
|
||||
stamp = x.get("updatedAt") or x.get("createdAt") or ""
|
||||
if not stamp or stamp <= cursor:
|
||||
continue
|
||||
ev = event(idempotency_key="seerr:request:%s:%s" % (x.get("id"), stamp),
|
||||
source="seerr", type_="media.request.created",
|
||||
subject=str(x.get("id")),
|
||||
data={"id": x.get("id"), "status": x.get("status"),
|
||||
"tmdbId": (x.get("media") or {}).get("tmdbId")})
|
||||
if enqueue(conn, ev):
|
||||
emitted += 1
|
||||
if stamp > last:
|
||||
last = stamp
|
||||
if emitted:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("""
|
||||
insert into events.adapter_state(adapter,cursor,updated_at)
|
||||
values ('seerr',%s,now())
|
||||
on conflict (adapter) do update
|
||||
set cursor=excluded.cursor, updated_at=now()
|
||||
""", (last,))
|
||||
conn.commit()
|
||||
return emitted
|
||||
|
||||
|
||||
def jellyfin_media(conn, limit=50):
|
||||
"""Emit media.work.added for newly created Jellyfin items."""
|
||||
key = Path("/root/.jellyfin-key").read_text().strip()
|
||||
qs = urllib.parse.urlencode({
|
||||
"SortBy": "DateCreated", "SortOrder": "Descending",
|
||||
"IncludeItemTypes": "Movie,Series", "Recursive": "true",
|
||||
"Limit": str(limit), "Fields": "DateCreated,ProductionYear,Overview",
|
||||
})
|
||||
req = urllib.request.Request(
|
||||
"http://192.168.1.103:8096/Items?" + qs,
|
||||
headers={"Authorization": 'MediaBrowser Token="%s"' % key,
|
||||
"Accept": "application/json"})
|
||||
with urllib.request.urlopen(req, timeout=10) as r:
|
||||
data = json.load(r)
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("select cursor from events.adapter_state where adapter='jellyfin'")
|
||||
row = cur.fetchone()
|
||||
cursor = row[0] if row and row[0] else "1970-01-01T00:00:00Z"
|
||||
emitted = 0
|
||||
last = cursor
|
||||
for item in data.get("Items") or []:
|
||||
created = item.get("DateCreated") or ""
|
||||
if created <= cursor:
|
||||
continue
|
||||
name = item.get("Name") or ""
|
||||
ev = event(idempotency_key="jellyfin:work:" + item.get("Id", ""),
|
||||
source="jellyfin", type_="media.work.added",
|
||||
subject=item.get("Id"),
|
||||
data={"id": item.get("Id"), "type": item.get("Type"),
|
||||
"year": item.get("ProductionYear"),
|
||||
"title_ascii": _ascii_text(name),
|
||||
"title_utf8_b64": _utf8_b64(name)})
|
||||
if enqueue(conn, ev):
|
||||
emitted += 1
|
||||
if created > last:
|
||||
last = created
|
||||
if emitted:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("""
|
||||
insert into events.adapter_state(adapter,cursor,updated_at)
|
||||
values ('jellyfin',%s,now())
|
||||
on conflict (adapter) do update
|
||||
set cursor=excluded.cursor, updated_at=now()
|
||||
""", (last,))
|
||||
conn.commit()
|
||||
return emitted
|
||||
|
||||
|
||||
def audit360(conn, limit=100):
|
||||
"""Emit one event per changed Audit 360 item."""
|
||||
with conn.cursor() as cur:
|
||||
|
||||
@@ -4,7 +4,7 @@ import asyncio
|
||||
import signal
|
||||
import time
|
||||
|
||||
from adapters import audit360
|
||||
from adapters import audit360, jellyfin_media, seerr_requests
|
||||
from event_store import connect, init
|
||||
from nats_relay import consume_once, relay_once
|
||||
|
||||
@@ -27,7 +27,7 @@ def main():
|
||||
try:
|
||||
conn = connect()
|
||||
init(conn)
|
||||
emitted = audit360(conn)
|
||||
emitted = audit360(conn) + jellyfin_media(conn) + seerr_requests(conn)
|
||||
conn.close()
|
||||
relayed = asyncio.run(relay_once())
|
||||
consumed = asyncio.run(consume_once(timeout=2))
|
||||
|
||||
Reference in new issue
Block a user