|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Quick Start Script for ScrapeGraphAI Elasticsearch Demo |
| 4 | +
|
| 5 | +This script provides an interactive demo of the key features. |
| 6 | +Run this after setting up Elasticsearch to see the integration in action. |
| 7 | +""" |
| 8 | + |
| 9 | +import sys |
| 10 | +import os |
| 11 | + |
| 12 | +# Add parent directory to path |
| 13 | +sys.path.insert(0, os.path.abspath(os.path.dirname(__file__))) |
| 14 | + |
| 15 | +from src.scrapegraph_demo import Config, ElasticsearchClient, MarketplaceScraper |
| 16 | +from src.scrapegraph_demo.models import ProductComparison |
| 17 | + |
| 18 | + |
| 19 | +def print_header(text): |
| 20 | + """Print a formatted header""" |
| 21 | + print("\n" + "=" * 70) |
| 22 | + print(f" {text}") |
| 23 | + print("=" * 70 + "\n") |
| 24 | + |
| 25 | + |
| 26 | +def print_step(number, text): |
| 27 | + """Print a formatted step""" |
| 28 | + print(f"\n[{number}] {text}") |
| 29 | + print("-" * 70) |
| 30 | + |
| 31 | + |
| 32 | +def wait_for_user(): |
| 33 | + """Wait for user to press Enter""" |
| 34 | + input("\nPress Enter to continue...") |
| 35 | + |
| 36 | + |
| 37 | +def main(): |
| 38 | + """Run the interactive quickstart demo""" |
| 39 | + |
| 40 | + print_header("ScrapeGraphAI Elasticsearch Demo - Quick Start") |
| 41 | + |
| 42 | + print("This interactive demo will showcase:") |
| 43 | + print(" • Configuration loading") |
| 44 | + print(" • Elasticsearch connection") |
| 45 | + print(" • Product scraping (using mock data)") |
| 46 | + print(" • Data indexing") |
| 47 | + print(" • Product search") |
| 48 | + print(" • Product comparison") |
| 49 | + print() |
| 50 | + |
| 51 | + input("Press Enter to begin...") |
| 52 | + |
| 53 | + # Step 1: Load Configuration |
| 54 | + print_step(1, "Loading Configuration") |
| 55 | + config = Config.from_env() |
| 56 | + print(f"✓ Configuration loaded") |
| 57 | + print(f" Elasticsearch URL: {config.elasticsearch_url}") |
| 58 | + wait_for_user() |
| 59 | + |
| 60 | + # Step 2: Connect to Elasticsearch |
| 61 | + print_step(2, "Connecting to Elasticsearch") |
| 62 | + try: |
| 63 | + es_client = ElasticsearchClient(config) |
| 64 | + print(f"✓ Connected to Elasticsearch") |
| 65 | + print(f" Index name: {es_client.INDEX_NAME}") |
| 66 | + es_connected = True |
| 67 | + except Exception as e: |
| 68 | + print(f"✗ Could not connect to Elasticsearch: {e}") |
| 69 | + print("\nNote: Elasticsearch is not running. Continuing with mock data only.") |
| 70 | + print("To use Elasticsearch, run: docker-compose up -d") |
| 71 | + es_connected = False |
| 72 | + wait_for_user() |
| 73 | + |
| 74 | + # Step 3: Initialize Scraper |
| 75 | + print_step(3, "Initializing Marketplace Scraper") |
| 76 | + scraper = MarketplaceScraper(config) |
| 77 | + print("✓ Scraper initialized") |
| 78 | + print(" Using mock data for demonstration") |
| 79 | + wait_for_user() |
| 80 | + |
| 81 | + # Step 4: Scrape Products |
| 82 | + print_step(4, "Scraping Products from Multiple Marketplaces") |
| 83 | + |
| 84 | + search_query = "wireless headphones" |
| 85 | + marketplaces = ["Amazon", "eBay", "BestBuy"] |
| 86 | + all_products = [] |
| 87 | + |
| 88 | + print(f"Search query: '{search_query}'") |
| 89 | + print() |
| 90 | + |
| 91 | + for marketplace in marketplaces: |
| 92 | + print(f" Scraping {marketplace}...", end=" ") |
| 93 | + products = scraper.scrape_search_results(search_query, marketplace, max_results=2) |
| 94 | + all_products.extend(products) |
| 95 | + print(f"✓ Found {len(products)} products") |
| 96 | + |
| 97 | + print(f"\n✓ Total products scraped: {len(all_products)}") |
| 98 | + wait_for_user() |
| 99 | + |
| 100 | + # Step 5: Display Sample Products |
| 101 | + print_step(5, "Sample Product Data") |
| 102 | + |
| 103 | + for i, product in enumerate(all_products[:3], 1): |
| 104 | + print(f"\nProduct {i}:") |
| 105 | + print(f" Name: {product.name}") |
| 106 | + print(f" Price: ${product.price:.2f} {product.currency}") |
| 107 | + print(f" Marketplace: {product.marketplace}") |
| 108 | + print(f" Brand: {product.brand}") |
| 109 | + print(f" Rating: {product.rating}/5.0") |
| 110 | + print(f" Reviews: {product.review_count}") |
| 111 | + |
| 112 | + wait_for_user() |
| 113 | + |
| 114 | + # Step 6: Index Products (if Elasticsearch is available) |
| 115 | + if es_connected: |
| 116 | + print_step(6, "Indexing Products in Elasticsearch") |
| 117 | + |
| 118 | + try: |
| 119 | + success, failed = es_client.index_products(all_products) |
| 120 | + print(f"✓ Successfully indexed {success} products") |
| 121 | + if failed: |
| 122 | + print(f"✗ Failed to index {len(failed)} products") |
| 123 | + except Exception as e: |
| 124 | + print(f"✗ Error indexing products: {e}") |
| 125 | + es_connected = False |
| 126 | + |
| 127 | + wait_for_user() |
| 128 | + |
| 129 | + # Step 7: Product Comparison |
| 130 | + print_step(7, "Product Comparison Analysis") |
| 131 | + |
| 132 | + comparison = ProductComparison( |
| 133 | + query=search_query, |
| 134 | + products=all_products |
| 135 | + ) |
| 136 | + |
| 137 | + print(f"Query: {comparison.query}") |
| 138 | + print(f"Total products: {len(comparison.products)}") |
| 139 | + print() |
| 140 | + |
| 141 | + # Price analysis |
| 142 | + min_price, max_price = comparison.get_price_range() |
| 143 | + print(f"Price Range: ${min_price:.2f} - ${max_price:.2f}") |
| 144 | + print() |
| 145 | + |
| 146 | + # Cheapest product |
| 147 | + cheapest = comparison.get_cheapest() |
| 148 | + print("Cheapest Product:") |
| 149 | + print(f" {cheapest.name}") |
| 150 | + print(f" ${cheapest.price:.2f} on {cheapest.marketplace}") |
| 151 | + print() |
| 152 | + |
| 153 | + # Best rated |
| 154 | + best_rated = comparison.get_best_rated() |
| 155 | + if best_rated: |
| 156 | + print("Best Rated Product:") |
| 157 | + print(f" {best_rated.name}") |
| 158 | + print(f" {best_rated.rating}/5.0 ({best_rated.review_count} reviews)") |
| 159 | + print(f" ${best_rated.price:.2f} on {best_rated.marketplace}") |
| 160 | + |
| 161 | + wait_for_user() |
| 162 | + |
| 163 | + # Step 8: Products by Marketplace |
| 164 | + print_step(8, "Products Grouped by Marketplace") |
| 165 | + |
| 166 | + grouped = comparison.group_by_marketplace() |
| 167 | + for marketplace, products in grouped.items(): |
| 168 | + print(f"\n{marketplace} ({len(products)} products):") |
| 169 | + for product in products: |
| 170 | + print(f" • {product.name}") |
| 171 | + print(f" ${product.price:.2f} | {product.rating}/5.0") |
| 172 | + |
| 173 | + wait_for_user() |
| 174 | + |
| 175 | + # Step 9: Search (if Elasticsearch is available) |
| 176 | + if es_connected: |
| 177 | + print_step(9, "Searching Products in Elasticsearch") |
| 178 | + |
| 179 | + try: |
| 180 | + # Search with price filter |
| 181 | + print(f"Searching for '{search_query}' under $50...") |
| 182 | + results = es_client.search_products(search_query, max_price=50.0, size=5) |
| 183 | + |
| 184 | + print(f"\n✓ Found {len(results)} results:") |
| 185 | + for i, product in enumerate(results, 1): |
| 186 | + print(f"\n{i}. {product.name}") |
| 187 | + print(f" ${product.price:.2f} | {product.marketplace}") |
| 188 | + print(f" Rating: {product.rating}/5.0") |
| 189 | + |
| 190 | + # Statistics |
| 191 | + print("\n" + "-" * 70) |
| 192 | + stats = es_client.aggregate_by_marketplace() |
| 193 | + print("\nProducts by marketplace:") |
| 194 | + for marketplace, count in stats.items(): |
| 195 | + print(f" {marketplace}: {count}") |
| 196 | + |
| 197 | + price_stats = es_client.get_price_statistics() |
| 198 | + print(f"\nPrice Statistics:") |
| 199 | + print(f" Average: ${price_stats['avg']:.2f}") |
| 200 | + print(f" Min: ${price_stats['min']:.2f}") |
| 201 | + print(f" Max: ${price_stats['max']:.2f}") |
| 202 | + |
| 203 | + except Exception as e: |
| 204 | + print(f"Error searching: {e}") |
| 205 | + |
| 206 | + wait_for_user() |
| 207 | + |
| 208 | + # Final message |
| 209 | + print_header("Quick Start Complete!") |
| 210 | + |
| 211 | + print("✓ You've successfully explored the ScrapeGraphAI Elasticsearch Demo!") |
| 212 | + print() |
| 213 | + print("Next Steps:") |
| 214 | + print(" 1. Review the README.md for detailed documentation") |
| 215 | + print(" 2. Check out examples/ directory for more use cases") |
| 216 | + print(" 3. Run the test suite: python run_tests.py") |
| 217 | + print(" 4. Try the example scripts:") |
| 218 | + print(" - python examples/basic_usage.py") |
| 219 | + print(" - python examples/product_comparison.py") |
| 220 | + print(" - python examples/advanced_search.py") |
| 221 | + print() |
| 222 | + |
| 223 | + if es_connected: |
| 224 | + print(" 5. Access Kibana at http://localhost:5601 for data visualization") |
| 225 | + print() |
| 226 | + es_client.close() |
| 227 | + else: |
| 228 | + print(" 5. Start Elasticsearch: docker-compose up -d") |
| 229 | + print(" Then re-run this quickstart script") |
| 230 | + print() |
| 231 | + |
| 232 | + print("Thank you for trying ScrapeGraphAI Elasticsearch Demo! 🎉") |
| 233 | + print() |
| 234 | + |
| 235 | + |
| 236 | +if __name__ == "__main__": |
| 237 | + try: |
| 238 | + main() |
| 239 | + except KeyboardInterrupt: |
| 240 | + print("\n\nQuickstart interrupted by user.") |
| 241 | + sys.exit(0) |
| 242 | + except Exception as e: |
| 243 | + print(f"\n\nError: {e}") |
| 244 | + sys.exit(1) |
0 commit comments