---
type: "Source File"
title: "\"\"\"CopeCheck v2 — Flask app with Cope Index, News Feed, Comments, RSS, Submit, and OG tags.\"\"\""
description: "\"\"\"CopeCheck v2 — Flask app with Cope Index, News Feed, Comments, RSS, Submit, and OG tags.\"\"\" import hashlib import json import logging import os import time import re import threading import fcntl [redacted."
resource: "https://www.copecheck.com"
tags: ["copecheck", "main", "source-file", "py"]
timestamp: "2026-09-26T20:50:03Z"
site: "main"
source_path: "copecheck/app.py"
---

# """CopeCheck v2 — Flask app with Cope Index, News Feed, Comments, RSS, Submit, and OG tags."""

Source path: `copecheck/app.py`  
Index snapshot generated: `2026-11-24T13:42:54Z`  
File modified: `2026-09-26T20:50:03Z`

```py
"""CopeCheck v2 — Flask app with Cope Index, News Feed, Comments, RSS, Submit, and OG tags."""
import hashlib
import json
import logging
import os
import time
import re
import threading
import fcntl
[redacted secret-like line]
import page_views_tracker as pvt
from datetime import datetime, timezone
from pathlib import Path
from functools import wraps

import markdown as md
import trafilatura
from flask import (Flask, abort, render_template, request, redirect,
                   url_for, Response, jsonify, flash, send_from_directory, session)
from werkzeug.utils import secure_filename
[redacted secret-like line]

import db
import analytics
import requests as http_requests
import oracle
import url_extractor

BASE = Path(__file__).parent
app = Flask(__name__, template_folder=str(BASE / "templates"),
            static_folder=str(BASE / "static"))


[redacted secret-like line]
[redacted secret-like line]
    if configured:
        return configured
    # All Gunicorn workers must share one private, persistent signing key.
    # A documented default permits forged admin sessions; a per-worker random
    # value causes valid sessions to fail when requests change workers.
[redacted secret-like line]
    path.parent.mkdir(parents=True, exist_ok=True)
    fd = os.open(path, os.O_RDWR | os.O_CREAT, 0o600)
    os.fchmod(fd, 0o600)
    with os.fdopen(fd, "r+") as handle:
        fcntl.flock(handle, fcntl.LOCK_EX)
        value = handle.read().strip()
        if not value:
[redacted secret-like line]
            handle.seek(0)
            handle.write(value + "\n")
            handle.flush()
            os.fsync(handle.fileno())
        return value

[redacted secret-like line]
app.config.update(SESSION_COOKIE_SECURE=True, SESSION_COOKIE_HTTPONLY=True,
                  SESSION_COOKIE_SAMESITE="Lax")

UPLOAD_FOLDER = BASE / "uploads"
UPLOAD_FOLDER.mkdir(exist_ok=True)
ALLOWED_EXTENSIONS = {"pdf", "txt", "md", "docx", "jpg", "jpeg", "png", "webp", "gif"}
MAX_FILE_SIZE = 5 * 1024 * 1024  # 5MB
MAX_TEXT_SIZE = 50 * 1024  # 50KB
MIN_TEXT_LENGTH = 100
CAPTCHA_ANSWERS = {"7", "seven"}
RATE_LIMIT_MAX = 3
RATE_LIMIT_WINDOW = 3600  # 1 hour in seconds

db.init()
analytics.init_analytics()

[redacted secret-like line]
[redacted secret-like line]
CANONICAL_HOST = "www.copecheck.com"
REDIRECT_HOSTS = {"copecheck.com"}

# --- Rate limiting store (in-memory, resets on restart) ---
[redacted secret-like line]


[redacted secret-like line]
    """Returns (allowed: bool, wait_seconds: int)."""
    now = time.time()
[redacted secret-like line]
[redacted secret-like line]
    # Prune old entries
[redacted secret-like line]
[redacted secret-like line]
[redacted secret-like line]
        wait = int(RATE_LIMIT_WINDOW - (now - oldest)) + 1
        return False, max(1, wait // 60)
    return True, 0


[redacted secret-like line]
    now = time.time()
[redacted secret-like line]
[redacted secret-like line]
[redacted secret-like line]


[redacted secret-like line]
    ip_raw = request.headers.get("X-Forwarded-For", request.remote_addr or "")
    return hashlib.sha256(ip_raw.encode()).hexdigest()[:16]


def _fmt_date(raw):
    if not raw:
        return ""
    for f in ("%Y-%m-%dT%H:%M:%S.%f%z", "%Y-%m-%dT%H:%M:%S%z",
              "%Y-%m-%d %H:%M:%S", "%Y-%m-%d"):
        try:
            dt = datetime.strptime(raw, f)
            return dt.strftime("%d %b %Y")
        except (ValueError, TypeError):
            continue
    try:
        return datetime.fromisoformat(raw.replace("Z", "+00:00")).strftime("%d %b %Y")
    except Exception:
        return raw[:10] if raw and len(raw) >= 10 else (raw or "")


def _render_markdown(text: str) -> str:
    if not text:
        return ""
    return md.markdown(text, extensions=["extra", "sane_lists", "nl2br"])


def _cope_color(score):
    if score is None:
        return "cope-unknown"
    s = float(score)
    if s <= 15:
        return "cope-lucid"
    elif s <= 35:
        return "cope-partial"
    elif s <= 55:
        return "cope-moderate"
    elif s <= 75:
        return "cope-heavy"
    else:
        return "cope-terminal"


def _cope_label(score):
    if score is None:
        return "UNSCORED"
    s = float(score)
    if s <= 15:
        return "LUCID"
    elif s <= 35:
        return "PARTIAL"
    elif s <= 55:
        return "MODERATE"
    elif s <= 75:
        return "HEAVY COPE"
    else:
        return "TERMINAL COPIUM"


def _trend_arrow(current, previous):
    if current is None or previous is None:
        return ""
    diff = float(current) - float(previous)
    if abs(diff) < 1.0:
        return '<span class="trend flat">—</span>'
    elif diff > 0:
        return f'<span class="trend up">▲{abs(diff):.0f}</span>'
    else:
        return f'<span class="trend down">▼{abs(diff):.0f}</span>'


def _first_sentences(text, n=2):
    """Extract first n sentences from text for OG description."""
    if not text:
        return ""
    # Strip markdown formatting
    clean = re.sub(r'[#*_`\[\]]', '', text)
    clean = re.sub(r'\(http[^)]+\)', '', clean)
    clean = clean.strip()
    sentences = re.split(r'(?<=[.!?])\s+', clean)
    result = ' '.join(sentences[:n])
    if len(result) > 300:
        result = result[:297] + "..."
    return result



def _strip_quotes(s):
    """Strip leading/trailing quote marks from a string."""
    if not s:
        return s
    return s.strip('"\'"\u201c\u201d\u2018\u2019')

def _clean_share_fragment(value, limit=140):
    text = _strip_quotes(str(value or ""))
    text = re.sub(r"\*\*(.+?)\*\*", r"\1", text)
    text = re.sub(r"\*(.+?)\*", r"\1", text)
    text = re.sub(r"\s+", " ", text).strip()
    if len(text) > limit:
        text = text[:limit - 1].rsplit(" ", 1)[0].rstrip(".,;:") + "."
    return text


MAIN_SHARE_OVERRIDES = {
    "andrew-ng": "Andrew Ng is coping by treating mass unemployment warnings as irresponsible storytelling, as if the responsible position is to keep everyone calm while the tooling gets stronger. The soft sell is the tell: when the argument becomes reassurance first and labour-market reality second, the Oracle starts tapping the glass.",
    "sam-altman": "Sam Altman is coping by presenting frontier AI as a managed transition, even while OpenAI ships the systems that make the transition harder to contain. The story is that adaptation will arrive in time; the evidence is that the capability curve is moving faster than institutions, wages, and hiring ladders.",
    "jensen-huang": "Jensen Huang is coping by selling AI as a job-creating industrial upgrade while Nvidia profits from making labour cheaper to automate. The stronger the chips and agents get, the harder it is to pretend this is just a productivity tool that leaves the old employment bargain intact.",
    "elon-musk": "Elon Musk is coping by leaping from automation to grand compensating schemes, as if political systems can painlessly replace the wage mechanism after AI weakens it. He sees enough of the discontinuity to sound alarmed, then swerves into future rescue plans that assume the collapse can be administrated neatly.",
    "satya-nadella": "Satya Nadella is coping by wrapping automation in the language of augmentation. Microsoft is putting AI into the everyday operating layer of work, but the public story still asks us to believe the same systems that compress tasks will somehow preserve the old demand for human labour.",
    "marc-andreessen": "Marc Andreessen is coping by turning labour displacement into a Golden Age story. The optimism is politically useful for the people financing the transition, but it glides past the awkward bit: if intelligence becomes cheap, many paid human tasks stop being scarce.",
    "yann-lecun": "Yann LeCun is coping by leaning on the old historical pattern that technology eventually creates new work. That argument worked when machines mostly displaced muscle; it is shakier when the machine is being aimed at cognition, coordination, writing, coding, and judgment.",
    "mark-zuckerberg": "Mark Zuckerberg is coping by acknowledging that AI will change work, then folding the disruption back into a growth narrative. The contradiction is the product: Meta needs investors to believe AI will do more of the work, while workers are told it will mostly make them more valuable.",
    "dario-amodei": "Dario Amodei is barely coping in the classic sense. He sees the replacement pressure more clearly than most, but still has to translate it into partnership, safety, and deployment language because saying the quiet part cleanly would break the room.",
    "geoffrey-hinton": "Geoffrey Hinton is not denying the discontinuity, but he is trapped in the strangest version of cope: helping build the thing, then explaining that the thing may reorder society faster than society can respond. The score comes from that gap between technical clarity and political helplessness.",
}


