---
type: "Source File"
title: "!/usr/bin/env python3"
description: "#!/usr/bin/env python3 \"\"\"READ-ONLY: check every indexable article's source URL for a dead link. Conservative: only 404/410 count as dead. A 403/429/timeout means the publisher blocked us, NOT that the article is gone."
resource: "https://www.copecheck.com"
tags: ["copecheck", "main", "source-file", "py"]
timestamp: "2026-09-25T16:54:44Z"
site: "main"
source_path: "copecheck/deadlink_crawl.py"
---

# !/usr/bin/env python3

Source path: `copecheck/deadlink_crawl.py`  
Index snapshot generated: `2026-11-24T13:42:54Z`  
File modified: `2026-09-25T16:54:44Z`

```py
#!/usr/bin/env python3
"""READ-ONLY: check every indexable article's source URL for a dead link.

Conservative: only 404/410 count as dead. A 403/429/timeout means the publisher
blocked us, NOT that the article is gone, so those are never treated as dead.
Confirms each candidate with a second GET before recording it.
"""
import concurrent.futures as cf
import json
import os
import sqlite3
import urllib.error
import urllib.request

HERE = os.path.dirname(os.path.abspath(__file__))
DB = os.path.join(HERE, "data", "copecheck.db")
OUT = os.path.join(HERE, "data", "deadlink_audit.json")
UA = ("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) "
      "Chrome/124.0 Safari/537.36 CopeCheckLinkAudit")

c = sqlite3.connect(f"file:{DB}?mode=ro", uri=True)
c.row_factory = sqlite3.Row
rows = c.execute(
    "SELECT slug, url FROM articles "
    "WHERE index_status IN ('index','trial_index')").fetchall()
c.close()
print(f"checking {len(rows)} indexable source URLs")

DEAD = {404, 410}


def probe(url, method):
    req = urllib.request.Request(url, method=method,
                                 headers={"User-Agent": UA})
    try:
        return urllib.request.urlopen(req, timeout=20).status
    except urllib.error.HTTPError as e:
        return e.code
    except Exception as e:
        return f"ERR:{type(e).__name__}"


def check(r):
    st = probe(r["url"], "HEAD")
    # Confirm any apparent death with a real GET before believing it.
    if st in DEAD:
        st2 = probe(r["url"], "GET")
        return (r["slug"], r["url"], st, st2, st2 in DEAD)
    return (r["slug"], r["url"], st, None, False)


res = []
with cf.ThreadPoolExecutor(12) as ex:
    for i, out in enumerate(ex.map(check, rows), 1):
        res.append(out)
        if i % 500 == 0:
            print(f"  ...{i}/{len(rows)}")

dead = [r for r in res if r[4]]
codes = {}
for r in res:
    codes[str(r[2])] = codes.get(str(r[2]), 0) + 1

print("\nstatus distribution:")
for k, v in sorted(codes.items(), key=lambda x: -x[1])[:12]:
    print(f"  {k:<12} {v}")
print(f"\nCONFIRMED DEAD (404/410 on both HEAD and GET): {len(dead)} "
      f"= {100*len(dead)/len(res):.1f}%")

json.dump({"checked": len(res),
           "dead": [{"slug": d[0], "url": d[1], "head": d[2], "get": d[3]}
                    for d in dead],
           "codes": codes}, open(OUT, "w"), indent=1)
print("written:", OUT)
```
