main.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import validators
  2. from typing import Optional
  3. from urllib.parse import urlparse
  4. from pydantic import BaseModel
  5. def get_filtered_results(results, filter_list):
  6. if not filter_list:
  7. return results
  8. # Domains starting without "!" → allowed
  9. allow_list = [d for d in filter_list if not d.startswith("!")]
  10. # Domains starting with "!" → blocked
  11. block_list = [d[1:] for d in filter_list if d.startswith("!")]
  12. filtered_results = []
  13. for result in results:
  14. url = result.get("url") or result.get("link", "") or result.get("href", "")
  15. if not validators.url(url):
  16. continue
  17. domain = urlparse(url).netloc
  18. # If allow list is non-empty, require domain to match one of them
  19. if allow_list:
  20. if not any(domain.endswith(allowed) for allowed in allow_list):
  21. continue
  22. # Block list always removes matches
  23. if any(domain.endswith(blocked) for blocked in block_list):
  24. continue
  25. filtered_results.append(result)
  26. return filtered_results
  27. class SearchResult(BaseModel):
  28. link: str
  29. title: Optional[str]
  30. snippet: Optional[str]