"""DuckDuckGo site preset."""
import re

class DuckDuckGo:
    """Extract structured data from DuckDuckGo search results."""
    
    DOMAINS = ["duckduckgo.com", "www.duckduckgo.com"]
    
    @staticmethod
    def extract(url, html):
        """Extract search results from HTML."""
        data = {"url": url, "site": "duckduckgo"}
        
        # Title
        m = re.search(r'<title[^>]*>([^<]+)</title>', html, re.I)
        if m:
            data["title"] = m.group(1).strip()
        
        # Search results
        results = re.findall(r'<a[^>]*class="[^"]*result__a[^"]*"[^>]*href=["\'](.*?)["\'][^>]*>(.*?)</a>', html, re.I | re.S)
        if results:
            data["results"] = [{"url": u, "title": re.sub(r'<[^>]+>', '', t).strip()} for u, t in results[:20]]
        
        # Snippets
        snippets = re.findall(r'<a[^>]*class="[^"]*result__snippet[^"]*"[^>]*>(.*?)</a>', html, re.I | re.S)
        if snippets:
            data["snippets"] = [re.sub(r'<[^>]+>', '', s).strip() for s in snippets[:20]]
        
        return data
