import requests
from bs4 import BeautifulSoup
import re
import time
import random
from fake_useragent import UserAgent

class WebCrawler:
    def __init__(self, year, industry):
        self.year = year
        self.industry = industry
        self.ua = UserAgent()

    def search_google(self):
        """Symulacja wyszukiwania Google"""
        query = f'"{self.industry}" site:*.pl intitle:kontakt before:{self.year}'
        headers = {'User-Agent': self.ua.random}
        
        try:
            response = requests.get(
                'https://www.google.com/search', 
                params={'q': query, 'num': 100},
                headers=headers
            )
            
            soup = BeautifulSoup(response.text, 'html.parser')
            links = [a['href'] for a in soup.find_all('a', href=True) 
                     if a['href'].startswith('http') and 'google.com' not in a['href']]
            
            return list(set(links))[:100]  # Usuwanie duplikatów
        except Exception as e:
            print(f"Błąd wyszukiwania: {e}")
            return []

    def extract_emails(self, url):
        """Ekstrakcja adresów email"""
        try:
            headers = {'User-Agent': self.ua.random}
            response = requests.get(url, headers=headers, timeout=10)
            
            # Zaawansowane wyrażenie regularne do ekstrakcji email
            email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
            emails = re.findall(email_pattern, response.text)
            
            return list(set(emails))  # Usuwanie duplikatów
        except Exception as e:
            print(f"Błąd pobierania {url}: {e}")
            return []

    def run_crawler(self):
        """Główna metoda crawlera"""
        results = {}
        websites = self.search_google()
        
        for website in websites:
            print(f"Przetwarzanie: {website}")
            emails = self.extract_emails(website)
            
            if emails:
                results[website] = emails
            
            # Losowe opóźnienie między zapytaniami
            time.sleep(random.uniform(1, 3))
        
        return results