GitHub Case Study
2 Modular Repositories

SafeNews - AI-Powered News Aggregation & Content Filtering Platform

Select Module Case Study:

πŸ›‘οΈ 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.

Python 3.11+ Gemini 2.5 Flash Trafilatura Cloud Firestore GitHub Actions Zero Server Cost License MIT


πŸ“‘ Table of Contents


🌐 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

Rendering Architecture Diagram...

πŸ“Š Production Benchmark

Comprehensive measurements from live production crawl runs across 48+ articles:

MetricBefore (Search Grounding)After (Trafilatura + Gemini 2.5 Flash)Impact / Optimization
Web Text Extraction Latency8.5s - 12.0s0.28s - 0.49s⚑ 20x Faster
End-to-End Pipeline Latency9.8s / article3.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 CostVPS ($5 - $10/mo)$0.00 / month (GitHub Actions)πŸŽ‰ 100% Free Serverless
Negative / Toxic Filter Accuracy92.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:

  1. ActiveLearner queries unhandled reports.
  2. Gemini analyzes the reported context and extracts new negative regex patterns.
  3. Patterns are dynamically saved to config/learned_patterns.json and immediately loaded into RuleFilter for 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.

SafeNews - AI-Powered News Aggregation & Content Filtering Platform | Nguyen Tran Anh Khoa