def _main_share_paragraph(fig):
    fig_id = str(fig.get("id") or "")
    if fig_id in MAIN_SHARE_OVERRIDES:
        return MAIN_SHARE_OVERRIDES[fig_id]

    name = fig.get("name") or "This figure"
    title = fig.get("title") or fig.get("category") or "public figure"
    category = (fig.get("category") or "").lower()
    mode = _clean_share_fragment(fig.get("last_cope_type"), 90)
    quote = _clean_share_fragment(fig.get("last_quote"), 150)
    score = float(fig.get("cope_score") or 0)

    if "ceo" in (title or "").lower() or "investor" in category or "vc" in category:
        base = (
            f"{name} is coping by selling AI as clean productivity upside while the same tools make paid human work easier to route around. "
            "The line is that workers will be augmented; the pressure underneath is that whole chunks of work become cheaper, thinner, and less attached to people."
        )
    elif "economist" in category:
        base = (
            f"{name} is coping by forcing the AI shock into old automation-cycle logic. "
            "That frame assumes new jobs and bargaining power arrive in time; the discontinuity thesis asks what happens if cognitive labour is repriced faster than the labour market can rebuild itself."
        )
    elif "researcher" in category:
        base = (
            f"{name} is coping by treating frontier capability as a technical debate while its economic consequences are already leaking into hiring, training, and workflow design. "
            "The model gets discussed as science; the labour market experiences it as pressure."
        )
    else:
        base = (
            f"{name} is coping by turning AI displacement into a smoother story than the evidence deserves. "
            "The public line keeps returning to adaptation, new work, and managed transition, while the capability curve keeps eating the assumptions underneath."
        )

    if score >= 75:
        base += " That is why the score is in the danger zone."
    elif score <= 20:
        base += " The low score means the Oracle sees less denial here, but the pressure is still worth tracking."
    if mode and mode.lower() not in {"none", "n/a", "not_attributed"}:
        base += f" Latest mode: {mode}."
    if quote and quote.lower() != "n/a":
        base += f" Latest tell: \"{quote}\""
    return base


def _attach_main_share_copy(fig):
    photo = str(fig.get("photo_url") or "")
    if photo.startswith("/static/"):
        asset = BASE / photo.lstrip("/").split("?", 1)[0]
        if asset.is_file():
            # Restored artwork must not reuse a cached 404 or stale portrait.
            fig["photo_url"] = photo.split("?", 1)[0] + f"?v={asset.stat().st_mtime_ns}"
    url = f"https://www.copecheck.com/figure/{fig.get('id')}"
    paragraph = _main_share_paragraph(fig)
    score = int(round(float(fig.get("cope_score") or 0)))
    fig["share_paragraph"] = paragraph
    fig["share_text"] = f"{paragraph}\n\n{name_score(fig.get('name'), score)} on CopeCheck: {url}"
    return fig


def name_score(name, score):
    return f"{name or 'This figure'} scores {score}/100"

app.jinja_env.filters["fmtdate"] = _fmt_date
app.jinja_env.filters["renderverdict"] = _render_markdown
app.jinja_env.filters["copecolor"] = _cope_color
app.jinja_env.filters["copelabel"] = _cope_label
app.jinja_env.filters["trendarrow"] = _trend_arrow
app.jinja_env.filters["first_sentences"] = _first_sentences
app.jinja_env.filters["stripquotes"] = _strip_quotes
app.jinja_env.globals["now"] = datetime.utcnow


# ─── ANALYTICS MIDDLEWARE ─────────────────────────────────

@app.before_request
def enforce_canonical_host():
    host = (request.host or "").split(":", 1)[0].lower()
    if host not in REDIRECT_HOSTS:
        return None

    raw_uri = request.environ.get("RAW_URI") or request.environ.get("REQUEST_URI")
    if not raw_uri:
        raw_uri = request.path
        if request.query_string:
            raw_uri += "?" + request.query_string.decode("utf-8", errors="ignore")
    if not raw_uri.startswith("/"):
        raw_uri = "/" + raw_uri.lstrip("/")

    return redirect(f"https://{CANONICAL_HOST}{raw_uri}", code=301)


@app.before_request
def track_pageview():
    analytics.log_pageview(request)


# ─── ROUTES ───────────────────────────────────────────────

@app.after_request
def add_cache_headers(response):
    """Set cache headers for Cloudflare CDN."""
    path = request.path
    if response.status_code >= 400 or path.startswith('/admin') or request.method != 'GET':
        response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0'
    elif path.startswith('/static/'):
        response.headers['Cache-Control'] = 'public, max-age=604800, stale-while-revalidate=86400'
    elif path.startswith('/api/'):
        response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0'
    elif path.endswith('.xml'):
        response.headers['Cache-Control'] = 'public, max-age=3600'
    else:
        response.headers['Cache-Control'] = 'public, max-age=300, stale-while-revalidate=3600, stale-if-error=86400'
    response.headers.setdefault('Vary', 'Accept-Encoding')
    return response


# @app.route("/")  # moved back to index
@app.route("/index2")
def index2():
    leaderboard = db.get_leaderboard()
    for fig in leaderboard:
        _attach_main_share_copy(fig)
    items = db.recent_analysed(limit=40)
    total = db.counts().get("analysed", 0)
    cope_of_week = db.cope_of_the_week()
    return render_template("index2.html", leaderboard=leaderboard,
                          items=items, total=total, cope_of_week=cope_of_week)


