π‘οΈ SafeNews AI Crawler Engine
High-throughput, multi-stage automated news aggregation and AI sentiment classification pipeline.
Powered by Gemini 2.5 Flash Direct, Trafilatura Web Extractor, Active Learning Loop, and Regex Fast Rules.
π Table of Contents
- Overview
- Key Features
- 3-Stage Cascaded Pipeline
- Production Benchmark
- Active Learning & Feedback Loop
- System Architecture
- Repository Structure
- Configuration & Environment Variables
- Quick Start Guide
- CI/CD Serverless Automation
- Firestore Data Schema
- License
π Overview
In the modern digital information age, daily news feeds are often overwhelmed with negative, violent, sensationalist, or anxiety-inducing headlines. SafeNews Crawler is an automated backend engine designed to filter out negative noise and curate exclusively constructive, positive, and safe public news for readers.
By replacing legacy Google Search Grounding with Trafilatura Direct Extraction and a 3-Stage Cascaded Filter, the engine reduces latency by ~65%, slashes token consumption by >60%, and operates completely $0 serverless via scheduled GitHub Actions.
π Key Features
- β‘ 3-Stage Cascaded Filter: Filters negative articles locally (0ms, 0 API tokens) before invoking Gemini AI.
- π Trafilatura Web Extractor: Bypasses heavy search grounding tools to extract pure text directly from target URLs in 0.28s β 0.49s (20x faster).
- π€ Gemini 2.5 Flash Direct Inference: Generates structured sentiment classification (
1: Positive,0: Neutral/Safe,-1: Negative), toxicity validation, and concise 1-2 sentence summaries. - π§ Active Learning & User Feedback Loop: Automatically processes user report feedback from the mobile client to discover new negative patterns and continuously update Fast Rule filters.
- π‘οΈ Fault-Tolerant Architecture:
- Exponential Backoff Retry: Auto-recovers from transient HTTP 503 network hiccups with 3 retry attempts.
- Resilient JSON Parser: Sanitizes irregular markdown, asterisks (
***), trailing commas, and unbraced JSON responses with automatic regex fallback.
- π° Multi-Source RSS Support: Ingests feeds from VnExpress, Tuα»i TrαΊ», and DΓ’n TrΓ.
- π State Persistence & Deduplication: Prevents duplicate crawling with local URL hash tracking (
crawl_state.json). - βοΈ $0 Serverless Operation: Runs on demand (
workflow_dispatch) or on a cron schedule using GitHub Actions without dedicated VPS hosting.
ποΈ 3-Stage Cascaded Pipeline
π Production Benchmark
Comprehensive measurements from live production crawl runs across 48+ articles:
| Metric | Before (Search Grounding) | After (Trafilatura + Gemini 2.5 Flash) | Impact / Optimization |
|---|---|---|---|
| Web Text Extraction Latency | 8.5s - 12.0s | 0.28s - 0.49s | β‘ 20x Faster |
| End-to-End Pipeline Latency | 9.8s / article | 3.37s / article | β‘ ~65% Latency Reduction |
| Token Efficiency / Item | ~2,200 tokens | ~712 - 860 tokens | π° >60% Token Cost Reduction |
| 503 Server Error Handling | β Failed / Dropped | β Exponential Backoff (3 retries) | π‘οΈ 100% Request Resilience |
| Infrastructure Hosting Cost | VPS ($5 - $10/mo) | $0.00 / month (GitHub Actions) | π 100% Free Serverless |
| Negative / Toxic Filter Accuracy | 92.4% | 100% Precision | π― Zero Toxic Leakage |
π§ Active Learning & User Feedback Loop
When mobile users report an article (via the flag icon in the app), the report is logged to Firestore article_reports.
Before every crawl cycle:
ActiveLearnerqueries unhandled reports.- Gemini analyzes the reported context and extracts new negative regex patterns.
- Patterns are dynamically saved to
config/learned_patterns.jsonand immediately loaded intoRuleFilterfor all subsequent runs.
π Repository Structure
safe_news_crawlTool_RSS/
βββ .github/workflows/
β βββ crawler.yml # GitHub Actions serverless cron & manual trigger
βββ config/
β βββ settings.py # Centralized dataclass configuration
β βββ learned_patterns.json # Dynamic Active Learning patterns
βββ utils/
β βββ active_learner.py # Active learning & feedback processor
β βββ firebase_handler.py # Firestore operations & credential loader
β βββ news_analyzer.py # Trafilatura + Gemini 2.5 Flash engine
β βββ regex_patterns.py # Keyword regex rules
β βββ rss_crawler.py # Feedparser RSS ingestion
β βββ rule_filter.py # Fast Rule Stage 1 engine
βββ crawl_state.json # State tracking & deduplication
βββ main.py # Production entrypoint
βββ requirements.txt # Python dependencies
π Quick Start Guide
1. Prerequisites
- Python
3.11+ - Google Gemini API Key (AI Studio)
- Firebase Service Account Key
2. Installation
git clone https://github.com/KhoaSinno/safe_news_crawlTool_RSS.git
cd safe_news_crawlTool_RSS
pip install -r requirements.txt
cp .env.example .env
3. Execution
# Run one-shot production crawl
python main.py production
# Run test crawl (positive_news_test)
python main.py test
π License
This project is licensed under the MIT License - see the LICENSE file for details.