fix(news): robust gnews URL extraction + smarter lazy trigger

- Replace fragile entry.get("link") with _gnews_entry_url() helper that
  checks entry.link attribute then falls back to entry.links[].href,
  fixing cases where feedparser puts the URL in a non-standard location
- Lazy news re-fetch on bill detail now only triggers when the stored
  trend score confirms gnews_count > 0, preventing endless re-queuing
  for bills with genuinely no news coverage

Co-Authored-By: Jack Levy
This commit is contained in:
Jack Levy
2026-03-01 00:49:02 -05:00
parent 50f93468db
commit b57833d4b7
2 changed files with 34 additions and 14 deletions

View File

@@ -110,8 +110,11 @@ async def get_bill(bill_id: str, db: AsyncSession = Depends(get_db)):
if bill.trend_scores:
detail.latest_trend = bill.trend_scores[0]
# Trigger a background news refresh if no articles are stored yet
if not bill.news_articles:
# Trigger a background news refresh if no articles are stored but trend
# data shows there are gnews results out there waiting to be fetched.
latest_trend = bill.trend_scores[0] if bill.trend_scores else None
has_gnews = latest_trend and (latest_trend.gnews_count or 0) > 0
if not bill.news_articles and has_gnews:
try:
from app.workers.news_fetcher import fetch_news_for_bill
fetch_news_for_bill.delay(bill_id)