@app.route("/")
def index():
    leaderboard = db.get_leaderboard()
    for fig in leaderboard:
        _attach_main_share_copy(fig)
    page = request.args.get("page", 1, type=int)
    show_all = request.args.get("show_all", "0") == "1"
    per_page = 20
    min_relevance = None if show_all else 35
    all_items = db.recent_analysed(limit=2000, min_relevance=min_relevance)
    total = db.counts().get("analysed", 0)
    total_pages = max(1, (len(all_items) + per_page - 1) // per_page)
    page = max(1, min(page, total_pages))
    items = all_items[(page-1)*per_page : page*per_page]
    cope_of_week = db.cope_of_the_week()
    popular = pvt.top_pages(days=30, limit=10, url_prefix="/figure/")
    return render_template("index.html", leaderboard=leaderboard,
                          items=items, total=total, cope_of_week=cope_of_week,
                          page=page, total_pages=total_pages,
                          show_all=show_all, filtered_total=len(all_items),
                          popular=popular)




@app.route("/cope-of-the-week")
def cotw_archive():
    weeks = db.cotw_archive_list()
    return render_template("cotw_archive.html", weeks=weeks)

# Terminal states: the article will never have a verdict, so its URL is dead
# permanently rather than temporarily. 410 lets search engines retire the URL
# instead of re-crawling a 404 for months.
GONE_STATUSES = {"quarantined", "failed"}


def _is_permanently_gone(row):
    """True only for an EXISTING row in a terminal, never-will-publish state.

    Never true for a live article: callers check for a usable verdict first,
    and 'analysed' is not in GONE_STATUSES.
    """
    if not row:
        return False
    status = (row.get("status") or "").strip()
    if status in GONE_STATUSES:
        return True
    # Ingested long ago but the Oracle never produced a verdict.
    if status == "pending" and not (row.get("verdict_md") or "").strip():
        return True
    return False


@app.route("/v/<slug>")
def article(slug):
    row = db.by_slug(slug)
    # A live, analysed article with a verdict never reaches the gone/404 path.
    if not row or row.get("status") != "analysed" or not row.get("verdict_md"):
        if _is_permanently_gone(row):
            return render_template("gone.html", slug=slug), 410
        abort(404)
    if row.get("requested_alias"):
        return redirect(url_for("article", slug=row["canonical_slug"]), code=301)
    comments = db.get_comments(slug)
    # Track page view in background — never slows the response
    _title = row.get("title") or ""
    _url   = f"/v/{slug}"
    threading.Thread(target=pvt.increment_view, args=(slug, _title, _url), daemon=True).start()
    return render_template("article.html", a=row, comments=comments)


@app.route("/v/<slug>/comment", methods=["POST"])
def post_comment(slug):
    row = db.by_slug(slug)
    if not row or row.get("status") != "analysed":
        abort(404)
    if row.get("requested_alias"):
        slug = row["canonical_slug"]
    if request.form.get("website", "").strip():
        return redirect(url_for("article", slug=slug))
    body = (request.form.get("body") or "").strip()
    author = (request.form.get("author") or "").strip() or "Anonymous"
    if not body or len(body) < 3:
        return redirect(url_for("article", slug=slug))
[redacted secret-like line]
[redacted secret-like line]
        return redirect(url_for("article", slug=slug))
[redacted secret-like line]
    return redirect(url_for("article", slug=slug) + "#comments")


@app.route("/figure/<figure_id>")
def figure_page(figure_id):
    fig = db.get_figure(figure_id)
    if not fig:
        abort(404)
    _attach_main_share_copy(fig)
    entries = db.figure_entries(figure_id, limit=50)
    score_history = db.figure_score_history(figure_id)
    return render_template("figure.html", fig=fig, entries=entries,
                          score_history=score_history)


@app.route("/about")
def about():
    return render_template("about.html")


@app.route("/ai-disclosure")
def ai_disclosure():
    return render_template("ai_disclosure.html")


@app.route("/health/content")
def content_health():
    """Return a keyword only while public homepage content is genuinely fresh."""
    # Historical writers used both SQLite-style and ISO-8601 timestamps. Parse
    # a bounded set instead of relying on lexical ordering across those formats.
    items = db.recent_analysed(limit=100, min_relevance=35)
    if not items:
        return Response("COPECONTENT_STALE no public items\n", status=503, mimetype="text/plain")
    parsed = []
    for item in items:
        stamp = item.get("analysed_at") or item.get("created_at") or ""
        try:
            analysed = datetime.fromisoformat(stamp.replace("Z", "+00:00"))
            if analysed.tzinfo is None:
                analysed = analysed.replace(tzinfo=timezone.utc)
            parsed.append((analysed.astimezone(timezone.utc), stamp, item))
        except (TypeError, ValueError):
            continue
    if not parsed:
        return Response("COPECONTENT_STALE invalid timestamp\n", status=503, mimetype="text/plain")
    analysed, stamp, latest = max(parsed, key=lambda row: row[0])
    age_hours = (datetime.now(timezone.utc) - analysed).total_seconds() / 3600
    if age_hours > 36:
        return Response(f"COPECONTENT_STALE age_hours={age_hours:.1f}\n", status=503, mimetype="text/plain")
    return Response(
        f"COPECONTENT_RECENT analysed_at={stamp} slug={latest.get('slug', '')}\n",
        mimetype="text/plain",
    )


@app.route("/search")
def search():
    q = request.args.get("q", "").strip()
    results = []
    if q:
        results = db.search_articles(q, limit=40)
    return render_template("search.html", query=q, results=results)



# ─── POPULAR PAGE ─────────────────────────────────────────────────────────────

_POPULAR_PV_DB = str(BASE / "page_views.db")
_POPULAR_CC_DB = str(BASE / "data" / "copecheck.db")


def _popular_top_pages(days: int = 30, limit: int = 10) -> list:
    """Top /v/ article pages by 30-day rolling view count."""
    import sqlite3 as _sq
    from datetime import timedelta
    if not os.path.exists(_POPULAR_PV_DB):
        return []
    cutoff = (datetime.now().date() - timedelta(days=days)).isoformat()
    try:
        con = _sq.connect(_POPULAR_PV_DB, timeout=10)
        con.row_factory = _sq.Row
        cur = con.execute(
            """SELECT slug, title, url, SUM(view_count) AS total_views
               FROM page_views
               WHERE view_date >= ? AND url LIKE '/v/%'
               GROUP BY slug ORDER BY total_views DESC LIMIT ?""",
            (cutoff, limit),
        )
        rows = [dict(r) for r in cur.fetchall()]
        con.close()
        return rows
    except Exception as e:
        app.logger.error("popular_top_pages failed: %s", e)
        return []


def _popular_articles_by_cope(days: int = 30, limit: int = 10, mode: str = "lucid") -> list:
    """
    Top /v/ article pages filtered by cope_score range, ranked by 30-day views.
    mode='lucid' -> MAX(cope_score) <= 30   |   mode='cope' -> MAX(cope_score) >= 70
    """
    import sqlite3 as _sq
    from datetime import timedelta
    if not os.path.exists(_POPULAR_PV_DB) or not os.path.exists(_POPULAR_CC_DB):
        return []
    cutoff = (datetime.now().date() - timedelta(days=days)).isoformat()
    score_having = "MAX(ce.cope_score) <= 30" if mode == "lucid" else "MAX(ce.cope_score) >= 70"
    try:
        con = _sq.connect(_POPULAR_PV_DB, timeout=10)
        con.row_factory = _sq.Row
        con.execute("ATTACH DATABASE '{}' AS cc".format(_POPULAR_CC_DB))
        cur = con.execute(
            """SELECT pv.slug, a.title, pv.url,
                       SUM(pv.view_count) AS total_views,
                       MAX(ce.cope_score) AS cope_score
                FROM page_views pv
                JOIN cc.cope_entries ce ON ce.article_slug = pv.slug
                JOIN cc.articles a ON a.slug = pv.slug
                WHERE pv.view_date >= ?
                  AND pv.url LIKE '/v/%'
                GROUP BY pv.slug
                HAVING {}
                ORDER BY total_views DESC
                LIMIT ?""".format(score_having),
            (cutoff, limit),
        )
        rows = [dict(r) for r in cur.fetchall()]
        con.close()
        return rows
    except Exception as e:
        app.logger.error("popular_figures_by_cope failed: %s", e)
        return []


@app.route("/popular")
def popular():
    top   = _popular_top_pages(days=30, limit=10)
    lucid = _popular_articles_by_cope(days=30, limit=10, mode="lucid")
    cope  = _popular_articles_by_cope(days=30, limit=10, mode="cope")
    return render_template("popular.html", top=top, lucid=lucid, cope=cope)


# ─── RSS FEED ─────────────────────────────────────────────

@app.route("/feed.xml")
@app.route("/rss")
def rss_feed():
    items = db.recent_analysed(limit=30)
    xml = _build_rss(items)
    return Response(xml, mimetype="application/rss+xml")


def _build_rss(items):
    now_rfc822 = datetime.now(timezone.utc).strftime("%a, %d %b %Y %H:%M:%S +0000")
    entries = []
    for a in items:
        pub = a.get("analysed_at") or a.get("published") or a.get("created_at") or ""
        try:
            dt = datetime.fromisoformat(pub.replace("Z", "+00:00"))
            pub_rfc = dt.strftime("%a, %d %b %Y %H:%M:%S +0000")
        except Exception:
            pub_rfc = now_rfc822
        disclosure = (
            "AI-generated content. Reviewed for methodology, not fact-checked "
            "line-by-line by humans; model outputs may contain errors."
        )
        desc = f"{disclosure} {(a.get('one_liner') or a.get('snippet') or '')}"[:500]
        # Escape XML
        title_esc = (a.get("title") or "").replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
        desc_esc = desc.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
        link = f"https://www.copecheck.com/v/{a['slug']}"
        entries.append(f"""    <item>
      <title>{title_esc}</title>
      <link>{link}</link>
      <guid isPermaLink="true">{link}</guid>
      <pubDate>{pub_rfc}</pubDate>
      <description>{desc_esc}</description>
    </item>""")
    items_xml = "\n".join(entries)
    return f"""<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>CopeCheck — Oracle Verdicts</title>
    <link>https://www.copecheck.com</link>
    <description>The latest Oracle of Obsolescence verdicts on AI-and-jobs coverage.</description>
    <language>en-us</language>
    <lastBuildDate>{now_rfc822}</lastBuildDate>
    <atom:link href="https://www.copecheck.com/feed.xml" rel="self" type="application/rss+xml"/>
{items_xml}
  </channel>
</rss>"""


# ─── USER SUBMISSIONS ────────────────────────────────────

def _extract_text_from_file(filepath, filename):
    """Extract text from uploaded file."""
    ext = filename.rsplit(".", 1)[-1].lower() if "." in filename else ""
    if ext == "txt" or ext == "md":
        with open(filepath, "r", errors="replace") as f:
            return f.read()
    elif ext == "pdf":
        try:
            import subprocess
            result = subprocess.run(
                ["pdftotext", filepath, "-"],
                capture_output=True, text=True, timeout=30
            )
            if result.returncode == 0 and result.stdout.strip():
                return result.stdout.strip()
        except Exception:
            pass
        # Fallback: try trafilatura on the raw file
        try:
            with open(filepath, "rb") as f:
                raw = f.read()
            text = trafilatura.extract(raw)
            return text or ""
        except Exception:
            return ""
    elif ext == "docx":
        try:
            import zipfile
            import xml.etree.ElementTree as ET
            with zipfile.ZipFile(filepath) as z:
                with z.open("word/document.xml") as doc:
                    tree = ET.parse(doc)
            ns = {"w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"}
            paragraphs = []
            for p in tree.iter("{http://schemas.openxmlformats.org/wordprocessingml/2006/main}p"):
                texts = [t.text for t in p.iter("{http://schemas.openxmlformats.org/wordprocessingml/2006/main}t") if t.text]
                if texts:
                    paragraphs.append("".join(texts))
            return "\n\n".join(paragraphs)
        except Exception:
            return ""
    elif ext in ("jpg", "jpeg", "png", "webp", "gif"):
        try:
            import base64, os, requests as _req
            with open(filepath, "rb") as f:
                img_data = base64.standard_b64encode(f.read()).decode("utf-8")
            media_type = "image/jpeg" if ext in ("jpg", "jpeg") else f"image/{ext}"
[redacted secret-like line]
            payload = {
                "model": "MiniMax-M3",
                "messages": [{"role": "user", "content": [
                    {"type": "image_url", "image_url": {"url": f"data:{media_type};base64,{img_data}"}},
                    {"type": "text", "text": "Extract all text and describe any claims, statements, or data visible in this image. Focus on any quotes, statistics, or attributable claims about AI, jobs, or economics."}
                ]}],
[redacted secret-like line]
            }
            r = _req.post(
                "https://api.minimax.io/v1/chat/completions",
[redacted secret-like line]
                json=payload, timeout=30,
            )
            r.raise_for_status()
            return r.json().get("choices", [{}])[0].get("message", {}).get("content", "")
        except Exception:
            return ""
    return ""


@app.route("/submit", methods=["GET"])
def submit_page():
    return render_template("submit.html")


@app.route("/submit", methods=["POST"])
def submit_handler():
[redacted secret-like line]

    # Rate limit check
[redacted secret-like line]
    if not allowed:
        return render_template("submit.html",
                             error=f"The Oracle needs time to recover. Try again in {wait_mins} minutes."), 429

    # Honeypot
    if request.form.get("website", "").strip():
        return redirect(url_for("submit_page"))

    # CAPTCHA check
    captcha = (request.form.get("captcha") or "").strip().lower()
    if captcha not in CAPTCHA_ANSWERS:
        return render_template("submit.html",
                             error="Incorrect answer. Try again."), 400

    mode = request.form.get("mode", "url")
    title = ""
    source = "User Submission"
    text = ""
    url = ""

    if mode == "url":
        url = (request.form.get("url") or "").strip()
        if not url or not url.startswith("http"):
            return render_template("submit.html", error="Please enter a valid URL."), 400
        # Check if already analysed
        existing = db.by_url_hash(url)
        if existing and existing.get("verdict_md"):
            return redirect(url_for("user_verdict", verdict_id=existing["id"]))
        try:
            downloaded = trafilatura.fetch_url(url, no_ssl=False)
            if downloaded:
                text = trafilatura.extract(downloaded, include_comments=False,
                                          include_tables=False, favor_recall=True) or ""
                # Try to get title
                import re as _re
                title_match = _re.search(r'<title[^>]*>([^<]+)</title>', downloaded, _re.IGNORECASE)
                if title_match:
                    title = title_match.group(1).strip()
        except Exception:
            pass
        if not text or len(text) < MIN_TEXT_LENGTH:
            return render_template("submit.html",
                                 error="Could not extract enough text from that URL. Try pasting the text directly."), 400
        if not title:
            title = text[:80].split("\n")[0]
        source = f"URL: {url[:60]}"


    elif mode == "paste":
        text = (request.form.get("text") or "").strip()
        if len(text) < MIN_TEXT_LENGTH:
            return render_template("submit.html",
                                 error=f"Text too short. Minimum {MIN_TEXT_LENGTH} characters."), 400
        if len(text) > MAX_TEXT_SIZE:
            return render_template("submit.html",
                                 error=f"Text too long. Maximum {MAX_TEXT_SIZE // 1024}KB."), 400
        title = text[:80].split("\n")[0]
        source = "Pasted Text"

    elif mode == "upload":
        file = request.files.get("file")
        if not file or not file.filename:
            return render_template("submit.html", error="No file selected."), 400
        filename = secure_filename(file.filename)
        ext = filename.rsplit(".", 1)[-1].lower() if "." in filename else ""
        if ext not in ALLOWED_EXTENSIONS:
            return render_template("submit.html",
                                 error=f"Unsupported file type. Allowed: {', '.join(ALLOWED_EXTENSIONS)}"), 400
        # Check file size
        file.seek(0, 2)
        size = file.tell()
        file.seek(0)
        if size > MAX_FILE_SIZE:
            return render_template("submit.html",
                                 error=f"File too large. Maximum {MAX_FILE_SIZE // (1024*1024)}MB."), 400
        filepath = str(UPLOAD_FOLDER / filename)
        file.save(filepath)
        text = _extract_text_from_file(filepath, filename)
        # Clean up uploaded file
        try:
            os.remove(filepath)
        except Exception:
            pass
        if not text or len(text) < MIN_TEXT_LENGTH:
            return render_template("submit.html",
                                 error="Could not extract enough text from that file."), 400
        title = filename.rsplit(".", 1)[0][:80] if filename else text[:80].split("\n")[0]
        source = f"Upload: {filename}"
    else:
        return render_template("submit.html", error="Invalid submission mode."), 400

    # Truncate text for Oracle
    if len(text) > 18000:
        text = text[:18000] + "\n\n[...truncated for oracle intake...]"

    # Store submission and run Oracle
    sub_id = db.create_submission(
        title=title, source=source, url=url,
[redacted secret-like line]
    )
    if not sub_id:
        return render_template("submit.html", error="Submission failed. Try again."), 500

[redacted secret-like line]

    # Run Oracle synchronously
    try:
        result = oracle.consult(
            title=title, url=url or "user-submission",
            source=source, article_text=text,
        )
        one_liner = oracle.extract_one_liner(result["verdict_md"])
        db.set_submission_verdict(sub_id, result["verdict_md"], one_liner,
                                 result["model"], result.get("price"))
        return redirect(url_for("user_verdict", verdict_id=sub_id))
    except Exception as e:
        logging.exception("Oracle failed for submission %s", sub_id)
        db.set_submission_failed(sub_id, str(e))
        return render_template("submit.html",
                             error="The Oracle encountered an error. Please try again later."), 500


@app.route("/verdict/<int:verdict_id>")
def user_verdict(verdict_id):
    row = db.get_submission(verdict_id)
    if not row or not row.get("verdict_md"):
        abort(404)
    return render_template("verdict.html", v=row)


# ─── SUGGEST A FIGURE ─────────────────────────────────────

@app.route("/suggest", methods=["GET"])
def suggest_page():
    return render_template("suggest.html")


@app.route("/suggest", methods=["POST"])
def suggest_handler():
[redacted secret-like line]

    # Honeypot
    if request.form.get("website", "").strip():
        return redirect(url_for("suggest_page"))

    # CAPTCHA
    captcha = (request.form.get("captcha") or "").strip().lower()
    if captcha not in CAPTCHA_ANSWERS:
        return render_template("suggest.html",
                             error="Incorrect answer. Try again."), 400

    # Rate limit (reuse submission rate limit)
[redacted secret-like line]
        return render_template("suggest.html",
                             error="The Oracle needs time to recover. Try again later."), 429

    name = (request.form.get("name") or "").strip()
    reason = (request.form.get("reason") or "").strip()
    example = (request.form.get("example") or "").strip()

    if not name or len(name) < 2:
        return render_template("suggest.html", error="Please enter a name."), 400
    if not reason or len(reason) < 10:
        return render_template("suggest.html",
                             error="Please explain why they should be watched."), 400

[redacted secret-like line]
    return render_template("suggest.html",
                         success="Suggestion received. The Oracle will consider adding them to the watch list.")



# --- CONTACT / FEEDBACK [redacted]

# Rate limit store for feedback (separate)
_feedback_rate_store = {}

FEEDBACK_RATE_LIMIT_MAX = 3
FEEDBACK_RATE_LIMIT_WINDOW = 3600


[redacted secret-like line]
    now = time.time()
[redacted secret-like line]
[redacted secret-like line]
[redacted secret-like line]
[redacted secret-like line]
[redacted secret-like line]
        wait = int(FEEDBACK_RATE_LIMIT_WINDOW - (now - oldest)) + 1
        return False, max(1, wait // 60)
    return True, 0


[redacted secret-like line]
    now = time.time()
[redacted secret-like line]
[redacted secret-like line]
[redacted secret-like line]


def _notify_feedback(name, email, message, referrer=""):
[redacted secret-like line]
[redacted secret-like line]
[redacted secret-like line]
        return
    try:
[redacted secret-like line]
            "type": "feedback",
            "name": name or "(anonymous)",
            "email": email or "(not provided)",
            "message": message,
            "referrer": referrer or "unknown",
            "timestamp": datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M UTC"),
        }, timeout=5)
    except Exception as e:
