steam.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Steam (store) for SearXNG."""
  3. from urllib.parse import urlencode
  4. from searx.utils import html_to_text
  5. from searx.result_types import EngineResults, MainResult
  6. about = {
  7. "website": 'https://store.steampowered.com/',
  8. "wikidata_id": 'Q337535',
  9. "use_official_api": False,
  10. "require_api_key": False,
  11. "results": 'JSON',
  12. }
  13. categories = []
  14. base_url = "https://store.steampowered.com"
  15. def request(query, params):
  16. query_params = {"term": query, "cc": "us", "l": "en"}
  17. params['url'] = f'{base_url}/api/storesearch/?{urlencode(query_params)}'
  18. return params
  19. def response(resp) -> EngineResults:
  20. results = EngineResults()
  21. search_results = resp.json()
  22. for item in search_results.get('items', []):
  23. app_id = item.get('id')
  24. currency = item.get('price', {}).get('currency', 'USD')
  25. price = item.get('price', {}).get('final', 0) / 100
  26. platforms = ', '.join([platform for platform, supported in item.get('platforms', {}).items() if supported])
  27. content = [f'Price: {price:.2f} {currency}', f'Platforms: {platforms}']
  28. results.add(
  29. MainResult(
  30. title=item.get('name'),
  31. content=html_to_text(' | '.join(content)),
  32. url=f'{base_url}/app/{app_id}',
  33. thumbnail=item.get('tiny_image', ''),
  34. )
  35. )
  36. return results