feat(newsletter): scan forward for multiple issues and re-enable cron

Loop through consecutive newsletter numbers until one is missing, so
backlog gets delivered in a single run. Use httpx for 404 check and
point to absolute claude binary path for cron. Enable job in config.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-21 05:45:10 +00:00
parent 9314d63aa0
commit 000b406c8d
3 changed files with 84 additions and 75 deletions

View File

@@ -20,7 +20,7 @@ PROJECT_ROOT = Path(__file__).resolve().parent.parent
STATE_FILE = PROJECT_ROOT / "cron" / "newsletter-cercetasi-state.json"
KB_PROMPT_FILE = PROJECT_ROOT / "memory" / "kb" / "projects" / "grup-sprijin" / "prompt-newsletter-cercetasi.md"
CONFIG_FILE = PROJECT_ROOT / "config.json"
CLAUDE_BIN = "claude"
CLAUDE_BIN = "/home/moltbot/.local/bin/claude"
CLAUDE_TIMEOUT = 300
NEWSLETTER_BASE_URL = "https://cercetaiis-newsletter.beehiiv.com/p/newsletter-{n}-din-{year}"
@@ -44,14 +44,9 @@ def write_state(state: dict):
def newsletter_exists(n: int, year: int) -> bool:
url = NEWSLETTER_BASE_URL.format(n=n, year=year)
try:
req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"})
with urllib.request.urlopen(req, timeout=10) as resp:
return resp.status == 200
except urllib.error.HTTPError as e:
if e.code == 404:
return False
log(f"HTTP check failed: {e}")
return False
with httpx.Client(follow_redirects=False, timeout=10) as client:
resp = client.get(url, headers={"User-Agent": "Mozilla/5.0"})
return resp.status_code == 200
except Exception as e:
log(f"HTTP check failed: {e}")
return False
@@ -202,76 +197,86 @@ def main():
log(f"New year detected ({state['year']}{current_year}), resetting counter")
state = {"last_sent": 0, "year": current_year}
next_n = state["last_sent"] + 1
log(f"Checking for newsletter #{next_n}/{current_year}...")
if not newsletter_exists(next_n, current_year):
log(f"Newsletter #{next_n}/{current_year} not yet available. Exiting.")
return
log(f"Newsletter #{next_n}/{current_year} found! Generating summary...")
summary = generate_summary(next_n, current_year)
if not summary:
log("Summary generation failed. Exiting.")
sys.exit(1)
success = False
# Send to Discord
config = json.loads(CONFIG_FILE.read_text())
discord_token = get_discord_token()
channel_id = get_discord_channel_id()
if discord_token and channel_id:
log(f"Sending {len(summary)} chars to Discord channel {channel_id}...")
if send_discord(channel_id, discord_token, summary):
log(f"Discord: sent successfully.")
success = True
else:
log("Discord send failed.")
else:
log("Discord token or channel ID missing, skipping.")
# Send to Telegram
telegram_token = get_telegram_token()
if telegram_token:
config = json.loads(CONFIG_FILE.read_text())
telegram_chat_id = config.get("newsletter_cercetasi", {}).get("telegram_chat_id", "5040014994")
log(f"Sending {len(summary)} chars to Telegram chat {telegram_chat_id}...")
if send_telegram(telegram_token, telegram_chat_id, summary):
log(f"Telegram: sent successfully.")
success = True
else:
log("Telegram send failed.")
else:
log("Telegram token missing, skipping.")
# Send to WhatsApp
config = json.loads(CONFIG_FILE.read_text())
telegram_chat_id = config.get("newsletter_cercetasi", {}).get("telegram_chat_id", "5040014994")
bridge_url = config.get("whatsapp", {}).get("bridge_url", "http://127.0.0.1:8098")
owner_phone = config.get("whatsapp", {}).get("owner", "")
if owner_phone:
wa_to = f"{owner_phone}@s.whatsapp.net"
log(f"Sending {len(summary)} chars to WhatsApp {owner_phone}...")
try:
with httpx.Client(timeout=15) as client:
resp = client.post(f"{bridge_url}/send", json={"to": wa_to, "text": summary})
if resp.status_code == 200 and resp.json().get("ok"):
log("WhatsApp: sent successfully.")
success = True
else:
log(f"WhatsApp send failed: {resp.text[:200]}")
except Exception as e:
log(f"WhatsApp send error: {e}")
else:
log("WhatsApp owner not configured, skipping.")
if success:
state["last_sent"] = next_n
state["year"] = current_year
state["last_sent_at"] = datetime.now(timezone.utc).isoformat()
write_state(state)
log(f"Newsletter #{next_n}/{current_year} done.")
else:
log("All sends failed — will retry next run.")
any_error = False
next_n = state["last_sent"] + 1
# Scan forward: process all available newsletters, not just the next one
while True:
log(f"Checking for newsletter #{next_n}/{current_year}...")
if not newsletter_exists(next_n, current_year):
log(f"Newsletter #{next_n}/{current_year} not yet available. Stopping.")
break
log(f"Newsletter #{next_n}/{current_year} found! Generating summary...")
summary = generate_summary(next_n, current_year)
if not summary:
log(f"Summary generation failed for #{next_n}. Will retry next run.")
any_error = True
break
success = False
# Send to Discord
if discord_token and channel_id:
log(f"Sending #{next_n} to Discord channel {channel_id}...")
if send_discord(channel_id, discord_token, summary):
log("Discord: sent successfully.")
success = True
else:
log("Discord send failed.")
else:
log("Discord token or channel ID missing, skipping.")
# Send to Telegram
if telegram_token:
log(f"Sending #{next_n} to Telegram chat {telegram_chat_id}...")
if send_telegram(telegram_token, telegram_chat_id, summary):
log("Telegram: sent successfully.")
success = True
else:
log("Telegram send failed.")
else:
log("Telegram token missing, skipping.")
# Send to WhatsApp
if owner_phone:
wa_to = f"{owner_phone}@s.whatsapp.net"
log(f"Sending #{next_n} to WhatsApp {owner_phone}...")
try:
with httpx.Client(timeout=15) as client:
resp = client.post(f"{bridge_url}/send", json={"to": wa_to, "text": summary})
if resp.status_code == 200 and resp.json().get("ok"):
log("WhatsApp: sent successfully.")
success = True
else:
log(f"WhatsApp send failed: {resp.text[:200]}")
except Exception as e:
log(f"WhatsApp send error: {e}")
else:
log("WhatsApp owner not configured, skipping.")
if success:
state["last_sent"] = next_n
state["year"] = current_year
state["last_sent_at"] = datetime.now(timezone.utc).isoformat()
write_state(state)
log(f"Newsletter #{next_n}/{current_year} done. State saved.")
next_n += 1
else:
log(f"All sends failed for #{next_n} — will retry next run.")
any_error = True
break
if any_error:
sys.exit(1)