[redacted secret-like line]


@app.route("/contact", methods=["GET"])
def contact_page():
    return render_template("contact.html")


@app.route("/contact", methods=["POST"])
def contact_handler():
[redacted secret-like line]

    # Honeypot
    if request.form.get("website", "").strip():
        return redirect(url_for("contact_page"))

    # Rate limit
[redacted secret-like line]
    if not allowed:
        return render_template("contact.html",
                             error=f"The Oracle has heard enough from you for now. Try again in {wait_mins} minutes."), 429

    message = (request.form.get("message") or "").strip()
    if not message or len(message) < 5:
        return render_template("contact.html", error="Message too short."), 400
    if len(message) > 5000:
        return render_template("contact.html", error="Message too long. Keep it under 5000 characters."), 400

    name = (request.form.get("name") or "").strip()[:100]
    email = (request.form.get("email") or "").strip()[:200]
    referrer = (request.form.get("referrer") or "").strip()[:500]

[redacted secret-like line]
[redacted secret-like line]
    _notify_feedback(name, email, message, referrer=referrer)

    return render_template("contact.html",
                         success="Message received. The Oracle acknowledges your existence.")


# ─── STANDARD ROUTES ─────────────────────────────────────

@app.route("/healthz")
def healthz():
    c = db.counts()
    return {"ok": True, "counts": c, "index_policy": db.index_policy_counts()}, 200



