Files
buryan-infra/ops/media/content_graph.py
buryan-agent 42bb7e9e12 Музыка в графе и команда «музыка»
content_graph читает /mnt/media/music (исполнитель/альбом/треки) и строит
связи: исполнитель → альбом «выпустил», альбом → трек «содержит».
Проверено на контрольном альбоме.

Команда «музыка» показывает состояние Navidrome, число альбомов и треков,
наличие токена Яндекс Музыки.

Зафиксировано, чего не делаем: скачивание защищённой музыки с торрентов.
Легальные пути — токен Яндекс Музыки, купленные файлы, свободная музыка.
2026-10-20 12:58:48 +03:00

428 lines
17 KiB
Python
Raw Permalink 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 series():
"""Сериалы из Sonarr: жанры, сеть, сезоны."""
url = os.environ.get("SONARR_URL", "http://192.168.1.117:8989")
key = os.environ.get("SONARR_KEY", "")
data, err = get(f"{url}/api/v3/series", {"X-Api-Key": key})
if err:
print(f" Sonarr: {err}")
return []
out = []
for x in data or []:
seasons = len([y for y in (x.get("seasons") or []) if (y.get("seasonNumber") or 0) > 0])
eps, err2 = get(f"{url}/api/v3/episode?seriesId={x.get('id')}", {"X-Api-Key": key})
by_season = {}
for e in (eps or []):
sn = e.get("seasonNumber")
if not sn or sn <= 0:
continue
item = by_season.setdefault(sn, {"episodes": 0, "aired": 0, "years": set()})
item["episodes"] += 1
ad = e.get("airDateUtc") or ""
if ad:
item["aired"] += 1
item["years"].add(ad[:4])
seasons_info = []
for sn in sorted(by_season):
info = by_season[sn]
years = sorted(info["years"])
seasons_info.append({
"season": sn,
"episodes": info["episodes"],
"aired": info["aired"],
"year": years[0] if years else None,
})
raw_title = x.get("title") or ""
clean = re.sub(r"\s*\(\d{4}\)\s*$", "", raw_title).strip() or raw_title
out.append({
"title": clean, "year": x.get("year"),
"tvdb": x.get("tvdbId"), "genres": x.get("genres") or [],
"network": x.get("network"), "seasons": seasons,
"status": x.get("status"),
"seasons_info": seasons_info,
})
return out
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 plural_ep(n):
n = int(n or 0)
if n % 10 == 1 and n % 100 != 11:
return str(n) + " эпизод"
if n % 10 in (2, 3, 4) and n % 100 not in (12, 13, 14):
return str(n) + " эпизода"
return str(n) + " эпизодов"
def music_library(root=None):
"""Музыка на диске: исполнитель → альбом → трек."""
root = root or os.environ.get("MUSIC_DIR", "/mnt/media/music")
out = []
if not os.path.isdir(root):
return out
for artist in sorted(os.listdir(root)):
apath = os.path.join(root, artist)
if not os.path.isdir(apath):
continue
for album in sorted(os.listdir(apath)):
bpath = os.path.join(apath, album)
if not os.path.isdir(bpath):
continue
tracks = [f for f in sorted(os.listdir(bpath))
if f.lower().endswith((".mp3", ".flac", ".ogg", ".m4a", ".wav"))]
if tracks:
out.append({"artist": artist, "album": album,
"tracks": [os.path.splitext(t)[0] for t in tracks]})
return out
def decade(year):
try:
y = int(year)
except (TypeError, ValueError):
return None
return f"{(y // 10) * 10}-е"
def main():
mv = movies()
bk = books()
sr = series()
print(f"фильмов: {len(mv)}, книг: {len(bk)}, сериалов: {len(sr)}")
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("Crew") or []):
if (c.get("Job") or "") in ("Original Music Composer", "Composer", "Music") and c.get("Name"):
cid = card("композитор", "person:" + norm(c["Name"]), c["Name"])
link(mid, cid, "музыка")
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 x in sr:
sid = card("сериал", "sonarr:" + str(x["tvdb"]), f"{x['title']} ({x['year']})",
"в медиатеке")
for g in x["genres"]:
link(sid, card("жанр", "genre:" + norm(g), g), "жанр")
if x["network"]:
link(sid, card("сеть", "network:" + norm(x["network"]), x["network"]), "показывает")
dec = decade(x["year"])
if dec:
link(sid, card("эпоха", "decade:" + dec, dec), "снят в")
for x in sr:
sid = ids.get("sonarr:" + str(x["tvdb"]))
for info in x.get("seasons_info") or []:
s_key = "season:" + str(x["tvdb"]) + ":" + str(info["season"])
scard = card("сезон", s_key, x["title"] + " — сезон " + str(info["season"]),
plural_ep(info["episodes"]))
link(sid, scard, "содержит")
if info.get("year"):
dec = decade(info["year"])
if dec:
link(scard, card("эпоха", "decade:" + dec, dec), "снят в")
# похожие сериалы по жанрам
made_s = 0
for i, a in enumerate(sr):
for b in sr[i + 1:]:
if len(set(a["genres"]) & set(b["genres"])) >= 2 and made_s < 20:
link(ids.get("sonarr:" + str(a["tvdb"])), ids.get("sonarr:" + str(b["tvdb"])), "похож")
made_s += 1
# ── музыка ──
mu = music_library()
if mu:
print(f" музыка: {len(mu)} альбомов")
for item in mu:
art = card("исполнитель", "artist:" + norm(item["artist"]), item["artist"])
alb = card("альбом", "album:" + norm(item["artist"]) + ":" + norm(item["album"]),
item["artist"] + " — " + item["album"], str(len(item["tracks"])) + " треков")
link(art, alb, "выпустил")
for t in item["tracks"]:
tr = card("трек", "track:" + norm(item["artist"]) + ":" + norm(t), t)
link(alb, tr, "содержит")
# ── книги ──
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}"), "экранизация")
# ── автор → экранизации его книг ──
for b in bk:
film_ids = [ids.get("tmdb:" + str(t)) for t in by_title.get(norm(b["title"]), [])]
if not film_ids:
continue
for a in b.get("authors") or []:
aid = ids.get("author:" + norm(a))
for fid in film_ids:
link(aid, fid, "экранирован")
# ── книга ↔ сериал, серия книг ↔ сериал ──
for x in sr:
xnorm = norm(x["title"])
for b in bk:
if norm(b["title"]) == xnorm:
link(ids.get("calibre:" + str(b["id"])), ids.get("sonarr:" + str(x["tvdb"])), "экранизация")
if b.get("series") and norm(b["series"]) == xnorm:
link(ids.get("series:" + norm(b["series"])), ids.get("sonarr:" + str(x["tvdb"])),
"общая вселенная")
# ── продолжения ──
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())