Files
buryan-infra/ops/media/content_graph.py
T
buryan-agent 3e680ab934 Граф v3: персоны, рекомендации, оценки, языки, русские названия
Добавлены связи из SkyHook: режиссёры и актёры (Credits), рекомендации
(Recommendations), оценки IMDb по корзинам, возрастной рейтинг, язык.
Русские названия из AlternativeTitles сохраняются как алиасы — поиск
«Дюна» находит Dune.

Итог: 359 карточек и 376 связей (было 182 и 193), 48 актёров, 6 режиссёров,
41 кандидат, 42 темы.

Новые команды бота Медиа: «тема <слово>» (подборка), «связи <название>»
(вся карточка со связями), «рекомендации» (что посмотреть из кандидатов).

Доработано в ядре: /entities с фильтром по виду и лимитом, поддержка
aliases при создании карточки.

Ловушки: кириллица в query требует urlencode; псевдоним «связи» перехватывался
командой link — переопределён ALIASES в MediaBot.
2026-10-20 10:49:09 +03:00

280 lines
11 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Граф контента v3: персоны, рекомендации, рейтинги, языки, подборки.
Дополнительно к v2 (жанры, студии, темы, эпохи, продолжения, похожие):
фильм → режиссёр «снял»
фильм → актёр «снялся»
фильм → кандидат «рекомендует» (из TMDB, если фильма нет в медиатеке)
фильм → рейтинг «возрастной рейтинг»
фильм → оценка «оценка»
фильм → язык «язык»
"""
import json
import os
import re
import subprocess
import sys
import urllib.error
import urllib.request
CORE = os.environ.get("CORE_URL", "http://127.0.0.1:9120")
RADARR = os.environ.get("RADARR_URL", "http://192.168.1.118:7878")
RADARR_KEY = os.environ.get("RADARR_KEY", "")
SKYHOOK = "https://api.radarr.video/v1/movie"
PROXY = "http://192.168.1.10:8888"
MAX_THEMES = 5
MAX_CAST = 5
MAX_RECOMMEND = 6
MAX_SIMILAR = 40
LANGS = {"en": "английский", "ru": "русский", "fr": "французский",
"de": "немецкий", "ja": "японский", "es": "испанский"}
def get(url, headers=None, timeout=25, proxy=None):
handlers = []
if proxy:
handlers.append(urllib.request.ProxyHandler({"https": proxy, "http": proxy}))
opener = urllib.request.build_opener(*handlers)
req = urllib.request.Request(url, headers=headers or {})
try:
with opener.open(req, timeout=timeout) as r:
return json.loads(r.read() or b"{}"), None
except Exception as e:
return None, str(e)[:100]
def post(path, payload):
req = urllib.request.Request(f"{CORE}{path}",
data=json.dumps(payload, ensure_ascii=False).encode(),
method="POST", headers={"Content-Type": "application/json"})
try:
with urllib.request.urlopen(req, timeout=20) as r:
return json.loads(r.read() or b"{}"), None
except Exception as e:
return None, str(e)[:100]
def norm(s):
s = (s or "").lower().replace("ё", "е")
s = re.sub(r"[:!?.,'’\"()\[\]]", " ", s)
return " ".join(s.split())
def movies():
data, err = get(f"{RADARR}/api/v3/movie", {"X-Api-Key": RADARR_KEY})
if err:
print(f" Radarr: {err}")
return []
out = []
for m in data or []:
col = m.get("collection") or {}
out.append({
"title": m.get("title"), "year": m.get("year"), "tmdb": m.get("tmdbId"),
"collection": col.get("title"), "collection_tmdb": col.get("tmdbId"),
"genres": m.get("genres") or [], "studio": m.get("studio"),
"keywords": (m.get("keywords") or [])[:MAX_THEMES],
})
return out
def skyhook(tmdb):
data, err = get(f"{SKYHOOK}/{tmdb}", proxy=PROXY, timeout=40)
if err:
return {}, err
return data or {}, None
def books():
script = '''
import json, sqlite3
c = sqlite3.connect('/opt/books/library/metadata.db')
rows = []
for bid, title in c.execute("select id, title from books"):
authors = [r[0] for r in c.execute(
"select a.name from authors a join books_authors_link l on l.author=a.id where l.book=?", (bid,))]
series = c.execute(
"select s.name from series s join books_series_link l on l.series=s.id where l.book=?", (bid,)).fetchone()
tags = [r[0] for r in c.execute(
"select t.name from tags t join books_tags_link l on l.tag=t.id where l.book=?", (bid,))]
rows.append({"id": bid, "title": title, "authors": authors,
"series": series[0] if series else None, "tags": tags})
print(json.dumps(rows, ensure_ascii=False))
'''
path = "/tmp/books_graph.json"
open(path, "w", encoding="utf-8").write(script)
subprocess.run(["pct", "push", "111", path, path, "--perms", "644"], capture_output=True, timeout=30)
out = subprocess.run(["pct", "exec", "111", "--", "python3", path],
capture_output=True, text=True, timeout=60)
try:
return json.loads(out.stdout or "[]")
except Exception:
return []
def decade(year):
try:
y = int(year)
except (TypeError, ValueError):
return None
return f"{(y // 10) * 10}-е"
def main():
mv = movies()
bk = books()
print(f"фильмов: {len(mv)}, книг: {len(bk)}")
ids, cards, links = {}, 0, 0
def card(kind, key, title, status=None, aliases=None):
nonlocal cards
payload = {"kind": kind, "key": key, "title": title, "status": status}
if aliases:
payload["aliases"] = aliases
res, err = post("/entity", payload)
if err or not res:
return None
cards += 1
ids[key] = res.get("id")
return res.get("id")
def link(a, b, rel):
nonlocal links
if not a or not b or a == b:
return
_, err = post("/link", {"from": a, "to": b, "relation": rel})
if not err:
links += 1
# ── фильмы и метаданные из Radarr ──
for m in mv:
mid = card("фильм", f"tmdb:{m['tmdb']}", f"{m['title']} ({m['year']})", "в медиатеке")
for g in m["genres"]:
link(mid, card("жанр", f"genre:{norm(g)}", g), "жанр")
if m["studio"]:
link(mid, card("студия", f"studio:{norm(m['studio'])}", m["studio"]), "снят студией")
for kw in m["keywords"]:
link(mid, card("тема", f"theme:{norm(kw)}", kw), "тема")
dec = decade(m["year"])
if dec:
link(mid, card("эпоха", f"decade:{dec}", dec), "снят в")
if m["collection"]:
cid = card("коллекция", f"collection:{m['collection_tmdb'] or norm(m['collection'])}",
m["collection"], "франшиза")
link(mid, cid, "входит в")
# ── детали из SkyHook: персоны, рейтинги, рекомендации, язык ──
in_library = {m["tmdb"] for m in mv}
persons = 0
for m in mv:
det, err = skyhook(m["tmdb"])
if err:
print(f" skyhook {m['title']}: {err}")
continue
mid = ids.get(f"tmdb:{m['tmdb']}")
# русские названия — для поиска «Дюна» вместо «Dune»
alts = []
for a in det.get("AlternativeTitles") or []:
if (a.get("Language") or "").lower() in ("ru", "ru-ru"):
t = a.get("Title")
if t and t not in alts:
alts.append(t)
if alts:
card("фильм", "tmdb:%s" % m["tmdb"], "%s (%s)" % (m["title"], m["year"]),
"в медиатеке", aliases=alts[:3])
credits = det.get("Credits") or {}
for c in (credits.get("Crew") or []):
if c.get("Job") == "Director" and c.get("Name"):
pid = card("режиссёр", f"person:{norm(c['Name'])}", c["Name"])
link(mid, pid, "снял")
persons += 1
for c in (credits.get("Cast") or [])[:MAX_CAST]:
if c.get("Name"):
pid = card("актёр", f"person:{norm(c['Name'])}", c["Name"])
link(mid, pid, "снялся")
persons += 1
ratings = det.get("MovieRatings") or {}
imdb = (ratings.get("Imdb") or {}).get("Value") or 0
if imdb:
bucket = "высокая (8+)" if imdb >= 8 else ("хорошая (7–8)" if imdb >= 7 else "средняя (<7)")
link(mid, card("оценка", f"rating:{bucket}", bucket), "оценка")
certs = det.get("Certifications") or []
us = next((c.get("Certification") for c in certs if c.get("Country") == "US"), None)
if us:
link(mid, card("рейтинг", f"cert:{us}", us), "возрастной рейтинг")
lang = det.get("OriginalLanguage") or ""
if lang:
name = LANGS.get(lang, lang)
link(mid, card("язык", f"lang:{lang}", name), "язык")
for r in (det.get("Recommendations") or [])[:MAX_RECOMMEND]:
rt = r.get("TmdbId")
if not rt or rt == m["tmdb"]:
continue
if rt in in_library:
link(mid, ids.get(f"tmdb:{rt}"), "рекомендует")
else:
title = r.get("Title") or f"tmdb:{rt}"
rid = card("кандидат", f"candidate:{rt}", title, "не в медиатеке")
link(mid, rid, "рекомендует")
print(f" персон связано: {persons}")
# ── книги ──
for b in bk:
bid = card("книга", f"calibre:{b['id']}", b["title"], "в библиотеке")
for a in b.get("authors") or []:
link(bid, card("автор", f"author:{norm(a)}", a), "написал")
if b.get("series"):
link(bid, card("серия книг", f"series:{norm(b['series'])}", b["series"]), "входит в серию")
for t in b.get("tags") or []:
link(bid, card("жанр", f"genre:{norm(t)}", t), "жанр")
# ── экранизации ──
by_title = {}
for m in mv:
by_title.setdefault(norm(m["title"]), []).append(m["tmdb"])
for b in bk:
for tmdb in by_title.get(norm(b["title"]), []):
link(ids.get(f"calibre:{b['id']}"), ids.get(f"tmdb:{tmdb}"), "экранизация")
# ── продолжения ──
by_col = {}
for m in mv:
if m["collection"]:
by_col.setdefault(norm(m["collection"]), []).append(m)
for _, items in by_col.items():
items.sort(key=lambda x: x.get("year") or 0)
for prev, nxt in zip(items, items[1:]):
link(ids.get(f"tmdb:{prev['tmdb']}"), ids.get(f"tmdb:{nxt['tmdb']}"), "продолжение")
# ── похожие по жанрам ──
made = 0
for i, a in enumerate(mv):
for b in mv[i + 1:]:
if len(set(a["genres"]) & set(b["genres"])) >= 2 and made < MAX_SIMILAR:
link(ids.get(f"tmdb:{a['tmdb']}"), ids.get(f"tmdb:{b['tmdb']}"), "похож")
made += 1
# ── общая вселенная ──
for m in mv:
if not m["collection"]:
continue
col_key = f"collection:{m['collection_tmdb'] or norm(m['collection'])}"
col_norm = norm(m["collection"]).replace("collection", "").strip()
for b in bk:
if b.get("series") and norm(b["series"]) == col_norm:
link(ids.get(col_key), ids.get(f"series:{norm(b['series'])}"), "общая вселенная")
print(f"карточек: {cards}, связей: {links}")
return 0
if __name__ == "__main__":
sys.exit(main())