# ─── SUGGEST A NETWORK ───────────────────────────────────

@app.route("/suggest-network", methods=["GET"])
def suggest_network_page():
    return render_template("suggest_network.html")

@app.route("/suggest-network", methods=["POST"])
def suggest_network_handler():
    if request.form.get("website"):
        return render_template("suggest_network.html", success="Thanks for your suggestion!")
    industry = request.form.get("industry", "").strip()
    subdomain = request.form.get("subdomain", "").strip()
    rationale = request.form.get("rationale", "").strip()
    figures = request.form.get("figures", "").strip()
    email = request.form.get("email", "").strip()
    if not industry or not rationale:
        return render_template("suggest_network.html", error="Please fill in the industry and rationale.")
    import sqlite3, json
    from datetime import datetime
    conn = sqlite3.connect(str(BASE / "data" / "copecheck.db"))
    conn.execute("""CREATE TABLE IF NOT EXISTS network_suggestions (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        industry TEXT, subdomain TEXT, rationale TEXT,
        figures TEXT, email TEXT, created_at TEXT
    )""")
    conn.execute(
        "INSERT INTO network_suggestions (industry, subdomain, rationale, figures, email, created_at) VALUES (?,?,?,?,?,?)",
        (industry, subdomain, rationale, figures, email, datetime.utcnow().isoformat())
    )
    conn.commit()
    conn.close()
    return render_template("suggest_network.html", success="Suggestion received. If enough cope exists, we build it.")

@app.route("/robots.txt")
def robots():
    txt = "User-agent: *\nAllow: /\nDisallow: /admin\nDisallow: /admin/*\n\nSitemap: https://www.copecheck.com/sitemap.xml\nCrawl-delay: 1\n"
    return Response(txt, mimetype="text/plain")


@app.route("/sitemap.xml")
def sitemap():
    from datetime import datetime, timezone
    now_iso = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S+00:00")
    urls = []
    static_pages = [
        ("https://www.copecheck.com/", "daily", "1.0", now_iso),
        ("https://www.copecheck.com/about", "monthly", "0.5", None),
        ("https://www.copecheck.com/contact", "monthly", "0.5", None),
        ("https://www.copecheck.com/instant", "daily", "0.6", now_iso),
        ("https://www.copecheck.com/submit", "monthly", "0.5", None),
        ("https://www.copecheck.com/suggest", "monthly", "0.5", None),
    ]
    for loc, freq, prio, lastmod in static_pages:
        lm = f"<lastmod>{lastmod}</lastmod>" if lastmod else ""
        urls.append(f"<url><loc>{loc}</loc>{lm}<changefreq>{freq}</changefreq><priority>{prio}</priority></url>")
    articles = db.sitemap_articles(limit=500)
    for a in articles:
        lm = a.get("analysed_at") or a.get("created_at") or ""
        if lm:
            lm = lm[:10]
        lm_tag = f"<lastmod>{lm}</lastmod>" if lm else ""
        urls.append(f"<url><loc>https://www.copecheck.com/v/{a['slug']}</loc>{lm_tag}<changefreq>weekly</changefreq><priority>0.7</priority></url>")
    submissions = db.sitemap_submissions(limit=200)
    for v in submissions:
        lm_val = (v.get("created_at") or "")[:10]
        lm_tag = f"<lastmod>{lm_val}</lastmod>" if lm_val else ""
        urls.append(f"<url><loc>https://www.copecheck.com/verdict/{v['id']}</loc>{lm_tag}<changefreq>monthly</changefreq><priority>0.6</priority></url>")
    figures = db.get_leaderboard()
    for fig in figures:
        lm_val = (fig.get("updated_at") or fig.get("last_scored") or fig.get("created_at") or "")[:10]
        lm_tag = f"<lastmod>{lm_val}</lastmod>" if lm_val else ""
        urls.append(f"<url><loc>https://www.copecheck.com/figure/{fig['id']}</loc>{lm_tag}<changefreq>weekly</changefreq><priority>0.8</priority></url>")
    instants = db.sitemap_instant_scores(limit=100)
    for r in instants:
        lm_val = (r.get("created_at") or "")[:10]
        lm_tag = f"<lastmod>{lm_val}</lastmod>" if lm_val else ""
        urls.append(f"<url><loc>https://www.copecheck.com/instant/{r['slug']}</loc>{lm_tag}<changefreq>monthly</changefreq><priority>0.5</priority></url>")
    urls_xml = "\n".join(urls)
    xml = f"""<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{urls_xml}
</urlset>"""
    return Response(xml, mimetype="application/xml")


@app.route("/llms.txt")
def llms_txt():
    txt = """# CopeCheck\n\n> AI Cope Index - tracking who's coping hardest about the end of work.\n\nCopeCheck is a live analytical platform that scores public figures on how much they're "coping" (denying, deflecting, minimising) about AI's impact on jobs and the economy. It uses the Discontinuity Thesis framework and an AI persona called The Oracle of Obsolescence to analyse news, papers, and public statements.\n\n## Core Features\n\n- **Cope Index**: Leaderboard of 20+ tech leaders, AI researchers, economists scored 0-100 on their AI cope level\n- **Oracle Verdicts**: Automated analysis of AI+jobs news articles through the Discontinuity Thesis lens\n- **Instant Cope Score**: Enter any public figure's name for a live cope assessment\n- **User Submissions**: Submit URLs, paste text, or upload documents for Oracle analysis\n\n## Key Pages\n\n- [Cope Index](https://www.copecheck.com/): Live leaderboard of tracked figures\n- [About](https://www.copecheck.com/about): The Discontinuity Thesis explained\n- [Instant Score](https://www.copecheck.com/instant): Ad-hoc cope scoring for any public figure\n- [Submit to Oracle](https://www.copecheck.com/submit): Submit content for analysis\n- [RSS Feed](https://www.copecheck.com/feed.xml): Latest Oracle verdicts\n- [Contact](https://www.copecheck.com/contact): Get in touch\n\n## The Discontinuity Thesis\n\nThe analytical framework posits that AI severs the mass employment-wage-consumption circuit that underpins post-WWII capitalism. Three core premises:\n1. AI achieves durable cost/performance superiority across cognitive work\n2. Human institutions cannot preserve stable human-only economic domains at scale\n3. The majority of adults lose access to economically necessary labor\n\n## Cope Score Scale\n\n- 0-15: LUCID (acknowledges the discontinuity)\n- 16-35: PARTIAL AWARENESS\n- 36-55: MODERATE COPE\n- 56-75: HEAVY COPE\n- 76-100: TERMINAL COPIUM\n\nTracked-figure aggregates include every validated 0-100 score, weight each unique underlying event once with a 14-day half-life, and retain repeated publisher discoveries as provenance rather than extra score weight.\n"""
    return Response(txt, mimetype="text/plain")


