feat: přidány top odkazy týdne z trends/links API
This commit is contained in:
@@ -125,6 +125,19 @@ def main():
|
|||||||
except Exception:
|
except Exception:
|
||||||
pass
|
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")
|
today = datetime.now(timezone.utc).strftime("%Y-%m-%d")
|
||||||
os.makedirs("data", exist_ok=True)
|
os.makedirs("data", exist_ok=True)
|
||||||
out_path = os.path.join("data", f"{today}.json")
|
out_path = os.path.join("data", f"{today}.json")
|
||||||
@@ -137,6 +150,7 @@ def main():
|
|||||||
"newest_account": newest_account,
|
"newest_account": newest_account,
|
||||||
"top": top,
|
"top": top,
|
||||||
"tags": tags,
|
"tags": tags,
|
||||||
|
"top_links": top_links,
|
||||||
}, f, ensure_ascii=False, indent=2)
|
}, f, ensure_ascii=False, indent=2)
|
||||||
|
|
||||||
print(f"Uloženo: {out_path} ({len(timeline)} tootů načteno, {len(top)} top, {len(tags)} hashtagů)")
|
print(f"Uloženo: {out_path} ({len(timeline)} tootů načteno, {len(top)} top, {len(tags)} hashtagů)")
|
||||||
|
|||||||
+33
-2
@@ -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,
|
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}
|
stats = {m["key"]: int(m["total"]) for m in measures_data}
|
||||||
new_users = stats.get("new_users", 0)
|
new_users = stats.get("new_users", 0)
|
||||||
active_users = stats.get("active_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 ""
|
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
|
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_from_str = format_date_cs(date_from)
|
||||||
date_to_str = format_date_cs(date_to)
|
date_to_str = format_date_cs(date_to)
|
||||||
year = date_to.year
|
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"{hashtags}\n"
|
||||||
f"\n"
|
f"\n"
|
||||||
f"{toot_tyden}"
|
f"{toot_tyden}"
|
||||||
|
f"{links_sekce}"
|
||||||
f"{extra}"
|
f"{extra}"
|
||||||
f"💡 Tip týdne: {tip}"
|
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]
|
acct, n = counts.most_common(1)[0]
|
||||||
return {"acct": acct, "count": n}
|
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):
|
def load_newest_account_from_data(date_to, days):
|
||||||
for i in range(days):
|
for i in range(days):
|
||||||
day = (date_to - timedelta(days=i)).strftime("%Y-%m-%d")
|
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)
|
total_count = load_total_count_from_data(date_to, 7)
|
||||||
top_author = load_top_author_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)
|
newest_account = load_newest_account_from_data(date_to, 7)
|
||||||
|
top_links = load_top_links_from_data(date_to, 7)
|
||||||
toot = build_toot(
|
toot = build_toot(
|
||||||
measures_data, tags, top_tooty, date_from, date_to, week_number,
|
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:
|
if args.dry_run:
|
||||||
|
|||||||
Reference in New Issue
Block a user