fix: pipeline reliability — WS, summarizer, ingest, health
Stop the live HUD reconnect storm (nginx WS snippet + backoff), copy intel/nous_client into the summarizer image, and make event ingest idempotent on URL. GDELT uses the DOC API; NWS no longer sends bbox; FIRMS is one ON CONFLICT batch; GET /api/aircraft serves last-known. Health reports freshness without 503ing docker. EONET + CISA KEV added.
This commit is contained in:
parent
c6bfc50705
commit
d81ea68dec
26 changed files with 1164 additions and 110 deletions
32
news/scraper/newsScraper/feed_util.py
Normal file
32
news/scraper/newsScraper/feed_util.py
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
"""Feed helpers shared by the news spider (no Scrapy import)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime
|
||||
from email.utils import parsedate_to_datetime
|
||||
from urllib.parse import urlparse
|
||||
|
||||
AUDIO_EXT = (".mp3", ".m4a", ".ogg", ".wav", ".aac", ".flac", ".opus")
|
||||
|
||||
|
||||
def is_audio_url(url: str) -> bool:
|
||||
path = urlparse(url or "").path.lower()
|
||||
return any(path.endswith(ext) for ext in AUDIO_EXT)
|
||||
|
||||
|
||||
def article_timestamp(pub_date: str | None) -> datetime.datetime:
|
||||
"""Prefer the feed's pubDate/published; fall back to now (UTC)."""
|
||||
if pub_date:
|
||||
try:
|
||||
return parsedate_to_datetime(pub_date).astimezone(datetime.timezone.utc)
|
||||
except (TypeError, ValueError, IndexError):
|
||||
pass
|
||||
try:
|
||||
raw = pub_date.replace("Z", "+00:00")
|
||||
ts = datetime.datetime.fromisoformat(raw)
|
||||
if ts.tzinfo is None:
|
||||
ts = ts.replace(tzinfo=datetime.timezone.utc)
|
||||
return ts
|
||||
except ValueError:
|
||||
pass
|
||||
return datetime.datetime.now(datetime.timezone.utc)
|
||||
|
|
@ -3,7 +3,7 @@
|
|||
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
|
||||
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
|
||||
|
||||
import logging
|
||||
import logging
|
||||
import psycopg2
|
||||
import os
|
||||
from scrapy.exceptions import DropItem
|
||||
|
|
@ -53,8 +53,10 @@ class PostgresPipeline:
|
|||
self.connection.commit()
|
||||
|
||||
def process_item(self, item, spider):
|
||||
if item ['url'] in self.seen_urls:
|
||||
raise DropItem()
|
||||
url = item['url']
|
||||
if url in self.seen_urls:
|
||||
raise DropItem(f"Duplicate URL (in-memory): {url}")
|
||||
self.seen_urls.add(url)
|
||||
try:
|
||||
self.cur.execute("""
|
||||
INSERT INTO articles (title, url, content, domain, timestamp)
|
||||
|
|
@ -68,15 +70,16 @@ class PostgresPipeline:
|
|||
item['timestamp']
|
||||
))
|
||||
if self.cur.rowcount == 0:
|
||||
e = DropItem("Duplicate URL (database conflict)")
|
||||
e.log_level = logging.DEBUG
|
||||
raise e
|
||||
raise DropItem(f"Duplicate URL (database): {url}")
|
||||
self.connection.commit()
|
||||
return item
|
||||
return item
|
||||
except DropItem:
|
||||
self.connection.rollback()
|
||||
raise
|
||||
except Exception as e:
|
||||
spider.logger.error(f"Error saving to Postgres: {e}")
|
||||
self.connection.rollback()
|
||||
raise
|
||||
raise
|
||||
|
||||
def close_spider(self, spider):
|
||||
self.cur.close()
|
||||
|
|
@ -88,5 +91,3 @@ from itemadapter import ItemAdapter
|
|||
class NewsscraperPipeline:
|
||||
def process_item(self, item, spider):
|
||||
return item
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ from urllib.parse import urljoin, urlparse
|
|||
import datetime
|
||||
import re
|
||||
|
||||
from newsScraper.feed_util import article_timestamp, is_audio_url
|
||||
|
||||
|
||||
class NewsRSSSpider(Spider):
|
||||
"""Crawl the curated news sources in urls.txt and extract articles.
|
||||
|
|
@ -87,6 +89,8 @@ class NewsRSSSpider(Spider):
|
|||
or node.xpath('updated/text()').get()
|
||||
)
|
||||
if link:
|
||||
if is_audio_url(link):
|
||||
continue
|
||||
yield scrapy.Request(
|
||||
link,
|
||||
callback=self.parse_article,
|
||||
|
|
@ -112,5 +116,5 @@ class NewsRSSSpider(Spider):
|
|||
'url': response.url,
|
||||
'text': pure_text,
|
||||
'domain': urlparse(response.url).netloc,
|
||||
'timestamp': datetime.datetime.now().isoformat()
|
||||
'timestamp': article_timestamp(response.meta.get('date')).isoformat()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
|
|||
COPY requirements.txt .
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
COPY summarizer.py run_news_summarizer.py ./
|
||||
COPY summarizer.py run_news_summarizer.py intel.py nous_client.py ./
|
||||
|
||||
# Security: run as a non-privileged user.
|
||||
RUN useradd -m summarizer_user
|
||||
|
|
|
|||
Loading…
Reference in a new issue