Files
buryan-infra/ops/digest/family_digest.py
T
root 9d49554ea1
lint / hygiene (push) Failing after 21s
standard / check (push) Successful in 8s
chore(audit): auto-snapshot and CHANGELOG update 2026-11-08
- LXC running: 30/30
- Alerts (24h): 0
- Disk PVE: 30%
2026-11-08 09:00:07 +03:00

308 lines
9.7 KiB
Python

#!/usr/bin/env python3
"""Дайджест «Что нового»: контент и новые возможности, без системного шума.
Правила:
• запускается раз в сутки в 12:00;
• если нового нет — не отправляет ничего;
• считает только реально появившийся контент (файлы), а не «добавлено в список».
"""
import json
import os
import re
import subprocess
import sys
import time
import urllib.parse
import urllib.request
from datetime import datetime, timedelta
BOTS_ENV = "/etc/hub-bots/bots.env"
DM_ENV = "/etc/hub-bots/dm.env"
WHATS_NEW = "/root/buryan-infra/ops/product/WHATS-NEW.md"
def env(path):
out = {}
try:
for line in open(path, encoding="utf-8"):
line = line.strip()
if line and not line.startswith("#") and "=" in line:
k, v = line.split("=", 1)
out[k] = v
except Exception:
pass
return out
def get_json(url, headers=None, timeout=25):
req = urllib.request.Request(url, headers=headers or {})
with urllib.request.urlopen(req, timeout=timeout) as r:
return json.loads(r.read() or b"{}")
def sh(cmd, timeout=90):
r = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=timeout)
return (r.stdout or "").strip()
def books(days):
out = sh(f"pct exec 111 -- python3 /root/calibre_query.py {days}")
try:
return json.loads(out or "[]")
except Exception:
return []
def media(days):
"""Только то, что реально появилось (файл скачан)."""
ts = int(time.time()) - days * 86400
bots = env(BOTS_ENV)
items = []
for base, key, path, kind in (
("http://192.168.1.118:7878", bots.get("RADARR_KEY"), "/api/v3/movie", "movie"),
("http://192.168.1.117:8989", bots.get("SONARR_KEY"), "/api/v3/series", "series"),
):
if not key:
continue
try:
data = get_json(base + path, {"X-Api-Key": key})
except Exception:
continue
for x in data:
st = x.get("statistics") or {}
files = st.get("movieFileCount") or st.get("episodeFileCount") or 0
if files <= 0:
continue
added = x.get("added") or ""
try:
t = datetime.fromisoformat(added.replace("Z", "+00:00")).timestamp()
except Exception:
continue
if t >= ts:
items.append((kind, x.get("title")))
return items
def photos(days):
key_file = "/root/.immich-key-new"
if not os.path.exists(key_file):
return None
key = open(key_file, encoding="utf-8").read().strip()
try:
after = (datetime.now().astimezone() - timedelta(days=days)).isoformat()
url = ("http://192.168.1.104:2283/api/search/metadata?"
+ urllib.parse.urlencode({"take": 1, "takenAfter": after}))
d = get_json(url, {"x-api-key": key})
total = d.get("total")
return int(total) if total else 0
except Exception:
return None
def documents(days):
token = "/root/.paperless-token"
if not os.path.exists(token):
return None
t = open(token, encoding="utf-8").read().strip()
try:
day = (datetime.now() - timedelta(days=days)).date().isoformat()
url = f"http://192.168.1.105:8000/api/documents/?created__gte={day}&page_size=1"
d = get_json(url, {"Authorization": "Token " + t})
return d.get("count")
except Exception:
return None
def new_features(days):
if not os.path.exists(WHATS_NEW):
return []
text = open(WHATS_NEW, encoding="utf-8").read()
blocks = re.split(r"\n##\s+", text)
cutoff = datetime.now().date() - timedelta(days=days)
out = []
for b in blocks[1:]:
head = b.splitlines()[0].strip()
m = re.match(r"(\d{4}-\d{2}-\d{2})", head)
if not m:
continue
try:
d = datetime.fromisoformat(m.group(1)).date()
except Exception:
continue
if d >= cutoff:
body = "\n".join(b.splitlines()[1:]).strip()
out.append((head, body))
return out
def projects():
"""Активные проекты комбайна."""
try:
d = get_json("http://127.0.0.1:8103/list")
return (d.get("projects") or [])[:5]
except Exception:
return []
def cameras():
"""Состояние камер во Frigate."""
try:
d = get_json("http://192.168.1.126:5000/api/stats")
cams = d.get("cameras") or {}
online = sum(1 for c in cams.values() if c.get("camera_fps", 0) > 0)
return {"total": len(cams), "online": online}
except Exception:
return None
def llm_progress():
"""Прогресс LLM-сбора документации Yealink."""
import glob
import json as _json
done = total = 0
for f in glob.glob("/opt/work-hub/research/worker-*.log"):
try:
last = None
for line in open(f, encoding="utf-8", errors="ignore"):
m = re.search(r"\[(\d+)/(\d+)\]", line)
if m:
last = (int(m.group(1)), int(m.group(2)))
if last:
done += last[0]
total = max(total, last[1])
except Exception:
continue
if not total:
return None
facts = 0
try:
for f in glob.glob("/opt/work-hub/research/results-*.jsonl"):
for line in open(f, encoding="utf-8", errors="ignore"):
line = line.strip()
if not line:
continue
try:
d = _json.loads(line)
except Exception:
continue
rec = (d.get("data") or [{}])[0] if d.get("data") else {}
facts += len(rec.get("facts") or [])
except Exception:
pass
return {"done": done, "of": total, "facts": facts}
def build(days):
books_l = books(days)
media_l = media(days)
photo_n = photos(days) or 0
doc_n = documents(days) or 0
feats = new_features(days)
projs = projects()
cams = cameras()
llm = llm_progress()
has_content = bool(books_l or media_l or photo_n or doc_n or feats
or projs or cams or llm)
if not has_content:
return None
lines = [f"📰 **Что нового в Buryans Hub**",
f"_{datetime.now():%d.%m.%Y}_", ""]
if books_l:
lines.append(f"📚 **Книги ({len(books_l)}):**")
for t in books_l[:8]:
lines.append(f"• {t}")
lines.append("")
if media_l:
lines.append(f"🎬 **Медиатека ({len(media_l)}):**")
for kind, title in media_l[:8]:
lines.append(f"• {title}")
lines.append("")
if photo_n:
lines.append(f"📷 **Фото:** {photo_n} новых")
lines.append("")
if doc_n:
lines.append(f"🧾 **Документы:** {doc_n} новых")
lines.append("")
if feats:
lines.append("🆕 **Новые возможности**")
for head, body in feats[:5]:
title = head.split("—", 1)[-1].strip() if "—" in head else head
lines.append(f"• **{title}**")
for row in body.splitlines()[:3]:
if row.strip():
lines.append(" " + row.strip())
lines.append("")
lines.append("_Проверить в любой момент: `!что нового`_")
if projs:
lines.append("")
lines.append("📐 **Проекты (%d):**" % len(projs))
for pj in projs[:5]:
lines.append("• %s — %s комн., %s ед." % (
pj.get("name"), pj.get("rooms", 0), pj.get("units", 0)))
if cams:
lines.append("")
lines.append("🎥 **Камеры:** %d из %d на связи" % (cams["online"], cams["total"]))
if llm:
lines.append("")
pct = round(100.0 * llm["done"] / max(llm["of"], 1), 1)
lines.append("🤖 **LLM-сбор документации:** %.1f%% (%d фактов)" % (pct, llm["facts"]))
return "\n".join(lines)
def send_matrix(text):
e = env(BOTS_ENV)
token = e.get("MEDIA_TOKEN")
hs = e.get("HS", "https://matrix.buryans.ru").rstrip("/")
rooms = []
try:
for line in open(DM_ENV, encoding="utf-8"):
if line.startswith("MEDIA="):
rooms.append(line.split("=", 1)[1].strip())
except Exception:
pass
sent = 0
for room in rooms:
url = (hs + "/_matrix/client/r0/rooms/" + urllib.parse.quote(room, safe="")
+ "/send/m.room.message/" + str(int(time.time() * 1000)))
body = json.dumps({"msgtype": "m.text", "body": text}).encode()
req = urllib.request.Request(url, data=body, method="PUT",
headers={"Authorization": "Bearer " + token,
"Content-Type": "application/json"})
try:
urllib.request.urlopen(req, timeout=20).read()
sent += 1
except Exception as ex:
print("send error:", ex)
return sent
def main():
days = 1
if "--days" in sys.argv:
days = int(sys.argv[sys.argv.index("--days") + 1])
text = build(days)
if text is None:
print("нового нет — не отправляем")
return
if "--print" in sys.argv:
print(text)
return
n = send_matrix(text)
print(f"отправлено в {n} комнат(ы)")
if __name__ == "__main__":
main()