bot: /new и /find читают dict-записи, /stats считает живые источники; режим OMNI_BOT_DRY для проверки без отправки

This commit is contained in:
reform
2026-10-26 12:47:01 +03:00
parent f8000f2f6c
commit e49f711b18

View File

@@ -46,6 +46,9 @@ def _api(method, **params):
def _send(chat_id, text, preview=True):
text = text[:MAX_LEN]
if os.environ.get("OMNI_BOT_DRY"):
print("[DRY] " + text.replace("</b>", "").replace("<b>", "")[:1200])
return {"ok": True}
return _api("sendMessage", chat_id=chat_id, text=text, parse_mode="HTML",
disable_web_page_preview="false" if preview else "true")
@@ -78,8 +81,7 @@ def cmd_start(chat_id, arg):
def cmd_new(chat_id, arg):
n = int(arg) if arg.strip().isdigit() else 8
rows = store.browse2(limit=min(n, 20), sort="fresh")
items = rows.get("items", rows) if isinstance(rows, dict) else rows
items, _total = store.browse2(limit=min(n, 20), sort="fresh")
if not items:
return _send(chat_id, "пусто")
_send(chat_id, "последние %d:\n\n" % len(items) + "\n\n".join(fmt_item(i) for i in items))
@@ -103,7 +105,8 @@ def cmd_find(chat_id, arg):
if not q:
return _send(chat_id, "что искать?")
rows = store.search(q, limit=8)
items = rows.get("items", rows) if isinstance(rows, dict) else rows
keys = ("provider", "kind", "source", "title", "score", "url")
items = [dict(zip(keys, r)) if not isinstance(r, dict) else r for r in rows]
if not items:
return _send(chat_id, "нет совпадений")
_send(chat_id, "поиск: %s\n\n" % _esc(q) + "\n\n".join(fmt_item(i) for i in items))
@@ -160,7 +163,8 @@ def cmd_auto(chat_id, arg):
def cmd_stats(chat_id, arg):
s = store.stats()
a = sources.all_sources()
alive = source_state.alive_count() if hasattr(source_state, "alive_count") else "?"
fl = source_state.flags()
alive = sum(1 for x in a if (fl.get(x.name, {}).get("items") or 0) > 0)
_send(chat_id, "<b>omni</b>\nзаписей %s · источников %d · живых %s\nкатегорий %d · прогонов %s · с ценой %s" % (
s.get("items"), len(a), alive, len(sources.CATEGORIES), s.get("runs"), s.get("priced")))