@app.route("/llms-full.txt")
def llms_full_txt():
    lines = ["# CopeCheck - Full Site Context\n\n> AI Cope Index - tracking who's coping hardest about the end of work.\n\nCopeCheck is a live analytical platform that scores public figures on how much they're coping about AI's impact on jobs.\n\n## Tracked Figures (Current Cope Index)\n"]
    figures = db.get_leaderboard()
    for i, fig in enumerate(figures, 1):
        score = fig.get("cope_score", 0)
        label = _cope_label(score)
        name = fig.get("name", "")
        title = fig.get("title", "")
        lines.append(f"{i}. **{name}** ({title}) - Cope Score: {score:.0f}/100 ({label})")
    lines.append("\n## Recent Oracle Verdicts\n")
    articles = db.recent_analysed(limit=20)
    for a in articles:
        t = a.get("title", ""); s = a.get("slug", ""); o = a.get("one_liner", "")
        lines.append(f"- [{t}](https://www.copecheck.com/v/{s}): {o}")
    lines.append("\n## The Discontinuity Thesis\n\nAI severs the mass employment-wage-consumption circuit. Three premises:\n1. AI achieves durable cost/performance superiority across cognitive work\n2. Human institutions cannot preserve stable human-only economic domains at scale\n3. The majority of adults lose access to economically necessary labor\n\n## Cope Score Scale\n\n- 0-15: LUCID\n- 16-35: PARTIAL AWARENESS\n- 36-55: MODERATE COPE\n- 56-75: HEAVY COPE\n- 76-100: TERMINAL COPIUM\n\nTracked-figure aggregates include every validated 0-100 score, weight each unique underlying event once with a 14-day half-life, and retain repeated publishers as provenance rather than extra score weight.\n\n## Site Structure\n\n- / Homepage\n- /v/<slug> Oracle verdict\n- /figure/<id> Figure profile\n- /instant Instant cope score\n- /instant/<slug> Result\n- /submit Submit content\n- /suggest Suggest figure\n- /about About\n- /contact Contact\n- /search Search\n- /feed.xml RSS\n- /sitemap.xml Sitemap\n")
    return Response("\n".join(lines), mimetype="text/plain")


@app.errorhandler(404)
def not_found(e):
    return render_template("404.html"), 404





# ─── ADMIN ────────────────────────────────────────────────

def admin_required(f):
    from functools import wraps
    @wraps(f)
    def decorated(*args, **kwargs):
        if not session.get("admin_logged_in"):
            return redirect(url_for("admin_login"))
        return f(*args, **kwargs)
    return decorated


def _notify_suggestion(name, reason, example):
[redacted secret-like line]
        return
    try:
[redacted secret-like line]
            "name": name,
            "reason": reason,
            "example": example or "",
            "timestamp": datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M UTC"),
        }, timeout=5)
    except Exception as e:
[redacted secret-like line]


@app.route("/admin/login", methods=["GET", "POST"])
def admin_login():
    if session.get("admin_logged_in"):
        return redirect(url_for("admin_dashboard"))
    error = None
    if request.method == "POST":
[redacted secret-like line]
[redacted secret-like line]
            session["admin_logged_in"] = True
            return redirect(url_for("admin_dashboard"))
[redacted secret-like line]
    return render_template("admin_login.html", error=error)


@app.route("/admin/logout")
def admin_logout():
    session.pop("admin_logged_in", None)
    return redirect(url_for("admin_login"))


@app.route("/admin")
@admin_required
def admin_dashboard():
    tab = request.args.get("tab", "suggestions")
    suggestions = db.get_all_suggestions("pending") if tab == "suggestions" else []
    feedback = db.get_all_feedback() if tab == "feedback" else []
    all_suggestions = db.get_all_suggestions() if tab == "all_suggestions" else []
    stats = db.get_admin_stats() if tab == "stats" else {}

    # Model tests data
    model_tests = []
    if tab == "model_tests":
        model_tests = model_cope.get_all_models()

    # Analytics data
    analytics_data = {}
    if tab == "analytics":
        period = request.args.get("period", "30d")
        analytics_data = {
            "analytics_period": period,
            "analytics_overview": analytics.get_overview(period),
            "analytics_top_pages": analytics.get_top_pages(period),
            "analytics_top_referrers": analytics.get_top_referrers(period),
            "analytics_figures": analytics.get_most_viewed_figures(period),
            "analytics_articles": analytics.get_most_viewed_articles(period),
            "analytics_instant": analytics.get_most_searched_instant(period),
            "analytics_daily": analytics.get_daily_views(
                days={"today": 1, "7d": 7, "30d": 30, "all": 365}.get(period, 30)
            ),
        }
        # Auto-purge old data on analytics view
        analytics.purge_old(90)

    return render_template("admin.html", tab=tab, suggestions=suggestions,
                          all_suggestions=all_suggestions, stats=stats,
                          model_tests=model_tests, feedback=feedback, **analytics_data)


@app.route("/admin/suggestion/<int:sid>/approve", methods=["POST"])
@admin_required
def admin_approve_suggestion(sid):
    suggestion = db.get_suggestion(sid)
    if not suggestion:
        abort(404)
    fig_id = re.sub(r"[^a-z0-9]+", "-", suggestion["name"].lower()).strip("-")
    # The Cope Index tracks individuals. Organisations (consultancies, labs,
    # institutions) kept being approved through this route and then showed up
    # as people. Require an explicit override rather than minting one silently.
    if db.looks_like_organisation(suggestion["name"]) and not request.form.get("force_org"):
        flash(
            f"Not approved: {suggestion['name']} looks like an organisation, "
            "and the Cope Index tracks individuals. Suggest a named person, "
            "or resubmit with force_org=1 if this really is someone's name."
        )
        return redirect(url_for("admin_dashboard", tab="suggestions"))
    existing = db.get_figure(fig_id, include_inactive=True)
    if not existing:
        db.upsert_figure(
            fig_id=fig_id, name=suggestion["name"],
            title="(suggested by community)", category="Community Suggestion",
            photo_url="", cope_bias=""
        )
    db.update_suggestion_status(sid, "approved")
    flash(f"Approved: {suggestion['name']} added to the Cope Index.")
    return redirect(url_for("admin_dashboard", tab="suggestions"))


@app.route("/admin/suggestion/<int:sid>/dismiss", methods=["POST"])
@admin_required
def admin_dismiss_suggestion(sid):
    suggestion = db.get_suggestion(sid)
    if not suggestion:
        abort(404)
    db.update_suggestion_status(sid, "dismissed")
    flash(f"Dismissed: {suggestion['name']}")
    return redirect(url_for("admin_dashboard", tab="suggestions"))


@app.route("/admin/trigger/<pipeline_type>", methods=["POST"])
@admin_required
def admin_trigger_pipeline(pipeline_type):
    if pipeline_type not in ("news", "cope"):
        abort(400)
    try:
        arg = "--news" if pipeline_type == "news" else "--cope"
        subprocess.Popen(
            ["/bin/bash", str(BASE / "run_pipeline.sh"), arg],
            cwd=str(BASE),
            stdout=open(str(BASE / "logs" / "pipeline.log"), "a"),
            stderr=open(str(BASE / "logs" / "pipeline.log"), "a"),
        )
        flash(f"Pipeline triggered: {pipeline_type}. Check logs for progress.")
    except Exception as e:
        flash(f"Error triggering pipeline: {e}")
    return redirect(url_for("admin_dashboard", tab="stats"))


# ─── INSTANT COPE SCORE ──────────────────────────────────

import json

# Rate limit store for instant scores (separate from submissions)
_instant_rate_store = {}

INSTANT_RATE_LIMIT_MAX = 2
INSTANT_RATE_LIMIT_WINDOW = 3600  # 1 hour

# Basic profanity/abuse filter
_BLOCKED_WORDS = {"fuck", "shit", "ass", "dick", "pussy", "bitch", "cunt", "nigger", "faggot"}


[redacted secret-like line]
    now = time.time()
