#!/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 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) has_content = bool(books_l or media_l or photo_n or doc_n or feats) 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("_Проверить в любой момент: `!что нового`_") 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()