torznab.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Torznab WebAPI
  3. A engine that implements the `torznab WebAPI`_.
  4. .. _torznab WebAPI: https://torznab.github.io/spec-1.3-draft/torznab
  5. """
  6. from urllib.parse import quote
  7. from lxml import etree
  8. from datetime import datetime
  9. from searx.exceptions import SearxEngineAPIException
  10. # about
  11. about = {
  12. "website": None,
  13. "wikidata_id": None,
  14. "official_api_documentation": "https://torznab.github.io/spec-1.3-draft/torznab/Specification-v1.3.html#torznab-api-specification",
  15. "use_official_api": True,
  16. "require_api_key": False,
  17. "results": 'XML',
  18. }
  19. categories = ['files']
  20. paging = False
  21. time_range_support = False
  22. # defined in settings.yml
  23. # example (Jackett): "http://localhost:9117/api/v2.0/indexers/all/results/torznab"
  24. base_url = ''
  25. api_key = ''
  26. # https://newznab.readthedocs.io/en/latest/misc/api/#predefined-categories
  27. torznab_categories = []
  28. def request(query, params):
  29. if len(base_url) < 1:
  30. raise SearxEngineAPIException('missing torznab base_url')
  31. search_url = base_url + '?t=search&q={search_query}'
  32. if len(api_key) > 0:
  33. search_url += '&apikey={api_key}'
  34. if len(torznab_categories) > 0:
  35. search_url += '&cat={torznab_categories}'
  36. params['url'] = search_url.format(
  37. search_query=quote(query),
  38. api_key=api_key,
  39. torznab_categories=",".join(torznab_categories)
  40. )
  41. return params
  42. def response(resp):
  43. results = []
  44. search_results = etree.XML(resp.content)
  45. # handle errors
  46. # https://newznab.readthedocs.io/en/latest/misc/api/#newznab-error-codes
  47. if search_results.tag == "error":
  48. raise SearxEngineAPIException(search_results.get("description"))
  49. for item in search_results[0].iterfind('item'):
  50. result = {'template': 'torrent.html'}
  51. enclosure = item.find('enclosure')
  52. result["filesize"] = int(enclosure.get('length'))
  53. link = get_property(item, 'link')
  54. guid = get_property(item, 'guid')
  55. comments = get_property(item, 'comments')
  56. # define url
  57. result["url"] = enclosure.get('url')
  58. if comments is not None and comments.startswith('http'):
  59. result["url"] = comments
  60. elif guid is not None and guid.startswith('http'):
  61. result["url"] = guid
  62. # define torrent file url
  63. result["torrentfile"] = None
  64. if enclosure.get('url').startswith("http"):
  65. result["torrentfile"] = enclosure.get('url')
  66. elif link is not None and link.startswith('http'):
  67. result["torrentfile"] = link
  68. # define magnet link
  69. result["magnetlink"] = get_torznab_attr(item, 'magneturl')
  70. if result["magnetlink"] is None:
  71. if enclosure.get('url').startswith("magnet"):
  72. result["magnetlink"] = enclosure.get('url')
  73. elif link is not None and link.startswith('magnet'):
  74. result["magnetlink"] = link
  75. result["title"] = get_property(item, 'title')
  76. result["files"] = get_property(item, 'files')
  77. result["publishedDate"] = None
  78. try:
  79. result["publishedDate"] = datetime.strptime(
  80. get_property(item, 'pubDate'), '%a, %d %b %Y %H:%M:%S %z')
  81. except (ValueError, TypeError) as e:
  82. pass
  83. result["seed"] = get_torznab_attr(item, 'seeders')
  84. # define leech
  85. result["leech"] = get_torznab_attr(item, 'leechers')
  86. if result["leech"] is None and result["seed"] is not None:
  87. peers = get_torznab_attr(item, 'peers')
  88. if peers is not None:
  89. result["leech"] = int(peers) - int(result["seed"])
  90. results.append(result)
  91. return results
  92. def get_property(item, property_name):
  93. property_element = item.find(property_name)
  94. if property_element is not None:
  95. return property_element.text
  96. return None
  97. def get_torznab_attr(item, attr_name):
  98. element = item.find(
  99. './/torznab:attr[@name="{attr_name}"]'.format(attr_name=attr_name),
  100. {
  101. 'torznab': 'http://torznab.com/schemas/2015/feed'
  102. }
  103. )
  104. if element is not None:
  105. return element.get("value")
  106. return None