79 lines
2.8 KiB
Python
79 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Отправка короткой сводки гигиены в Matrix (комната INFRA)."""
|
|
import json
|
|
import re
|
|
import sys
|
|
import time
|
|
import urllib.parse
|
|
import urllib.request
|
|
from datetime import date
|
|
|
|
REPORT = "/root/buryan-infra/ops/hygiene/report-%s.md" % date.today().isoformat()
|
|
|
|
|
|
def read_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 main():
|
|
try:
|
|
text = open(REPORT, encoding="utf-8").read()
|
|
except Exception as e:
|
|
print("нет отчёта:", e)
|
|
return 1
|
|
|
|
m = re.search(r"(?:находок|Найдено):\s*\*\*(\d+)\*\*", text, re.I)
|
|
total = m.group(1) if m else "?"
|
|
m2 = re.search(r"· high: (\d+) · medium: (\d+) · low: (\d+) · info: (\d+)", text)
|
|
counts = m2.groups() if m2 else ("?", "?", "?", "?")
|
|
|
|
highs = re.findall(r"## \[HIGH\] (.+)\n\n\*\*Что:\*\* (.+)", text)
|
|
plan = re.search(r"## Предложения агента.*?```\n(.*?)```", text, re.S)
|
|
|
|
lines = ["🧹 Гигиена: найдено %s (high %s, medium %s)" % (total, counts[0], counts[1])]
|
|
for kind, what in highs[:3]:
|
|
lines.append("• [%s] %s" % (kind, what[:110]))
|
|
if plan:
|
|
p = [x.strip() for x in plan.group(1).splitlines() if x.strip().startswith("[")]
|
|
if p:
|
|
lines.append("— агент предлагает (dry-run):")
|
|
for x in p[:3]:
|
|
lines.append(" " + x[:110])
|
|
lines.append("полный отчёт: ops/hygiene/report-%s.md" % date.today().isoformat())
|
|
msg = "\n".join(lines)
|
|
|
|
env = read_env("/etc/hub-bots/bots.env")
|
|
token = env.get("INFRA_TOKEN") or env.get("MEDIA_TOKEN")
|
|
hs = (env.get("HS") or "https://matrix.buryans.ru").rstrip("/")
|
|
room = read_env("/etc/hub-bots/dm.env").get("INFRA") or read_env("/etc/hub-bots/dm.env").get("MEDIA")
|
|
if not (token and room):
|
|
print("нет токена/комнаты")
|
|
return 1
|
|
url = "%s/_matrix/client/v3/rooms/%s/send/m.room.message/%d" % (
|
|
hs, urllib.parse.quote(room, safe=""), int(time.time() * 1000))
|
|
req = urllib.request.Request(url, data=json.dumps({"msgtype": "m.text", "body": msg}).encode(),
|
|
method="PUT")
|
|
req.add_header("Authorization", "Bearer " + token)
|
|
req.add_header("Content-Type", "application/json")
|
|
try:
|
|
with urllib.request.urlopen(req, timeout=30) as r:
|
|
print("отправлено:", r.status)
|
|
except Exception as e:
|
|
print("ошибка отправки:", type(e).__name__, str(e)[:150])
|
|
return 1
|
|
print(msg)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|