logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

nyaa.py (3202B)


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