diff --git a/app.py b/app.py index 921582a..0c7ad6e 100644 --- a/app.py +++ b/app.py @@ -3,6 +3,7 @@ import os import re import json import time +import hashlib import threading import unicodedata import socket @@ -417,6 +418,18 @@ def match_offers_ai(): "SELECT id, heading, catalog FROM offers")] if not offers: return 0 + + # Skip the (daily) match call when the offer set hasn't changed since the + # last successful match — the avis only turns over weekly, so this avoids a + # pointless Claude call most days. + sig = hashlib.md5( + "\n".join(sorted(o['id'] + '|' + (o.get('heading') or '') for o in offers)) + .encode('utf-8')).hexdigest() + with get_db() as c: + if _meta_get(c, 'last_match_sig') == sig and \ + c.execute("SELECT COUNT(*) FROM offer_matches").fetchone()[0]: + return 0 + try: import anthropic except Exception as e: @@ -465,6 +478,7 @@ def match_offers_ai(): for ik, oid in pairs: c.execute("INSERT OR REPLACE INTO offer_matches(item_key,offer_id,updated) " "VALUES(?,?,?)", (ik, oid, now)) + _meta_set(c, 'last_match_sig', sig) # remember this offer set as matched app.logger.info("citti match: %d AI matches", len(pairs)) return len(pairs) @@ -496,6 +510,27 @@ def refresh_offers_safe(): return None +_refreshing = threading.Lock() + + +def refresh_offers_async(): + """Run a refresh in the background. Translation/matching can be slow under + rate limits, so the manual Opdater button must not block the HTTP request + (and time out the browser). Returns True if a run was started, False if one + is already in progress.""" + if not _refreshing.acquire(blocking=False): + return False + + def _run(): + try: + refresh_offers_safe() + finally: + _refreshing.release() + + threading.Thread(target=_run, name="offer-refresh", daemon=True).start() + return True + + def _offers_stale(): with get_db() as c: last = _meta_get(c, 'last_refresh') @@ -843,10 +878,8 @@ def offer_pick(): @app.route('/api/offers/refresh', methods=['POST']) def offer_refresh(): - summary = refresh_offers_safe() - if summary is None: - return jsonify(ok=False, error='refresh failed'), 502 - return jsonify(ok=True, **summary) + started = refresh_offers_async() + return jsonify(ok=True, started=started) @app.route('/koeb-nu') diff --git a/citti.nomad b/citti.nomad index c0214ac..e6ac13e 100644 --- a/citti.nomad +++ b/citti.nomad @@ -97,15 +97,21 @@ job "citti" { HOST = "0.0.0.0" } - # Claude OAuth token for DA translation + AI offer matching. Set as - # ANTHROPIC_AUTH_TOKEN (the SDK reads it as a Bearer token); the app adds - # the required anthropic-beta: oauth-2025-04-20 header. We deliberately do - # NOT set ANTHROPIC_API_KEY — even an empty value would shadow the token. - # keyOrDefault keeps it empty (not a deploy failure) if the KV is unset — - # offers then stay German with heuristic matching. + # Claude credential for DA translation + AI offer matching. + # Prefer a dedicated API key (citti/anthropic_api_key) — its own rate + # budget, so citti doesn't compete with interactive Claude Code sessions + # on the same subscription. Else fall back to the OAuth token, set as + # ANTHROPIC_AUTH_TOKEN (the app adds the oauth-2025-04-20 beta header). + # Only ONE var is written: an empty ANTHROPIC_API_KEY would shadow the + # token, so we never emit it unless the key actually exists. If neither + # is set the token var is empty and offers stay German (heuristic match). template { data = <