From 83d3a323267aa9867594808e182218fb171057f4 Mon Sep 17 00:00:00 2001 From: Archos Date: Mon, 15 Jun 2026 18:56:36 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20p=C5=99id=C3=A1ny=20top=20odkazy=20t?= =?UTF-8?q?=C3=BDdne=20z=20trends/links=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- daily_top.py | 14 ++++++++++++++ weekly_report.py | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/daily_top.py b/daily_top.py index 92ebf79..5a7b06f 100644 --- a/daily_top.py +++ b/daily_top.py @@ -125,6 +125,19 @@ def main(): except Exception: pass + top_links = [] + try: + links_data = api_get(f"{base_url}/api/v1/trends/links?limit=3", token) + for link in links_data[:3]: + top_links.append({ + "url": link.get("url", ""), + "title": link.get("title", ""), + "description": link.get("description", ""), + "provider_name": link.get("provider_name", ""), + }) + except Exception: + pass + today = datetime.now(timezone.utc).strftime("%Y-%m-%d") os.makedirs("data", exist_ok=True) out_path = os.path.join("data", f"{today}.json") @@ -137,6 +150,7 @@ def main(): "newest_account": newest_account, "top": top, "tags": tags, + "top_links": top_links, }, f, ensure_ascii=False, indent=2) print(f"Uloženo: {out_path} ({len(timeline)} tootů načteno, {len(top)} top, {len(tags)} hashtagů)") diff --git a/weekly_report.py b/weekly_report.py index ed713db..ae139c2 100644 --- a/weekly_report.py +++ b/weekly_report.py @@ -184,7 +184,7 @@ def build_monthly_toot(measures_data, tags, top_tooty, date_to, prev_stats, inst ) 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): + total_count=0, top_author=None, newest_account=None, user_count=0, top_links=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) @@ -208,6 +208,15 @@ def build_toot(measures_data, tags, top_tooty, date_from, date_to, week_number, 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 + if top_links: + link_lines = "\n".join( + f"🔗 {link.get('title', link.get('url', ''))}\n{link.get('provider_name', '')} · {link.get('url', '')}" + for link in top_links[:3] + ) + links_sekce = f"🌐 Top odkazy týdne:\n\n{link_lines}\n\n" + else: + links_sekce = "" + date_from_str = format_date_cs(date_from) date_to_str = format_date_cs(date_to) year = date_to.year @@ -225,6 +234,7 @@ def build_toot(measures_data, tags, top_tooty, date_from, date_to, week_number, f"{hashtags}\n" f"\n" f"{toot_tyden}" + f"{links_sekce}" f"{extra}" f"💡 Tip týdne: {tip}" ) @@ -298,6 +308,26 @@ def load_top_author_from_data(date_to, days): acct, n = counts.most_common(1)[0] return {"acct": acct, "count": n} +def load_top_links_from_data(date_to, days): + seen = set() + result = [] + 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 link in file_data.get("top_links", []): + url = link.get("url", "") + if url and url not in seen: + seen.add(url) + result.append(link) + if len(result) >= 3: + return result + except FileNotFoundError: + pass + return result + def load_newest_account_from_data(date_to, days): for i in range(days): day = (date_to - timedelta(days=i)).strftime("%Y-%m-%d") @@ -424,9 +454,10 @@ def main(): total_count = load_total_count_from_data(date_to, 7) top_author = load_top_author_from_data(date_to, 7) newest_account = load_newest_account_from_data(date_to, 7) + top_links = load_top_links_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, + total_count, top_author, newest_account, user_count, top_links, ) if args.dry_run: