yggtorrent.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # Yggtorrent (Videos, Music, Files)
  2. #
  3. # @website https://www2.yggtorrent.si
  4. # @provide-api no (nothing found)
  5. #
  6. # @using-api no
  7. # @results HTML (using search portal)
  8. # @stable no (HTML can change)
  9. # @parse url, title, seed, leech, publishedDate, filesize
  10. from lxml import html
  11. from operator import itemgetter
  12. from datetime import datetime
  13. from urllib.parse import quote
  14. from searx.engines.xpath import extract_text
  15. from searx.utils import get_torrent_size
  16. from searx.poolrequests import get as http_get
  17. # engine dependent config
  18. categories = ['videos', 'music', 'files']
  19. paging = True
  20. # search-url
  21. url = 'https://www2.yggtorrent.si/'
  22. search_url = url + 'engine/search?name={search_term}&do=search&page={pageno}&category={search_type}'
  23. # yggtorrent specific type-definitions
  24. search_types = {'files': 'all',
  25. 'music': '2139',
  26. 'videos': '2145'}
  27. cookies = dict()
  28. def init(engine_settings=None):
  29. global cookies
  30. # initial cookies
  31. resp = http_get(url, allow_redirects=False)
  32. if resp.ok:
  33. for r in resp.history:
  34. cookies.update(r.cookies)
  35. cookies.update(resp.cookies)
  36. # do search-request
  37. def request(query, params):
  38. search_type = search_types.get(params['category'], 'all')
  39. pageno = (params['pageno'] - 1) * 50
  40. params['url'] = search_url.format(search_term=quote(query),
  41. search_type=search_type,
  42. pageno=pageno)
  43. params['cookies'] = cookies
  44. return params
  45. # get response from search-request
  46. def response(resp):
  47. results = []
  48. dom = html.fromstring(resp.text)
  49. search_res = dom.xpath('//section[@id="#torrents"]/div/table/tbody/tr')
  50. # return empty array if nothing is found
  51. if not search_res:
  52. return []
  53. # parse results
  54. for result in search_res:
  55. link = result.xpath('.//a[@id="torrent_name"]')[0]
  56. href = link.attrib.get('href')
  57. title = extract_text(link)
  58. seed = result.xpath('.//td[8]/text()')[0]
  59. leech = result.xpath('.//td[9]/text()')[0]
  60. # convert seed to int if possible
  61. if seed.isdigit():
  62. seed = int(seed)
  63. else:
  64. seed = 0
  65. # convert leech to int if possible
  66. if leech.isdigit():
  67. leech = int(leech)
  68. else:
  69. leech = 0
  70. params = {'url': href,
  71. 'title': title,
  72. 'seed': seed,
  73. 'leech': leech,
  74. 'template': 'torrent.html'}
  75. # let's try to calculate the torrent size
  76. try:
  77. filesize_info = result.xpath('.//td[6]/text()')[0]
  78. filesize = filesize_info[:-2]
  79. filesize_multiplier = filesize_info[-2:].lower()
  80. multiplier_french_to_english = {
  81. 'to': 'TiB',
  82. 'go': 'GiB',
  83. 'mo': 'MiB',
  84. 'ko': 'KiB'
  85. }
  86. filesize = get_torrent_size(filesize, multiplier_french_to_english[filesize_multiplier])
  87. params['filesize'] = filesize
  88. except:
  89. pass
  90. # extract and convert creation date
  91. try:
  92. date_ts = result.xpath('.//td[5]/div/text()')[0]
  93. date = datetime.fromtimestamp(float(date_ts))
  94. params['publishedDate'] = date
  95. except:
  96. pass
  97. # append result
  98. results.append(params)
  99. # return results sorted by seeder
  100. return sorted(results, key=itemgetter('seed'), reverse=True)