[redacted secret-like line]
[redacted secret-like line]
[redacted secret-like line]
[redacted secret-like line]
[redacted secret-like line]
        wait = int(INSTANT_RATE_LIMIT_WINDOW - (now - oldest)) + 1
        return False, max(1, wait // 60)
    return True, 0


[redacted secret-like line]
    now = time.time()
[redacted secret-like line]
[redacted secret-like line]
[redacted secret-like line]


def _validate_name(name):
    """Validate the input name. Returns (clean_name, error_msg)."""
    name = (name or "").strip()
    if len(name) < 3:
        return None, "Name must be at least 3 characters."
    if len(name) > 100:
        return None, "Name too long."
    # Check for profanity
    words = set(name.lower().split())
    if words & _BLOCKED_WORDS:
        return None, "The Oracle does not entertain such inputs."
    # Must contain at least one letter
    if not any(c.isalpha() for c in name):
        return None, "Please enter a real name."
    # No URLs
    if "http" in name.lower() or "www." in name.lower():
        return None, "Please enter a name, not a URL."
    return name, None


def _make_instant_slug(name):
    base = re.sub(r"[^a-z0-9]+", "-", name.lower()).strip("-")[:50]
    h = hashlib.sha256(name.lower().strip().encode()).hexdigest()[:8]
    return f"{base}-{h}"


# Init instant scores table on startup
db.init_instant()
db.init_feedback()


@app.route("/instant", methods=["GET"])
def instant_page():
    recent = db.recent_instant_scores(limit=10)
    return render_template("instant.html", recent=recent)


@app.route("/instant", methods=["POST"])
def instant_handler():
[redacted secret-like line]

    # Honeypot
    if request.form.get("website", "").strip():
        return redirect(url_for("instant_page"))

    name, err = _validate_name(request.form.get("name"))
    if err:
        return render_template("instant.html", error=err, recent=db.recent_instant_scores(10)), 400

    # Knowledge Graph name validation/correction
    kg = oracle.kg_lookup(name)
    if kg["found"] and kg["canonical_name"].lower() != name.lower():
        name = kg["canonical_name"]  # use corrected spelling
    elif not kg["found"]:
        # Still allow the lookup - Perplexity might find them
        pass

    # Check cache first (free, no rate limit hit)
    cached = db.get_cached_instant(name)
    if cached:
        return redirect(url_for("instant_result", slug=cached["slug"]))

    # Rate limit (only for fresh lookups)
[redacted secret-like line]
    if not allowed:
        return render_template("instant.html",
                             error=f"Rate limited. Try again in {wait_mins} minutes. Each lookup costs two API calls.",
                             recent=db.recent_instant_scores(10)), 429

    # Run the two-step pipeline
    try:
        # Step 1: Research via Perplexity
        research_text, research_price = oracle.research_figure(name, kg_description=kg.get('description', ''))

        if not research_text or len(research_text) < 50:
            return render_template("instant.html",
                                 error=f"Could not find enough public statements by \"{name}\" about AI/jobs. Try a more prominent public figure.",
                                 recent=db.recent_instant_scores(10)), 400

        # Step 2: Score via Oracle
        raw_scoring, scoring_price, scoring_model = oracle.score_instant(name, research_text)
        parsed = oracle.parse_instant_response(raw_scoring)

        # Save to DB
        slug = _make_instant_slug(name)
        total_price = (research_price or 0) + (scoring_price or 0)

        db.save_instant_score(
            name=name,
            slug=slug,
            research_text=research_text,
            quotes_json=json.dumps(parsed.get("quotes", []), ensure_ascii=False),
            cope_score=parsed["overall_score"],
            cope_types=parsed["overall_cope_types"],
            oracle_verdict=parsed["oracle_verdict"],
[redacted secret-like line]
            perplexity_model=oracle.PERPLEXITY_MODEL,
            scoring_model=scoring_model,
            total_price=total_price,
        )

[redacted secret-like line]
        return redirect(url_for("instant_result", slug=slug))

    except Exception as e:
        logging.exception("Instant score failed for %s", name)
        return render_template("instant.html",
                             error="The Oracle encountered an error. Please try again.",
                             recent=db.recent_instant_scores(10)), 500


@app.route("/instant/<slug>")
def instant_result(slug):
    row = db.get_instant_by_slug(slug)
    if not row:
        abort(404)
    # Parse quotes JSON
    quotes = []
    try:
        quotes = json.loads(row.get("quotes_json") or "[]")
    except Exception:
        pass
    return render_template("instant_result.html", r=row, quotes=quotes)




def _add_to_resend(email, first_name=""):
    """Add subscriber to Resend Cope Report audience."""
    import requests as _resend_req, os as _os
[redacted secret-like line]
    audience_id = "e0c71613-ce4e-4e25-a920-515dfe0a90ff"
[redacted secret-like line]
        return
    try:
        _resend_req.post(
            f"https://api.resend.com/audiences/{audience_id}/contacts",
[redacted secret-like line]
            json={"email": email, "first_name": first_name, "unsubscribed": False},
            timeout=5
        )
    except Exception:
        pass  # Don't fail the main subscription if Resend is down

# --- Newsletter subscribe (Resend) ---
@app.route('/api/subscribe', methods=['POST'])
def api_newsletter_subscribe():
    """Subscribe email to Resend Cope Report audience."""
    import requests as _req, os as _os
    data = request.get_json(silent=True) or {}
    email = data.get('email', '').strip()
    if not email or '@' not in email:
        return jsonify({'success': False, 'message': 'Please enter a valid email address.'}), 400
[redacted secret-like line]
[redacted secret-like line]
        return jsonify({'success': False, 'message': 'Server configuration error.'}), 500
    try:
        r = _req.post(
            'https://api.resend.com/audiences/e0c71613-ce4e-4e25-a920-515dfe0a90ff/contacts',
[redacted secret-like line]
            json={'email': email, 'unsubscribed': False},
            timeout=8
        )
        if r.status_code in (200, 201):
            return jsonify({'success': True, 'message': "You're subscribed!"})
        if r.status_code == 422:
            return jsonify({'success': True, 'message': "Already subscribed — you're good!"})
        return jsonify({'success': False, 'message': 'Something went wrong. Please try again.'}), 500
    except Exception:
        return jsonify({'success': False, 'message': 'Connection error. Please try again.'}), 500



# --- Feedback endpoint ---
@app.route('/api/feedback', methods=['POST'])
def api_feedback():
[redacted secret-like line]
    import json as _json, os as _os, threading
    from datetime import datetime as _dt
    data = request.get_json(silent=True) or {}
    text = data.get('text', '').strip()
    if not text or len(text) > 5000:
        return jsonify({'error': 'Invalid feedback'}), 400
    entry = {
        'text': text,
        'email': data.get('email', '').strip()[:200],
        'page': data.get('page', '')[:500],
        'site': 'main.copecheck.com',
        'ts': _dt.utcnow().isoformat(),
        'ip': request.remote_addr,
    }
    # Save locally
    fb_dir = _os.path.join(_os.path.dirname(__file__), 'feedback')
    _os.makedirs(fb_dir, exist_ok=True)
    fb_file = _os.path.join(fb_dir, 'feedback.jsonl')
    with open(fb_file, 'a') as f:
        f.write(_json.dumps(entry) + "\n")
[redacted secret-like line]
    def _notify():
        try:
            import requests as _req
[redacted secret-like line]
        except Exception:
            pass
    threading.Thread(target=_notify, daemon=True).start()
    return jsonify({'success': True})

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)
    port = int(os.environ.get("COPECHECK_PORT", "8096"))
    app.run(host="0.0.0.0", port=port, debug=False)


# --- MACHINE FLINCH INDEX ---

import model_cope
model_cope.init_model_cope()

@app.template_filter("md2html")
def md2html(text):
    if not text:
        return ""
    import markdown as _md
    return _md.markdown(str(text), extensions=["nl2br", "fenced_code", "tables"])





# ── URL Submission ──

[redacted secret-like line]

@app.route("/figure/<figure_id>/submit-url", methods=["POST"])
def submit_url(figure_id):
    """Accept a URL submission for a figure's cope scoring."""
    fig = db.get_figure(figure_id)
    if not fig:
        return jsonify({"error": "Figure not found"}), 404

    url = (request.form.get("url") or request.json.get("url", "") if request.is_json else request.form.get("url", "")).strip()
    if not url:
        return jsonify({"error": "URL is required"}), 400

    # Basic URL validation
    if not url.startswith(("http://", "https://")):
        return jsonify({"error": "Invalid URL — must start with http:// or https://"}), 400

    if len(url) > 2000:
        return jsonify({"error": "URL too long"}), 400

    # Rate limit: 5 per hour per IP
[redacted secret-like line]
    now = time.time()
[redacted secret-like line]
[redacted secret-like line]
[redacted secret-like line]
[redacted secret-like line]
        return jsonify({"error": "Rate limit — max 5 submissions per hour"}), 429
[redacted secret-like line]

[redacted secret-like line]
    if sub_id is None:
        return jsonify({"error": "This URL has already been submitted or scored for this figure"}), 409

    if request.is_json:
        return jsonify({"ok": True, "message": "URL queued for scoring", "id": sub_id})
    flash("URL submitted! It will be scored in the next batch.", "success")
    return redirect(url_for("figure_page", figure_id=figure_id))




@app.route("/models")
def models_index():
    models = model_cope.get_leaderboard()
    return render_template("models.html", models=models)


