#!/usr/bin/env python3 """M-22/M-23: обслуживание медиа — дубли, осиротевшие файлы, крупные файлы. Пишет отчёт в Matrix и в лог. Ничего не удаляет автоматически. """ import json import os import time import urllib.parse import urllib.request MEDIA = "/mnt/media" BOTS_ENV = "/etc/hub-bots/bots.env" DM_ENV = "/etc/hub-bots/dm.env" REPORT = "/var/lib/hub-media/report.json" MIN_SIZE = 50 * 1024 * 1024 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 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 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=15).read() except Exception as ex: print("matrix:", ex) def scan(): files = [] for root, dirs, names in os.walk(MEDIA): dirs[:] = [d for d in dirs if not d.startswith(".")] for n in names: if not n.lower().endswith((".mkv", ".mp4", ".avi", ".ts", ".m4v", ".mov")): continue p = os.path.join(root, n) try: st = os.stat(p) except Exception: continue files.append({"path": p, "size": st.st_size, "mtime": st.st_mtime, "name": n}) return files def main(): os.makedirs("/var/lib/hub-media", exist_ok=True) files = scan() total = sum(f["size"] for f in files) # дубли по имени+размеру seen = {} dups = [] for f in files: key = (f["name"].lower(), f["size"]) if key in seen: dups.append((seen[key], f["path"])) else: seen[key] = f["path"] # крупные и старые big = sorted(files, key=lambda x: -x["size"])[:5] old = [f for f in files if (time.time() - f["mtime"]) > 365 * 86400] report = { "ts": time.strftime("%Y-%m-%dT%H:%M:%S"), "files": len(files), "total_gb": round(total / 1024 ** 3, 1), "duplicates": dups[:10], "old_files": len(old), "biggest": [{"name": f["name"][:70], "gb": round(f["size"] / 1024 ** 3, 1)} for f in big], } json.dump(report, open(REPORT, "w"), ensure_ascii=False, indent=1) lines = [f"🎬 **Обслуживание медиа**", f"файлов: {report['files']}, всего {report['total_gb']} ГБ"] if dups: lines.append(f"⚠️ возможных дублей: {len(dups)}") for a, b in dups[:3]: lines.append(f" • {os.path.basename(a)} ↔ {os.path.basename(b)}") if old: lines.append(f"🕸 файлов старше года: {len(old)}") if big: lines.append("📦 крупнейшие: " + ", ".join( f"{b['name']} ({b['gb']} ГБ)" for b in report["biggest"][:3])) text = "\n".join(lines) print(text) matrix(text) if __name__ == "__main__": main()