#!/usr/bin/env python3
"""
eBay Cast Iron Scanner
Scans eBay for cast iron cookware deals
"""
import requests
import re
from datetime import datetime
from bs4 import BeautifulSoup

def search_ebay_cast_iron():
    """
    Search eBay for cast iron items
    Returns list of items found
    """
    # eBay search URL for cast iron cookware
    # Using their REST API would be better but requires API keys
    # For now, we'll use RSS feeds which are public
    
    search_terms = [
        "griswold skillet",
        "wagner cast iron",
        "vintage cast iron skillet",
        "cast iron restoration",
        "wapak skillet",
        "birmingham skillet"
    ]
    
    items = []
    
    for term in search_terms:
        # eBay RSS feed (no API key needed!)
        rss_url = f"https://www.ebay.com/sch/i.html?_from=R40&_nkw={term.replace(' ', '%20')}&_sacat=0&LH_TitleDesc=0&_rss=1"
        
        try:
            response = requests.get(rss_url, timeout=10)
            if response.status_code == 200:
                soup = BeautifulSoup(response.content, 'lxml-xml')
                entries = soup.find_all('item')
                
                for entry in entries[:10]:  # Top 10 results
                    try:
                        title = entry.find('title').text
                        link = entry.find('link').text
                        pub_date = entry.find('pubDate').text
                        
                        # Extract price from description or title
                        price_match = re.search(r'\$([\d,]+\.?\d*)', title)
                        price = float(price_match.group(1).replace(',', '')) if price_match else 0
                        
                        items.append({
                            'title': title,
                            'price': price,
                            'link': link,
                            'source': 'eBay',
                            'found_at': datetime.now().isoformat(),
                            'pub_date': pub_date
                        })
                    except Exception as e:
                        continue
        except Exception as e:
            print(f"Error scanning eBay for '{term}': {e}")
    
    return items

if __name__ == "__main__":
    print("🔍 Scanning eBay for cast iron deals...")
    items = search_ebay_cast_iron()
    print(f"Found {len(items)} items")
    for item in items[:5]:
        print(f"  - {item['title'][:60]} - ${item['price']}")
