Files
matrix-ai-agent/cron/formatter.py
2026-03-16 14:16:49 +02:00

57 lines
1.8 KiB
Python

"""Format cron job results as Matrix messages (markdown)."""
def format_search_results(job_name: str, results: list[dict]) -> str:
"""Format Brave Search results as a markdown message for Matrix."""
count = len(results)
lines = [f"**{job_name}** \u2014 {count} new result{'s' if count != 1 else ''}:\n"]
for i, r in enumerate(results, 1):
title = r.get("title", "Untitled")
url = r.get("url", "")
desc = r.get("description", "")
lines.append(f"{i}. **[{title}]({url})**")
if desc:
lines.append(f" {desc}")
lines.append("")
lines.append(
"_[Manage automations](https://matrixhost.eu/settings/automations)_"
)
return "\n".join(lines)
def format_listings(job_name: str, listings: list[dict]) -> str:
"""Format browser-scraped listings as a markdown message for Matrix."""
count = len(listings)
lines = [f"**{job_name}** \u2014 {count} new listing{'s' if count != 1 else ''}:\n"]
for i, item in enumerate(listings, 1):
title = item.get("title", "Unknown")
price = item.get("price", "")
location = item.get("location", "")
url = item.get("url", "")
age = item.get("age", "")
line = f"{i}. **{title}**"
if price:
line += f" \u2014 {price}"
lines.append(line)
details = []
if location:
details.append(f"\U0001f4cd {location}")
if age:
details.append(f"\U0001f4c5 {age}")
if url:
details.append(f"[View listing]({url})")
if details:
sep = " \u00b7 "
lines.append(f" {sep.join(details)}")
lines.append("")
lines.append(
"_[Manage automations](https://matrixhost.eu/settings/automations)_"
)
return "\n".join(lines)