serpstack.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import json
  2. import logging
  3. import requests
  4. from apps.rag.search.main import SearchResult
  5. from config import SRC_LOG_LEVELS
  6. log = logging.getLogger(__name__)
  7. log.setLevel(SRC_LOG_LEVELS["RAG"])
  8. def search_serpstack(
  9. api_key: str, query: str, count: int, https_enabled: bool = True
  10. ) -> list[SearchResult]:
  11. """Search using serpstack.com's and return the results as a list of SearchResult objects.
  12. Args:
  13. api_key (str): A serpstack.com API key
  14. query (str): The query to search for
  15. https_enabled (bool): Whether to use HTTPS or HTTP for the API request
  16. """
  17. url = f"{'https' if https_enabled else 'http'}://api.serpstack.com/search"
  18. headers = {"Content-Type": "application/json"}
  19. params = {
  20. "access_key": api_key,
  21. "query": query,
  22. }
  23. response = requests.request("POST", url, headers=headers, params=params)
  24. response.raise_for_status()
  25. json_response = response.json()
  26. results = sorted(
  27. json_response.get("organic_results", []), key=lambda x: x.get("position", 0)
  28. )
  29. return [
  30. SearchResult(
  31. link=result["url"], title=result.get("title"), snippet=result.get("snippet")
  32. )
  33. for result in results[:count]
  34. ]