yggtorrent.py 3.4 KB

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