nyaa.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Nyaa.si (Anime Bittorrent tracker)
  3. """
  4. from urllib.parse import urlencode
  5. from lxml import html
  6. from searx.utils import (
  7. eval_xpath_getindex,
  8. extract_text,
  9. int_or_zero,
  10. )
  11. # about
  12. about = {
  13. "website": 'https://nyaa.si/',
  14. "wikidata_id": None,
  15. "official_api_documentation": None,
  16. "use_official_api": False,
  17. "require_api_key": False,
  18. "results": 'HTML',
  19. }
  20. # engine dependent config
  21. categories = ['files']
  22. paging = True
  23. # search-url
  24. base_url = 'https://nyaa.si/'
  25. # xpath queries
  26. xpath_results = '//table[contains(@class, "torrent-list")]//tr[not(th)]'
  27. xpath_category = './/td[1]/a[1]'
  28. xpath_title = './/td[2]/a[last()]'
  29. xpath_torrent_links = './/td[3]/a'
  30. xpath_filesize = './/td[4]/text()'
  31. xpath_seeds = './/td[6]/text()'
  32. xpath_leeches = './/td[7]/text()'
  33. xpath_downloads = './/td[8]/text()'
  34. # do search-request
  35. def request(query, params):
  36. args = urlencode(
  37. {
  38. 'q': query,
  39. 'p': params['pageno'],
  40. }
  41. )
  42. params['url'] = base_url + '?' + args #
  43. logger.debug("query_url --> %s", params['url'])
  44. return params
  45. # get response from search-request
  46. def response(resp):
  47. results = []
  48. dom = html.fromstring(resp.text)
  49. for result in dom.xpath(xpath_results):
  50. # defaults
  51. filesize = 0
  52. magnet_link = ""
  53. torrent_link = ""
  54. # category in which our torrent belongs
  55. category = eval_xpath_getindex(result, xpath_category, 0, '')
  56. if category:
  57. category = category.attrib.get('title')
  58. # torrent title
  59. page_a = result.xpath(xpath_title)[0]
  60. title = extract_text(page_a)
  61. # link to the page
  62. href = base_url + page_a.attrib.get('href')
  63. for link in result.xpath(xpath_torrent_links):
  64. url = link.attrib.get('href')
  65. if 'magnet' in url:
  66. # link to the magnet
  67. magnet_link = url
  68. else:
  69. # link to the torrent file
  70. torrent_link = url
  71. # seed count
  72. seed = int_or_zero(result.xpath(xpath_seeds))
  73. # leech count
  74. leech = int_or_zero(result.xpath(xpath_leeches))
  75. # torrent downloads count
  76. downloads = int_or_zero(result.xpath(xpath_downloads))
  77. # let's try to calculate the torrent size
  78. filesize = eval_xpath_getindex(result, xpath_filesize, 0, '')
  79. # content string contains all information not included into template
  80. content = 'Category: "{category}". Downloaded {downloads} times.'
  81. content = content.format(category=category, downloads=downloads)
  82. results.append(
  83. {
  84. 'url': href,
  85. 'title': title,
  86. 'content': content,
  87. 'seed': seed,
  88. 'leech': leech,
  89. 'filesize': filesize,
  90. 'torrentfile': torrent_link,
  91. 'magnetlink': magnet_link,
  92. 'template': 'torrent.html',
  93. }
  94. )
  95. return results