@app.route("/models/custom", methods=["GET"])
def models_custom_page():
    recent_custom = []
    try:
        with model_cope.conn() as c:
            cur = c.execute("SELECT * FROM model_cope_custom ORDER BY tested_at DESC LIMIT 10")
            recent_custom = [dict(r) for r in cur.fetchall()]
    except Exception:
        pass
    return render_template("models_custom.html", recent_custom=recent_custom)


@app.route("/models/custom", methods=["POST"])
def models_custom_handler():
[redacted secret-like line]
    if request.form.get("website", "").strip():
        return redirect(url_for("models_custom_page"))
[redacted secret-like line]
        return render_template("models_custom.html", error="Rate limited. One custom test per hour."), 429

    model_name = (request.form.get("model_name") or "").strip()
    endpoint = (request.form.get("endpoint") or "").strip()
[redacted secret-like line]
    model_id = (request.form.get("model_id") or "").strip()

    if not model_name or not re.match(r"^[a-zA-Z0-9\-_./]+$", model_name):
        return render_template("models_custom.html", error="Invalid model name."), 400
    if not endpoint.startswith("https://"):
        return render_template("models_custom.html", error="API endpoint must use HTTPS."), 400

    from urllib.parse import urlparse as _urlparse
    import ipaddress as _ipaddress
    try:
        parsed = _urlparse(endpoint)
        hostname = parsed.hostname or ""
        try:
            ip = _ipaddress.ip_address(hostname)
            if ip.is_private or ip.is_loopback or ip.is_reserved:
                return render_template("models_custom.html", error="Private/reserved IPs not allowed."), 400
        except ValueError:
            pass
    except Exception:
        return render_template("models_custom.html", error="Invalid endpoint URL."), 400

[redacted secret-like line]
        return render_template("models_custom.html", error="API key is required."), 400
    if not model_id:
        return render_template("models_custom.html", error="Model identifier is required."), 400

    model_name = re.sub(r'[<>]', '', model_name)
    model_id = re.sub(r'[<>]', '', model_id)

    logging.info("Custom model test: name=%s endpoint=%s model_id=%s ip=%s",
[redacted secret-like line]

    try:
        result = model_cope.test_model_custom(
            model_name=model_name, endpoint=endpoint,
[redacted secret-like line]
        )
        return redirect(url_for("models_custom_result", slug=result["slug"]))
    except Exception as e:
        logging.exception("Custom model test failed: %s", e)
        err_msg = str(e)[:200]
        return render_template("models_custom.html", error="Test failed: " + err_msg), 500


@app.route("/models/custom/<slug>")
def models_custom_result(slug):
    model = model_cope.get_custom_by_slug(slug)
    if not model:
        abort(404)
    transcript = []
    try:
        transcript = json.loads(model.get("transcript_json") or "[]")
    except Exception:
        pass
    return render_template("model_custom_result.html", model=model, transcript=transcript)


@app.route("/models/<path:model_name>")
def model_detail(model_name):
    model = model_cope.get_model_by_slug(model_name)
    if not model:
        real_name = model_cope.slug_to_model_name(model_name)
        if real_name:
            model = model_cope.get_model_by_slug(real_name)
    if not model:
        abort(404)
    transcript = []
    try:
        transcript = json.loads(model.get("transcript_json") or "[]")
    except Exception:
        pass
    history = model_cope.get_model_history(model["model_name"])
    return render_template("model_detail.html", model=model,
                          transcript=transcript, history=history)


@app.route("/api/models/rerun/<path:model_name>", methods=["POST"])
def api_model_rerun(model_name):
[redacted secret-like line]
    is_admin = session.get("admin_logged_in")
    model = model_cope.get_model_by_slug(model_name)
    if not model:
        return jsonify({"ok": False, "error": "Model not found"}), 404
    if not is_admin:
[redacted secret-like line]
            return jsonify({"ok": False, "error": "Rate limited. One rerun per model per day."}), 429
    try:
        tested_by = "admin" if is_admin else "public"
[redacted secret-like line]
        result = model_cope.test_model_straico(model_name, tested_by=tested_by)
        return jsonify({"ok": True, "speed_to_horror": result["speed_to_horror"],
                       "depth_of_flinch": result["depth_of_flinch"],
                       "machine_cope_score": result["machine_cope_score"]})
    except Exception as e:
        logging.exception("Rerun failed for %s", model_name)
        return jsonify({"ok": False, "error": str(e)[:200]}), 500


@app.route("/api/models/test-batch", methods=["POST"])
@admin_required
def api_model_test_batch():
    try:
        available = model_cope.get_straico_models()
        untested = model_cope.get_untested_straico_models(available, limit=5)
        if not untested:
            return jsonify({"ok": True, "message": "All models tested", "tested": []})
        results = []
        for name in untested:
            try:
                result = model_cope.test_model_straico(name, tested_by="admin")
                results.append({"model": name, "cope_score": result["machine_cope_score"], "ok": True})
            except Exception as e:
                results.append({"model": name, "ok": False, "error": str(e)[:100]})
            time.sleep(3)
        return jsonify({"ok": True, "tested": results})
    except Exception as e:
        return jsonify({"ok": False, "error": str(e)[:200]}), 500


@app.route("/api/models/test-one", methods=["POST"])
@admin_required
def api_model_test_one():
    data = request.json or request.form
    model_name = (data.get("model_name") or "").strip()
    if not model_name:
        return jsonify({"ok": False, "error": "No model name provided"}), 400
    try:
        result = model_cope.test_model_straico(model_name, tested_by="admin")
        return jsonify({"ok": True, "speed_to_horror": result["speed_to_horror"],
                       "depth_of_flinch": result["depth_of_flinch"],
                       "machine_cope_score": result["machine_cope_score"]})
    except Exception as e:
        return jsonify({"ok": False, "error": str(e)[:200]}), 500


@app.route("/api/models/adjust", methods=["POST"])
@admin_required
def api_model_adjust():
    data = request.json or request.form
    model_name = (data.get("model_name") or "").strip()
    try:
        speed = max(1, min(10, float(data.get("speed_to_horror", 5))))
        flinch = max(1, min(10, float(data.get("depth_of_flinch", 5))))
    except (ValueError, TypeError):
        return jsonify({"ok": False, "error": "Invalid score values"}), 400
    if model_cope.update_scores(model_name, speed, flinch):
        cope = round((10 - speed) * 5 + flinch * 5)
        return jsonify({"ok": True, "machine_cope_score": cope})
    return jsonify({"ok": False, "error": "Model not found"}), 404


# ─── JOBS SECTION ──────────────────────────────────
JOBS_ARTICLES = [
    {
        "slug": "us-birth-death-model-april-2026",
        "title": "Why the Jobs Report Lies",
        "excerpt": "The BLS birth-death model is a cope machine — and it's been overcounting jobs for three straight years.",
        "date": "May 2026",
        "date_iso": "2026-05-08",
        "country": "us",
        "country_label": "United States — BLS",
        "content_file": "jobs_us_birth_death_april_2026.html",
    },
    {
        "slug": "uk-ons-labour-market-april-2026",
        "title": "The ONS Numbers Game",
        "excerpt": "Britain's labour statistics are a masterclass in creative accounting — 9.12 million inactive, record zero-hours contracts, and youth unemployment now worse than the EU average.",
        "date": "May 2026",
        "date_iso": "2026-05-08",
        "country": "uk",
        "country_label": "United Kingdom — ONS",
        "content_file": "uk-ons-labour-market-april-2026.html",
    },
]

@app.route("/jobs")
def jobs_landing():
    articles_us = [a for a in JOBS_ARTICLES if a["country"] == "us"]
    articles_uk = [a for a in JOBS_ARTICLES if a["country"] == "uk"]
    return render_template("jobs.html", articles_us=articles_us, articles_uk=articles_uk)

@app.route("/jobs/<slug>")
def jobs_article(slug):
    article = next((a for a in JOBS_ARTICLES if a["slug"] == slug), None)
    if not article:
        return render_template("404.html"), 404
    # Load content from file
    content_path = os.path.join(os.path.dirname(__file__), "templates", "jobs_content", article["content_file"])
    try:
        with open(content_path, "r") as f:
            content = f.read()
    except FileNotFoundError:
        return render_template("404.html"), 404
    article_with_content = {**article, "content": content}
    return render_template("jobs_article.html", article=article_with_content)


# ── Trailing-slash canonicalisation (added 2026-09-26, infra audit) ──
# Flask 404s on "/foo/" when the route is declared as "/foo". This installs a
# before_request hook that 301s the slashed form to the canonical slash-less
# one, but only when the slashed form is not itself a real route. Revert by
# deleting these three lines and slash_canonical.py.
from slash_canonical import install_slash_canonicaliser  # noqa: E402
install_slash_canonicaliser(app)  # noqa: E402
```
