nyaa.py 3.1 KB

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