---
type: "Source File"
title: "\"\"\""
description: "\"\"\" seo.copecheck.com — CopeCheck SEO tracking site. Port 8106. Single route: /search-progress/ \"\"\" import os import sqlite3 from datetime import date, timedelta from flask import Flask, render_template app =."
resource: "https://seo.copecheck.com"
tags: ["copecheck", "seo", "source-file", "py"]
timestamp: "2026-05-29T06:54:34Z"
site: "seo"
source_path: "copecheck-seo/app.py"
---

# """

Source path: `copecheck-seo/app.py`  
Index snapshot generated: `2026-11-24T13:27:52Z`  
File modified: `2026-05-29T06:54:34Z`

```py
"""
seo.copecheck.com — CopeCheck SEO tracking site.
Port 8106. Single route: /search-progress/
"""
import os
import sqlite3
from datetime import date, timedelta
from flask import Flask, render_template

app = Flask(__name__)

PAGE_VIEWS_DB   = "/home/ben/infra/copecheck/page_views.db"
COPECHECK_DB    = "/home/ben/infra/copecheck/data/copecheck.db"


def _pv_conn():
    con = sqlite3.connect(PAGE_VIEWS_DB, timeout=10)
    con.row_factory = sqlite3.Row
    return con


def _top_pages(days: int = 30, limit: int = 10) -> list:
    """Top /figure/ pages by rolling 30-day view count."""
    if not os.path.exists(PAGE_VIEWS_DB):
        return []
    cutoff = (date.today() - timedelta(days=days)).isoformat()
    try:
        con = _pv_conn()
        cur = con.execute(
            """SELECT slug, title, url,
                      SUM(view_count) AS total_views
               FROM page_views
               WHERE view_date >= ?
                 AND url LIKE '/figure/%'
               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("page_views query failed: %s", e)
        return []


def _figures_by_cope(days: int = 30, limit: int = 5,
                     order: str = "ASC") -> list:
    """
    Top /figure/ pages sorted by cope_score (ASC = Most Lucid, DESC = Most Cope).
    Joins page_views.db with copecheck.db figures table via ATTACH.
    Only includes figures that have had at least one view in the window.
    """
    if not os.path.exists(PAGE_VIEWS_DB) or not os.path.exists(COPECHECK_DB):
        return []
    cutoff = (date.today() - timedelta(days=days)).isoformat()
    try:
        con = _pv_conn()
        con.execute(f"ATTACH DATABASE '{COPECHECK_DB}' AS cc")
        cur = con.execute(
            f"""SELECT pv.slug, pv.title, pv.url,
                       SUM(pv.view_count) AS total_views,
                       f.cope_score
                FROM page_views pv
                JOIN cc.figures f ON f.id = pv.slug
                WHERE pv.view_date >= ?
                  AND pv.url LIKE '/figure/%'
                GROUP BY pv.slug
                ORDER BY f.cope_score {order}
                LIMIT ?""",
            (cutoff, limit),
        )
        rows = [dict(r) for r in cur.fetchall()]
        con.close()
        return rows
    except Exception as e:
        app.logger.error("figures_by_cope query failed: %s", e)
        return []


@app.route("/")
@app.route("/search-progress/")
@app.route("/search-progress")
def search_progress():
    pages       = _top_pages(days=30, limit=10)
    most_lucid  = _figures_by_cope(days=30, limit=5, order="ASC")
    most_cope   = _figures_by_cope(days=30, limit=5, order="DESC")
    today  = date.today().isoformat()
    cutoff = (date.today() - timedelta(days=30)).isoformat()
    return render_template(
        "search_progress.html",
        pages=pages,
        most_lucid=most_lucid,
        most_cope=most_cope,
        today=today,
        cutoff=cutoff,
        days=30,
    )


if __name__ == "__main__":
    app.run(host="127.0.0.1", port=8106, debug=False)
```
