torznab.py 4.1 KB

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