---
type: "Source File"
title: "!/usr/bin/env python3"
description: "#!/usr/bin/env python3 \"\"\" Process queued URL submissions: extract content, filter spam, score cope. Run daily or on-demand: python3 process_url_submissions.py \"\"\" import logging import os import sys import time from."
resource: "https://www.copecheck.com"
tags: ["copecheck", "main", "source-file", "py"]
timestamp: "2026-08-24T12:39:23Z"
site: "main"
source_path: "copecheck/process_url_submissions.py"
---

# !/usr/bin/env python3

Source path: `copecheck/process_url_submissions.py`  
Index snapshot generated: `2026-11-24T13:42:54Z`  
File modified: `2026-08-24T12:39:23Z`

```py
#!/usr/bin/env python3
"""
Process queued URL submissions: extract content, filter spam, score cope.
Run daily or on-demand: python3 process_url_submissions.py
"""
import logging
import os
import sys
import time
from pathlib import Path

# Load .env
env_path = Path(__file__).parent / '.env'
if env_path.exists():
    for line in open(env_path):
        line = line.strip()
        if line and not line.startswith('#') and '=' in line:
            k, v = line.split('=', 1)
            os.environ[k] = v

sys.path.insert(0, str(Path(__file__).parent))
import db
import oracle
import url_extractor
from content_quality import extraction_quality_issue
from pipeline import AUTO_PUBLISH_ATTRIBUTION, MIN_CONFIDENCE

log = logging.getLogger("url_submissions")
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s %(levelname)s %(name)s | %(message)s",
)

db.init()

MAX_PROCESS = int(os.environ.get("MAX_URL_PROCESS", "20"))


def process_pending():
    """Process all pending URL submissions."""
    pending = db.get_pending_submissions(limit=MAX_PROCESS)
    if not pending:
        log.info("No pending URL submissions")
        return 0

    log.info("Processing %d pending URL submissions", len(pending))
    scored = 0

    for sub in pending:
        sub_id = sub["id"]
        url = sub["url"]
        figure_id = sub["figure_id"]
        figure_name = sub.get("figure_name", "unknown")
        figure_title = sub.get("figure_title", "")

        log.info("[%d] Extracting: %s for %s", sub_id, url[:80], figure_name)

        # 1. Detect URL type and extract content
        url_type = url_extractor.detect_url_type(url)
        result = url_extractor.extract_content(url, url_type)

        if result.get("error"):
            log.warning("[%d] Extraction failed: %s", sub_id, result["error"])
            db.update_submission(sub_id, "failed", url_type=url_type,
                               error_msg=result["error"])
            continue

        text = result.get("text", "").strip()
        title = result.get("title", "")

        if not text or len(text) < 50:
            log.warning("[%d] Too little text extracted (%d chars)", sub_id, len(text))
            db.update_submission(sub_id, "failed", extracted_text=text[:500],
                               url_type=url_type, error_msg="Insufficient text extracted")
            continue

        issue = extraction_quality_issue(text)
        if issue and not (url_type == "tweet" and issue == "too_short"):
            log.info("[%d] Rejected non-article extraction: %s", sub_id, issue)
            db.update_submission(
                sub_id, "rejected", extracted_text=text[:500],
                url_type=url_type, error_msg=f"Extraction quality: {issue}",
            )
            continue

        # 2. Spam check
        if url_extractor.is_spam(text, url):
            log.info("[%d] Flagged as spam/irrelevant", sub_id)
            db.update_submission(sub_id, "rejected", extracted_text=text[:500],
                               url_type=url_type, error_msg="Not relevant to AI/jobs")
            continue

        # 3. Extract figure context (look for their name in the content)
        from pipeline import _extract_figure_context
        quote_context = _extract_figure_context(text, figure_name)
        if not quote_context or len(quote_context) < 80:
            # For tweets, use the whole text since it's short
            if url_type == "tweet" and len(text) < 2000:
                quote_context = text
            else:
                log.warning("[%d] Could not find %s context in extracted text", sub_id, figure_name)
                db.update_submission(sub_id, "failed", extracted_text=text[:500],
                                   url_type=url_type,
                                   error_msg=f"Could not find mentions of {figure_name}")
                continue

        if db.record_cope_observation_if_known(
            figure_id, quote_context, source_url=url,
            source_title=title or url, channel="user_submission",
        ):
            db.update_submission(
                sub_id, "duplicate", extracted_text=text[:2000],
                url_type=url_type, error_msg="Underlying event already scored",
            )
            continue

        # 4. Score cope
        try:
            log.info("[%d] Scoring cope for %s...", sub_id, figure_name)
            cope_result = oracle.score_cope(
                figure_name, figure_title,
                quote_context,
                source_context=f"From {url_type}: {title or url}",
            )

            # Fix 2: Skip irrelevant zero-score entries
            if cope_result["cope_score"] == 0 and cope_result.get("cope_type") == "N/A":
                log.info("[%d] SKIPPED: score=0 type=N/A (irrelevant)", sub_id)
                db.update_submission(sub_id, "rejected", extracted_text=text[:500],
                                   url_type=url_type, error_msg="Irrelevant (score 0)")
                continue
            ctype = cope_result.get("cope_type", "").lower().strip()
            attr = cope_result.get("attribution_strength", "mentioned_only")
            confidence = float(cope_result.get("confidence", 0.0) or 0.0)
            if (cope_result.get("cope_score", 0) < 0
                    or "not_attributed" in ctype or ctype == "n/a"
                    or attr not in AUTO_PUBLISH_ATTRIBUTION
                    or confidence < MIN_CONFIDENCE
                    or cope_result.get("needs_review")):
                db.update_submission(
                    sub_id, "rejected", extracted_text=text[:500],
                    url_type=url_type,
                    error_msg="Attribution did not pass publication gate",
                )
                continue
            added = db.add_cope_entry(
                figure_id=figure_id,
                article_slug=None,
                quote=cope_result.get("cope_quote") or quote_context[:300],
                source_url=url,
                source_title=title or url,
                cope_score=cope_result["cope_score"],
                cope_type=cope_result.get("cope_type", "unknown"),
                analysis_md=cope_result.get("analysis", ""),
                model=cope_result.get("model", ""),
                channel="user_submission",
            )

            was_inserted = bool(added and added.get("inserted"))
            db.update_submission(
                sub_id, "scored" if was_inserted else "duplicate",
                extracted_text=text[:2000], url_type=url_type,
                error_msg=None if was_inserted else "Underlying event already scored",
            )
            scored += int(was_inserted)
            log.info("[%d] Scored: %s = %.0f/100 (%s)",
                     sub_id, figure_name,
                     cope_result["cope_score"],
                     cope_result.get("cope_type", ""))
            time.sleep(2)  # be nice to APIs

        except Exception as e:
            log.error("[%d] Scoring failed: %s", sub_id, e)
            db.update_submission(sub_id, "failed", extracted_text=text[:500],
                               url_type=url_type, error_msg=str(e)[:500])

    log.info("Done: %d/%d scored", scored, len(pending))
    return scored


if __name__ == "__main__":
    process_pending()
```
