prowlarr.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. Prowlarr search (Files)
  4. """
  5. from json import loads
  6. from urllib.parse import urlencode
  7. from searx.exceptions import SearxEngineAPIException
  8. categories = ''
  9. paging = False
  10. api_key = ''
  11. indexer_ids = ''
  12. search_type = 'search'
  13. search_categories = ''
  14. base_url = ''
  15. def request(query, params):
  16. if not base_url:
  17. raise SearxEngineAPIException('missing prowlarr base url')
  18. if not api_key:
  19. raise SearxEngineAPIException('missing prowlarr API key')
  20. query_args = {
  21. 'query': query,
  22. 'apikey': api_key,
  23. 'type': search_type
  24. }
  25. if indexer_ids:
  26. query_args['indexerIds'] = indexer_ids
  27. if search_categories:
  28. query_args['categories'] = search_categories
  29. params['url'] = base_url + urlencode(query_args)
  30. return params
  31. def response(resp):
  32. results = []
  33. json_data = loads(resp.text)
  34. for result in json_data:
  35. new_result = {
  36. 'title': result['title'],
  37. 'url': result['infoUrl'],
  38. 'template': 'torrent.html'
  39. }
  40. if 'files' in result:
  41. new_result['files'] = result['files']
  42. if 'size' in result:
  43. new_result['filesize'] = result['size']
  44. if 'seeders' in result:
  45. new_result['seed'] = result['seeders']
  46. if 'leechers' in result:
  47. new_result['leech'] = result['leechers']
  48. if 'downloadUrl' in result:
  49. new_result['torrentfile'] = result['downloadUrl']
  50. # magnet link *may* be in guid, but it may be also identical to infoUrl
  51. if 'guid' in result and isinstance(result['guid'], str) and result['guid'].startswith('magnet'):
  52. new_result['magnetlink'] = result['guid']
  53. results.append(new_result)
  54. return results