nyaa.py 3.2 KB

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