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

@@ -49,7 +49,7 @@
} }
}, },
"newsletter_cercetasi": { "newsletter_cercetasi": {
"enabled": false, "enabled": true,
"cron": "0 17 * * 4,5,1", "cron": "0 17 * * 4,5,1",
"channel": "echo-core" "channel": "echo-core"
}, },

View File

@@ -1 +1,5 @@
{"last_sent": 13, "year": 2026, "last_sent_at": "2026-04-02T18:18:46.000000+00:00"} {
"last_sent": 14,
"year": 2026,
"last_sent_at": "2026-04-09T14:23:55.586085+00:00"
}

View File

@@ -20,7 +20,7 @@ PROJECT_ROOT = Path(__file__).resolve().parent.parent
STATE_FILE = PROJECT_ROOT / "cron" / "newsletter-cercetasi-state.json" STATE_FILE = PROJECT_ROOT / "cron" / "newsletter-cercetasi-state.json"
KB_PROMPT_FILE = PROJECT_ROOT / "memory" / "kb" / "projects" / "grup-sprijin" / "prompt-newsletter-cercetasi.md" KB_PROMPT_FILE = PROJECT_ROOT / "memory" / "kb" / "projects" / "grup-sprijin" / "prompt-newsletter-cercetasi.md"
CONFIG_FILE = PROJECT_ROOT / "config.json" CONFIG_FILE = PROJECT_ROOT / "config.json"
CLAUDE_BIN = "claude" CLAUDE_BIN = "/home/moltbot/.local/bin/claude"
CLAUDE_TIMEOUT = 300 CLAUDE_TIMEOUT = 300
NEWSLETTER_BASE_URL = "https://cercetaiis-newsletter.beehiiv.com/p/newsletter-{n}-din-{year}" 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: def newsletter_exists(n: int, year: int) -> bool:
url = NEWSLETTER_BASE_URL.format(n=n, year=year) url = NEWSLETTER_BASE_URL.format(n=n, year=year)
try: try:
req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"}) with httpx.Client(follow_redirects=False, timeout=10) as client:
with urllib.request.urlopen(req, timeout=10) as resp: resp = client.get(url, headers={"User-Agent": "Mozilla/5.0"})
return resp.status == 200 return resp.status_code == 200
except urllib.error.HTTPError as e:
if e.code == 404:
return False
log(f"HTTP check failed: {e}")
return False
except Exception as e: except Exception as e:
log(f"HTTP check failed: {e}") log(f"HTTP check failed: {e}")
return False return False
@@ -202,76 +197,86 @@ def main():
log(f"New year detected ({state['year']}{current_year}), resetting counter") log(f"New year detected ({state['year']}{current_year}), resetting counter")
state = {"last_sent": 0, "year": current_year} state = {"last_sent": 0, "year": current_year}
next_n = state["last_sent"] + 1 config = json.loads(CONFIG_FILE.read_text())
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
discord_token = get_discord_token() discord_token = get_discord_token()
channel_id = get_discord_channel_id() 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() telegram_token = get_telegram_token()
if telegram_token: telegram_chat_id = config.get("newsletter_cercetasi", {}).get("telegram_chat_id", "5040014994")
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())
bridge_url = config.get("whatsapp", {}).get("bridge_url", "http://127.0.0.1:8098") bridge_url = config.get("whatsapp", {}).get("bridge_url", "http://127.0.0.1:8098")
owner_phone = config.get("whatsapp", {}).get("owner", "") 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: any_error = False
state["last_sent"] = next_n next_n = state["last_sent"] + 1
state["year"] = current_year
state["last_sent_at"] = datetime.now(timezone.utc).isoformat() # Scan forward: process all available newsletters, not just the next one
write_state(state) while True:
log(f"Newsletter #{next_n}/{current_year} done.") log(f"Checking for newsletter #{next_n}/{current_year}...")
else:
log("All sends failed — will retry next run.") 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) sys.exit(1)