torrentz.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. """
  2. Torrentz2.is (BitTorrent meta-search engine)
  3. @website https://torrentz2.is/
  4. @provide-api no
  5. @using-api no
  6. @results HTML
  7. @stable no (HTML can change, although unlikely,
  8. see https://torrentz.is/torrentz.btsearch)
  9. @parse url, title, publishedDate, seed, leech, filesize, magnetlink
  10. """
  11. import re
  12. from urllib.parse import urlencode
  13. from lxml import html
  14. from datetime import datetime
  15. from searx.utils import extract_text, get_torrent_size
  16. # engine dependent config
  17. categories = ['files', 'videos', 'music']
  18. paging = True
  19. # search-url
  20. # https://torrentz2.is/search?f=EXAMPLE&p=6
  21. base_url = 'https://torrentz2.is/'
  22. search_url = base_url + 'search?{query}'
  23. # do search-request
  24. def request(query, params):
  25. page = params['pageno'] - 1
  26. query = urlencode({'f': query, 'p': page})
  27. params['url'] = search_url.format(query=query)
  28. return params
  29. # get response from search-request
  30. def response(resp):
  31. results = []
  32. dom = html.fromstring(resp.text)
  33. for result in dom.xpath('//div[@class="results"]/dl'):
  34. name_cell = result.xpath('./dt')[0]
  35. title = extract_text(name_cell)
  36. # skip rows that do not contain a link to a torrent
  37. links = name_cell.xpath('./a')
  38. if len(links) != 1:
  39. continue
  40. # extract url and remove a slash in the beginning
  41. link = links[0].attrib.get('href').lstrip('/')
  42. seed = 0
  43. leech = 0
  44. try:
  45. seed = int(result.xpath('./dd/span[4]/text()')[0].replace(',', ''))
  46. leech = int(result.xpath('./dd/span[5]/text()')[0].replace(',', ''))
  47. except:
  48. pass
  49. params = {
  50. 'url': base_url + link,
  51. 'title': title,
  52. 'seed': seed,
  53. 'leech': leech,
  54. 'template': 'torrent.html'
  55. }
  56. # let's try to calculate the torrent size
  57. try:
  58. filesize_info = result.xpath('./dd/span[3]/text()')[0]
  59. filesize, filesize_multiplier = filesize_info.split()
  60. filesize = get_torrent_size(filesize, filesize_multiplier)
  61. params['filesize'] = filesize
  62. except:
  63. pass
  64. # does our link contain a valid SHA1 sum?
  65. if re.compile('[0-9a-fA-F]{40}').match(link):
  66. # add a magnet link to the result
  67. params['magnetlink'] = 'magnet:?xt=urn:btih:' + link
  68. # extract and convert creation date
  69. try:
  70. date_ts = result.xpath('./dd/span[2]')[0].attrib.get('title')
  71. date = datetime.fromtimestamp(float(date_ts))
  72. params['publishedDate'] = date
  73. except:
  74. pass
  75. results.append(params)
  76. return results