---
type: "Source File"
title: "!/usr/bin/env python3"
description: "#!/usr/bin/env python3 \"\"\"Rescore batch 2 - processes the SECOND HALF of articles (by id ASC offset). Runs in parallel with rescore_v3.py which handles the first half. \"\"\" import logging, time, os LOG_FILE =."
resource: "https://www.copecheck.com"
tags: ["copecheck", "main", "source-file", "py"]
timestamp: "2026-05-11T12:25:06Z"
site: "main"
source_path: "copecheck/rescore_batch2.py"
---

# !/usr/bin/env python3

Source path: `copecheck/rescore_batch2.py`  
Index snapshot generated: `2026-11-24T13:42:54Z`  
File modified: `2026-05-11T12:25:06Z`

```py
#!/usr/bin/env python3
"""Rescore batch 2 - processes the SECOND HALF of articles (by id ASC offset).
Runs in parallel with rescore_v3.py which handles the first half.
"""

import logging, time, os

LOG_FILE = "/home/ben/infra/copecheck/logs/rescore_batch2.log"

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s %(levelname)s %(name)s | %(message)s",
    handlers=[logging.StreamHandler(), logging.FileHandler(LOG_FILE)],
)
log = logging.getLogger("rescore_batch2")

# Retry wrapper for MiniMax calls
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()

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

total = len(all_articles)
midpoint = total // 2  # ~680
articles = all_articles[midpoint:]  # second half only

log.info("Total articles: %d | Batch2 starts at index %d | Processing %d articles",
         total, midpoint, len(articles))

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

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("Batch2 progress: %d/%d (%.0f/min) | %d entries | %d errs | %.0fs",
                 i, len(articles), rate, n, errors, el)

    # Dedup guard: skip if this article already has ANY cope_entries
    with db.conn() as c:
        existing = c.execute(
            "SELECT COUNT(*) FROM cope_entries WHERE article_slug = ?",
            (art["slug"],),
        ).fetchone()[0]
    if existing > 0:
        continue

    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]

log.info("=== BATCH2 RESCORE COMPLETE in %.0fs (%.1fmin) ===", el, el / 60)
log.info("Final entries: %d | Figures: %d | Errors: %d", final, figs, errors)
```
