|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Cassandra Database Integration Demo |
| 4 | +Demonstrates distributed data storage, deduplication, and seed management. |
| 5 | +""" |
| 6 | + |
| 7 | +import asyncio |
| 8 | +from datetime import datetime |
| 9 | +from src.database.cassandra_manager import CassandraManager, CassandraConfig |
| 10 | +from src.schemas.news import NewsArticle |
| 11 | + |
| 12 | + |
| 13 | +async def main(): |
| 14 | + """Demonstrate Cassandra database integration capabilities.""" |
| 15 | + print("🗄️ Cassandra Database Integration Demo") |
| 16 | + print("=" * 50) |
| 17 | + |
| 18 | + # Configuration |
| 19 | + config = CassandraConfig( |
| 20 | + hosts=["localhost"], |
| 21 | + keyspace="web_scraper_demo", |
| 22 | + replication_factor=1 |
| 23 | + ) |
| 24 | + |
| 25 | + try: |
| 26 | + # Initialize database connection |
| 27 | + print("\n🔌 Connecting to Cassandra...") |
| 28 | + manager = CassandraManager(config) |
| 29 | + await manager.connect() |
| 30 | + print("✅ Connected successfully!") |
| 31 | + |
| 32 | + # Demo 1: Store sample articles |
| 33 | + print("\n📰 Storing sample articles...") |
| 34 | + |
| 35 | + sample_articles = [ |
| 36 | + # Only title and url are required - all other fields are Optional |
| 37 | + NewsArticle( # type: ignore |
| 38 | + title="Revolutionary AI Breakthrough in Healthcare", |
| 39 | + content="Researchers have developed an AI system that can diagnose diseases " |
| 40 | + "with 95% accuracy, potentially transforming medical care worldwide.", |
| 41 | + url="https://example.com/ai-healthcare-breakthrough", |
| 42 | + author="Dr. Jane Smith" |
| 43 | + ), |
| 44 | + NewsArticle( # type: ignore |
| 45 | + title="Climate Change Solutions: New Carbon Capture Technology", |
| 46 | + content="Scientists unveil innovative carbon capture technology that could " |
| 47 | + "remove millions of tons of CO2 from the atmosphere annually.", |
| 48 | + url="https://example.com/carbon-capture-tech" |
| 49 | + ), |
| 50 | + NewsArticle( # type: ignore |
| 51 | + title="Quantum Computing Milestone Achieved", |
| 52 | + content="Tech giant announces quantum computer with 1000+ qubits, bringing " |
| 53 | + "practical quantum computing closer to reality.", |
| 54 | + url="https://example.com/quantum-milestone", |
| 55 | + author="Tech Reporter" |
| 56 | + ) |
| 57 | + ] |
| 58 | + |
| 59 | + stored_count = 0 |
| 60 | + duplicate_count = 0 |
| 61 | + |
| 62 | + for article in sample_articles: |
| 63 | + was_stored = await manager.store_article(article, "generic_news") |
| 64 | + if was_stored: |
| 65 | + stored_count += 1 |
| 66 | + print(f" ✅ Stored: {article.title[:50]}...") |
| 67 | + else: |
| 68 | + duplicate_count += 1 |
| 69 | + print(f" ⚠️ Duplicate: {article.title[:50]}...") |
| 70 | + |
| 71 | + print(f"\n📊 Storage Results: {stored_count} stored, {duplicate_count} duplicates") |
| 72 | + |
| 73 | + # Demo 2: Test deduplication |
| 74 | + print("\n🔄 Testing deduplication...") |
| 75 | + |
| 76 | + # Try to store the same article again |
| 77 | + duplicate_article = sample_articles[0] # First article again |
| 78 | + was_stored = await manager.store_article(duplicate_article, "generic_news") |
| 79 | + |
| 80 | + if not was_stored: |
| 81 | + print("✅ Deduplication working correctly - duplicate detected and skipped") |
| 82 | + else: |
| 83 | + print("❌ Deduplication failed - duplicate was stored") |
| 84 | + |
| 85 | + # Demo 3: Add seed URLs |
| 86 | + print("\n🌱 Managing seed URLs...") |
| 87 | + |
| 88 | + seed_urls = [ |
| 89 | + { |
| 90 | + "url": "https://techcrunch.com", |
| 91 | + "label": "h2 a", |
| 92 | + "parser": "news", |
| 93 | + "priority": 8 |
| 94 | + }, |
| 95 | + { |
| 96 | + "url": "https://news.ycombinator.com", |
| 97 | + "label": "a.storylink", |
| 98 | + "parser": "news", |
| 99 | + "priority": 6 |
| 100 | + }, |
| 101 | + { |
| 102 | + "url": "https://reddit.com/r/technology", |
| 103 | + "label": "a[data-click-id='body']", |
| 104 | + "parser": "generic_news", |
| 105 | + "priority": 5 |
| 106 | + } |
| 107 | + ] |
| 108 | + |
| 109 | + for seed in seed_urls: |
| 110 | + await manager.add_seed_url( |
| 111 | + url=seed["url"], |
| 112 | + label=seed["label"], |
| 113 | + parser=seed["parser"], |
| 114 | + priority=seed["priority"] |
| 115 | + ) |
| 116 | + print(f" ✅ Added seed: {seed['url']}") |
| 117 | + |
| 118 | + # Demo 4: Retrieve seeds from database |
| 119 | + print("\n📋 Retrieving seeds from database...") |
| 120 | + |
| 121 | + seeds = await manager.get_seed_urls(limit=10) |
| 122 | + print(f"Found {len(seeds)} active seeds:") |
| 123 | + |
| 124 | + for i, seed in enumerate(seeds, 1): |
| 125 | + print(f" {i}. {seed['url']}") |
| 126 | + print(f" Label: {seed['label']}") |
| 127 | + print(f" Parser: {seed['parser']}") |
| 128 | + |
| 129 | + # Demo 5: Get crawl statistics |
| 130 | + print("\n📈 Crawl Statistics...") |
| 131 | + |
| 132 | + stats = await manager.get_crawl_statistics(days=1) |
| 133 | + if stats: |
| 134 | + for metric, count in stats.items(): |
| 135 | + print(f" {metric}: {count}") |
| 136 | + else: |
| 137 | + print(" No statistics available yet") |
| 138 | + |
| 139 | + print("\n🎯 Key Features Demonstrated:") |
| 140 | + print(" ✅ Distributed data storage with Cassandra") |
| 141 | + print(" ✅ Automatic URL and content deduplication") |
| 142 | + print(" ✅ Dynamic seed URL management from database") |
| 143 | + print(" ✅ Time-series data tracking and statistics") |
| 144 | + print(" ✅ Scalable architecture for high-volume scraping") |
| 145 | + print(" ✅ Content versioning and change tracking") |
| 146 | + |
| 147 | + print("\n🔧 Database Architecture:") |
| 148 | + print(" • Articles table: Main content storage with partitioning") |
| 149 | + print(" • URL tracker: Deduplication and processing history") |
| 150 | + print(" • Seeds table: Dynamic crawl target management") |
| 151 | + print(" • Statistics: Performance metrics and monitoring") |
| 152 | + print(" • History: Content versioning and change detection") |
| 153 | + |
| 154 | + print("\n🚀 Production Benefits:") |
| 155 | + print(" • High write throughput for large-scale scraping") |
| 156 | + print(" • Horizontal scaling across multiple nodes") |
| 157 | + print(" • No single point of failure with replication") |
| 158 | + print(" • Efficient time-series data for analytics") |
| 159 | + print(" • Schema flexibility for varying content structures") |
| 160 | + |
| 161 | + # Cleanup |
| 162 | + await manager.close() |
| 163 | + print("\n✨ Demo completed successfully!") |
| 164 | + |
| 165 | + except Exception as e: |
| 166 | + print(f"\n❌ Demo failed: {e}") |
| 167 | + print("\n💡 Make sure Cassandra is running:") |
| 168 | + print(" docker-compose -f docker-compose.cassandra.yml up -d cassandra") |
| 169 | + |
| 170 | + |
| 171 | +if __name__ == "__main__": |
| 172 | + asyncio.run(main()) |
0 commit comments