541f59ed6e
Frontend (the premium baseline): - The hero is now the ONLY image slot. Soft feed images get an atmospheric gradient overlay; no over-reliance on inconsistent RSS image quality. - Every secondary/lane card is a uniform typographic editorial tile: no thumbnails, equal visual weight, a faint topic wordmark watermark, a slim sage top accent, consistent source, reason text as the trust signal, visible Replace with quiet tuning actions. Fixes the jarring mixed-media row rhythm and removes muddy thumbnails entirely. Backend (composition): - _select_diverse now balances topics: no more than 2 of one topic while other topics have candidates (relaxing source then topic caps only to fill), so the daily five stop clustering medical/science items. Candidates now carry s.topic. Tests updated for the topic-balance contract (79 total). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
201 lines
7.2 KiB
Python
201 lines
7.2 KiB
Python
from __future__ import annotations
|
|
|
|
import sqlite3
|
|
from datetime import date
|
|
|
|
from .paywall import is_paywalled
|
|
|
|
|
|
def build_daily_brief(
|
|
conn: sqlite3.Connection,
|
|
brief_date: str | None = None,
|
|
limit: int = 5,
|
|
replace: bool = False,
|
|
window_days: int = 3,
|
|
) -> int:
|
|
target_date = brief_date or date.today().isoformat()
|
|
existing = conn.execute("SELECT id FROM daily_briefs WHERE brief_date = ?", (target_date,)).fetchone()
|
|
if existing and not replace:
|
|
return int(existing["id"])
|
|
if existing and replace:
|
|
conn.execute("DELETE FROM daily_briefs WHERE id = ?", (existing["id"],))
|
|
|
|
brief_id = conn.execute(
|
|
"INSERT INTO daily_briefs (brief_date, title) VALUES (?, ?)",
|
|
(target_date, f"Five Good Things Today - {target_date}"),
|
|
).lastrowid
|
|
|
|
rows = _candidate_articles(conn, target_date, window_days)
|
|
# A calm daily brief shouldn't repeatedly hand the reader a locked door:
|
|
# push paywalled candidates below readable ones (stable, so composite order
|
|
# is preserved within each group) before selecting the five.
|
|
rows = sorted(rows, key=lambda r: is_paywalled(r["canonical_url"]))
|
|
selected = _select_diverse(rows, limit)
|
|
for index, row in enumerate(selected, start=1):
|
|
conn.execute(
|
|
"""
|
|
INSERT INTO daily_brief_items (brief_id, article_id, rank, selection_reason)
|
|
VALUES (?, ?, ?, ?)
|
|
""",
|
|
(
|
|
brief_id,
|
|
row["id"],
|
|
index,
|
|
_selection_reason(row),
|
|
),
|
|
)
|
|
conn.commit()
|
|
return int(brief_id)
|
|
|
|
|
|
def show_brief(conn: sqlite3.Connection, brief_date: str | None = None, limit: int = 10) -> list[sqlite3.Row]:
|
|
target_date = brief_date or _latest_brief_date(conn)
|
|
if not target_date:
|
|
return []
|
|
return conn.execute(
|
|
"""
|
|
SELECT
|
|
b.brief_date,
|
|
bi.rank,
|
|
bi.selection_reason,
|
|
a.title,
|
|
a.description,
|
|
a.canonical_url,
|
|
a.published_at,
|
|
src.name AS source_name,
|
|
src.default_category,
|
|
s.constructive_score,
|
|
s.cortisol_score,
|
|
s.ragebait_score,
|
|
s.agency_score,
|
|
s.human_benefit_score,
|
|
s.reason_code,
|
|
s.reason_text,
|
|
s.model_name
|
|
FROM daily_briefs b
|
|
JOIN daily_brief_items bi ON bi.brief_id = b.id
|
|
JOIN articles a ON a.id = bi.article_id
|
|
JOIN sources src ON src.id = a.source_id
|
|
LEFT JOIN article_scores s ON s.article_id = a.id
|
|
WHERE b.brief_date = ?
|
|
ORDER BY bi.rank
|
|
LIMIT ?
|
|
""",
|
|
(target_date, limit),
|
|
).fetchall()
|
|
|
|
|
|
def _candidate_articles(
|
|
conn: sqlite3.Connection, target_date: str, window_days: int = 3
|
|
) -> list[sqlite3.Row]:
|
|
"""Brief candidates, sparse-day-proof.
|
|
|
|
Prefers articles dated on target_date, but widens to the preceding
|
|
`window_days` so the brief still fills on slow news days. Anything already
|
|
featured in a brief within the last 7 days (other than this same date, which
|
|
is being rebuilt) is excluded so backfilled stories cannot linger across
|
|
consecutive days.
|
|
"""
|
|
return conn.execute(
|
|
"""
|
|
SELECT
|
|
a.id,
|
|
a.title,
|
|
a.description,
|
|
a.canonical_url,
|
|
a.published_at,
|
|
a.discovered_at,
|
|
src.name AS source_name,
|
|
src.default_category,
|
|
src.trust_score,
|
|
s.constructive_score,
|
|
s.cortisol_score,
|
|
s.ragebait_score,
|
|
s.agency_score,
|
|
s.human_benefit_score,
|
|
s.novelty_score,
|
|
s.pr_risk_score,
|
|
s.reason_code,
|
|
s.reason_text,
|
|
s.model_name,
|
|
s.topic,
|
|
s.flavor,
|
|
CASE WHEN date(COALESCE(a.published_at, a.discovered_at)) = date(?)
|
|
THEN 1 ELSE 0 END AS is_today
|
|
FROM articles a
|
|
JOIN sources src ON src.id = a.source_id
|
|
JOIN article_scores s ON s.article_id = a.id
|
|
WHERE s.accepted = 1
|
|
AND a.duplicate_of IS NULL
|
|
AND date(COALESCE(a.published_at, a.discovered_at)) <= date(?)
|
|
AND date(COALESCE(a.published_at, a.discovered_at)) > date(?, '-' || ? || ' days')
|
|
AND a.id NOT IN (
|
|
SELECT bi.article_id
|
|
FROM daily_brief_items bi
|
|
JOIN daily_briefs b ON b.id = bi.brief_id
|
|
WHERE b.brief_date <> ?
|
|
AND b.brief_date <= date(?)
|
|
AND b.brief_date > date(?, '-7 days')
|
|
)
|
|
ORDER BY
|
|
is_today DESC,
|
|
(s.constructive_score + s.agency_score + s.human_benefit_score + src.trust_score
|
|
- s.cortisol_score - s.ragebait_score - s.pr_risk_score) DESC,
|
|
COALESCE(a.published_at, a.discovered_at) DESC
|
|
LIMIT 50
|
|
""",
|
|
(target_date, target_date, target_date, window_days, target_date, target_date, target_date),
|
|
).fetchall()
|
|
|
|
|
|
def _select_diverse(rows: list[sqlite3.Row], limit: int, max_per_topic: int = 2) -> list[sqlite3.Row]:
|
|
"""Pick up to `limit` items from `rows` (already ranked best-first).
|
|
|
|
Contract:
|
|
1. Prefer higher-ranked items.
|
|
2. Source diversity: at most one item per source while other sources remain.
|
|
3. Topic balance: no more than `max_per_topic` of the same topic while other
|
|
topics still have candidates — so the five don't cluster (e.g. several
|
|
medical/science items) when community/culture/animals/environment exist.
|
|
4. Always fill to `limit` when enough candidates exist: the source and topic
|
|
caps are relaxed (in that order) only as needed to reach the count.
|
|
"""
|
|
selected: list[sqlite3.Row] = []
|
|
selected_ids: set = set()
|
|
seen_sources: set = set()
|
|
topic_count: dict = {}
|
|
|
|
def consider(enforce_source: bool, enforce_topic: bool) -> None:
|
|
for row in rows:
|
|
if len(selected) >= limit:
|
|
return
|
|
if row["id"] in selected_ids:
|
|
continue
|
|
if enforce_source and row["source_name"] in seen_sources:
|
|
continue
|
|
topic = row["topic"]
|
|
if enforce_topic and topic_count.get(topic, 0) >= max_per_topic:
|
|
continue
|
|
selected.append(row)
|
|
selected_ids.add(row["id"])
|
|
seen_sources.add(row["source_name"])
|
|
topic_count[topic] = topic_count.get(topic, 0) + 1
|
|
|
|
consider(enforce_source=True, enforce_topic=True) # distinct source, topic-balanced
|
|
consider(enforce_source=True, enforce_topic=False) # relax topic cap to fill
|
|
consider(enforce_source=False, enforce_topic=False) # relax source too, last resort
|
|
return selected
|
|
|
|
|
|
def _selection_reason(row: sqlite3.Row) -> str:
|
|
return (
|
|
f"{row['reason_code']}; constructive={row['constructive_score']}, "
|
|
f"agency={row['agency_score']}, human_benefit={row['human_benefit_score']}, "
|
|
f"cortisol={row['cortisol_score']}, source={row['source_name']}"
|
|
)
|
|
|
|
|
|
def _latest_brief_date(conn: sqlite3.Connection) -> str | None:
|
|
row = conn.execute("SELECT brief_date FROM daily_briefs ORDER BY brief_date DESC LIMIT 1").fetchone()
|
|
return row["brief_date"] if row else None
|