feat: top přispěvatel, nový účet, milník v reportu

This commit is contained in:
2026-05-04 19:31:00 +02:00
parent 11f7eab2ef
commit a22d761211
2 changed files with 99 additions and 8 deletions
+72 -6
View File
@@ -126,7 +126,8 @@ def format_month_cs(dt):
]
return f"{months[dt.month - 1]} {dt.year}"
def build_monthly_toot(measures_data, tags, top_tooty, date_to, prev_stats, instance_info, total_count=0):
def build_monthly_toot(measures_data, tags, top_tooty, date_to, prev_stats, instance_info,
total_count=0, top_author=None, newest_account=None):
stats = {m["key"]: int(m["total"]) for m in measures_data}
new_users = stats.get("new_users", 0)
active_users = stats.get("active_users", 0)
@@ -142,9 +143,18 @@ def build_monthly_toot(measures_data, tags, top_tooty, date_to, prev_stats, inst
hashtags = " ".join(f"#{t['name']}" for t in tags[:5]) if tags else "(žádné)"
inst_stats = instance_info.get("stats", {}) if instance_info else {}
user_count = inst_stats.get("user_count", "?")
user_count_val = inst_stats.get("user_count", 0)
user_count = user_count_val if user_count_val else "?"
domain_count = inst_stats.get("domain_count", "?")
author_line = f"✍️ Top přispěvatel: @{top_author['acct']} ({top_author['count']} tootů)\n" if top_author else ""
newest_line = f"👋 Nový účet: @{newest_account['acct']}\n" if newest_account else ""
milestone_line = (
f"🎉 Milník: Mamutovo dosáhlo {user_count_val} uživatelů!\n"
if user_count_val and user_count_val % 10 == 0 else ""
)
extra = author_line + newest_line + milestone_line
if top_tooty:
blocks = "\n\n".join(
f"🐘 @{s['acct']}\n\"{truncate(s['text'], 80).replace(chr(10), ' ')}\"\n🔁 {s['reblogs']}{s['favourites']}\n🔗 {s.get('url', '')}"
@@ -167,11 +177,14 @@ def build_monthly_toot(measures_data, tags, top_tooty, date_to, prev_stats, inst
f"🌐 Federovaných instancí: {domain_count}\n"
f"\n"
f"🔥 Top hashtagy měsíce:\n"
f"{hashtags}"
f"{hashtags}\n"
f"\n"
f"{extra}"
f"{tooty_sekce}"
)
def build_toot(measures_data, tags, top_tooty, date_from, date_to, week_number, total_count=0):
def build_toot(measures_data, tags, top_tooty, date_from, date_to, week_number,
total_count=0, top_author=None, newest_account=None, user_count=0):
stats = {m["key"]: int(m["total"]) for m in measures_data}
new_users = stats.get("new_users", 0)
active_users = stats.get("active_users", 0)
@@ -190,6 +203,11 @@ def build_toot(measures_data, tags, top_tooty, date_from, date_to, week_number,
else:
toot_tyden = ""
author_line = f"✍️ Top přispěvatel: @{top_author['acct']} ({top_author['count']} tootů)\n" if top_author else ""
newest_line = f"👋 Nový účet: @{newest_account['acct']}\n" if newest_account else ""
milestone_line = f"🎉 Milník: Mamutovo dosáhlo {user_count} uživatelů!\n" if user_count and user_count % 10 == 0 else ""
extra = author_line + newest_line + milestone_line
date_from_str = format_date_cs(date_from)
date_to_str = format_date_cs(date_to)
year = date_to.year
@@ -207,6 +225,7 @@ def build_toot(measures_data, tags, top_tooty, date_from, date_to, week_number,
f"{hashtags}\n"
f"\n"
f"{toot_tyden}"
f"{extra}"
f"💡 Tip týdne: {tip}"
)
@@ -262,6 +281,37 @@ def load_total_count_from_data(date_to, days):
pass
return total
def load_top_author_from_data(date_to, days):
counts = Counter()
for i in range(days):
day = (date_to - timedelta(days=i)).strftime("%Y-%m-%d")
path = os.path.join("data", f"{day}.json")
try:
with open(path, encoding="utf-8") as f:
file_data = json.load(f)
for acct, n in file_data.get("authors_count", {}).items():
counts[acct] += n
except FileNotFoundError:
pass
if not counts:
return None
acct, n = counts.most_common(1)[0]
return {"acct": acct, "count": n}
def load_newest_account_from_data(date_to, days):
for i in range(days):
day = (date_to - timedelta(days=i)).strftime("%Y-%m-%d")
path = os.path.join("data", f"{day}.json")
try:
with open(path, encoding="utf-8") as f:
file_data = json.load(f)
acc = file_data.get("newest_account")
if acc:
return acc
except FileNotFoundError:
pass
return None
def main():
parser = argparse.ArgumentParser(description="Statistiky Mamutovo.cz")
parser.add_argument("--dry-run", action="store_true", help="Pouze vypíše toot, neodešle")
@@ -315,7 +365,12 @@ def main():
top_tooty = load_tooty_from_data(date_to, 30)
total_count = load_total_count_from_data(date_to, 30)
toot = build_monthly_toot(measures_data, tags, top_tooty, date_to, prev_stats, instance_info, total_count)
top_author = load_top_author_from_data(date_to, 30)
newest_account = load_newest_account_from_data(date_to, 30)
toot = build_monthly_toot(
measures_data, tags, top_tooty, date_to, prev_stats, instance_info,
total_count, top_author, newest_account,
)
if args.dry_run:
print(toot)
@@ -359,9 +414,20 @@ def main():
except Exception:
tags = []
try:
instance_info = api_get(f"{base_url}/api/v1/instance")
except Exception:
instance_info = {}
user_count = instance_info.get("stats", {}).get("user_count", 0)
top_tooty = load_tooty_from_data(date_to, 7)
total_count = load_total_count_from_data(date_to, 7)
toot = build_toot(measures_data, tags, top_tooty, date_from, date_to, week_number, total_count)
top_author = load_top_author_from_data(date_to, 7)
newest_account = load_newest_account_from_data(date_to, 7)
toot = build_toot(
measures_data, tags, top_tooty, date_from, date_to, week_number,
total_count, top_author, newest_account, user_count,
)
if args.dry_run:
print(toot)