---
type: "Source File"
title: "!/usr/bin/env python3"
description: "#!/usr/bin/env python3 # Rescore v3 - fixed retry wrapper import logging, time logging.basicConfig( level=logging.INFO, format=\"%(asctime)s %(levelname)s %(name)s | %(message)s\", handlers=[logging.StreamHandler()."
resource: "https://www.copecheck.com"
tags: ["copecheck", "main", "source-file", "py"]
timestamp: "2026-05-11T11:53:43Z"
site: "main"
source_path: "copecheck/rescore_v3.py"
---

# !/usr/bin/env python3

Source path: `copecheck/rescore_v3.py`  
Index snapshot generated: `2026-11-24T13:42:54Z`  
File modified: `2026-05-11T11:53:43Z`

```py
#!/usr/bin/env python3
# Rescore v3 - fixed retry wrapper

import logging, time

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s %(levelname)s %(name)s | %(message)s",
    handlers=[logging.StreamHandler(), logging.FileHandler("/home/ben/infra/copecheck/logs/rescore_v3.log")],
)
log = logging.getLogger("rescore_v3")

import oracle
_orig = oracle.score_cope

def _retry(*args, **kwargs):
    retries = 3
    base = 5
    for a in range(retries + 1):
        try:
            return _orig(*args, **kwargs)
        except Exception as e:
            if a < retries:
                d = base * (2 ** a)
                log.warning("retry %d in %ds: %s", a+1, d, str(e)[:200])
                time.sleep(d)
            else:
                raise

oracle.score_cope = _retry

import db, pipeline
db.init()
pipeline.sync_figures()

with db.conn() as c:
    old = c.execute("SELECT COUNT(*) FROM cope_entries").fetchone()[0]
    log.info("Current cope_entries: %d", old)

with db.conn() as c:
    cur = c.execute("SELECT * FROM articles WHERE status = ? AND body IS NOT NULL ORDER BY id ASC", ("analysed",))
    articles = [dict(r) for r in cur.fetchall()]

log.info("Rescoring %d articles", len(articles))
errors = 0
t0 = time.time()

for i, art in enumerate(articles):
    if i % 50 == 0:
        el = time.time() - t0
        with db.conn() as c:
            n = c.execute("SELECT COUNT(*) FROM cope_entries").fetchone()[0]
        rate = i / el * 60 if el > 0 else 0
        log.info("Progress: %d/%d (%.0f/min) | %d entries | %d errs | %.0fs", i, len(articles), rate, n, errors, el)
    try:
        pipeline._crosslink_figures(art, {"verdict_md": art.get("verdict_md", "")})
    except Exception as e:
        errors += 1
        log.warning("Error %s: %s", art.get("slug","?")[:40], str(e)[:200])
        time.sleep(3)
        if errors > 100:
            log.error("Too many errors, aborting")
            break

el = time.time() - t0
with db.conn() as c:
    final = c.execute("SELECT COUNT(*) FROM cope_entries").fetchone()[0]
    figs = c.execute("SELECT COUNT(DISTINCT figure_id) FROM cope_entries").fetchone()[0]
    top = c.execute("SELECT f.name, f.cope_score, f.total_quotes FROM figures f WHERE f.total_quotes > 0 ORDER BY f.cope_score DESC").fetchall()

log.info("=== RESCORE COMPLETE in %.0fs (%.1fmin) ===", el, el/60)
log.info("New entries: %d | Figures: %d | Errors: %d", final, figs, errors)
for f in top:
    log.info("  %s: score=%.1f quotes=%d", f[0], f[1], f[2])
```
