kickass.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. """
  2. Kickass Torrent (Videos, Music, Files)
  3. @website https://kickass.so
  4. @provide-api no (nothing found)
  5. @using-api no
  6. @results HTML (using search portal)
  7. @stable yes (HTML can change)
  8. @parse url, title, content, seed, leech, magnetlink
  9. """
  10. from urlparse import urljoin
  11. from cgi import escape
  12. from urllib import quote
  13. from lxml import html
  14. from operator import itemgetter
  15. from searx.engines.xpath import extract_text
  16. # engine dependent config
  17. categories = ['videos', 'music', 'files']
  18. paging = True
  19. # search-url
  20. url = 'https://kickass.to/'
  21. search_url = url + 'search/{search_term}/{pageno}/'
  22. # specific xpath variables
  23. magnet_xpath = './/a[@title="Torrent magnet link"]'
  24. torrent_xpath = './/a[@title="Download torrent file"]'
  25. content_xpath = './/span[@class="font11px lightgrey block"]'
  26. # do search-request
  27. def request(query, params):
  28. params['url'] = search_url.format(search_term=quote(query),
  29. pageno=params['pageno'])
  30. return params
  31. # get response from search-request
  32. def response(resp):
  33. results = []
  34. dom = html.fromstring(resp.text)
  35. search_res = dom.xpath('//table[@class="data"]//tr')
  36. # return empty array if nothing is found
  37. if not search_res:
  38. return []
  39. # parse results
  40. for result in search_res[1:]:
  41. link = result.xpath('.//a[@class="cellMainLink"]')[0]
  42. href = urljoin(url, link.attrib['href'])
  43. title = extract_text(link)
  44. content = escape(extract_text(result.xpath(content_xpath)))
  45. seed = result.xpath('.//td[contains(@class, "green")]/text()')[0]
  46. leech = result.xpath('.//td[contains(@class, "red")]/text()')[0]
  47. filesize = result.xpath('.//td[contains(@class, "nobr")]/text()')[0]
  48. filesize_multiplier = result.xpath('.//td[contains(@class, "nobr")]//span/text()')[0]
  49. files = result.xpath('.//td[contains(@class, "center")][2]/text()')[0]
  50. # convert seed to int if possible
  51. if seed.isdigit():
  52. seed = int(seed)
  53. else:
  54. seed = 0
  55. # convert leech to int if possible
  56. if leech.isdigit():
  57. leech = int(leech)
  58. else:
  59. leech = 0
  60. # convert filesize to byte if possible
  61. try:
  62. filesize = float(filesize)
  63. # convert filesize to byte
  64. if filesize_multiplier == 'TB':
  65. filesize = int(filesize * 1024 * 1024 * 1024 * 1024)
  66. elif filesize_multiplier == 'GB':
  67. filesize = int(filesize * 1024 * 1024 * 1024)
  68. elif filesize_multiplier == 'MB':
  69. filesize = int(filesize * 1024 * 1024)
  70. elif filesize_multiplier == 'KB':
  71. filesize = int(filesize * 1024)
  72. except:
  73. filesize = None
  74. # convert files to int if possible
  75. if files.isdigit():
  76. files = int(files)
  77. else:
  78. files = None
  79. magnetlink = result.xpath(magnet_xpath)[0].attrib['href']
  80. torrentfile = result.xpath(torrent_xpath)[0].attrib['href']
  81. torrentfileurl = quote(torrentfile, safe="%/:=&?~#+!$,;'@()*")
  82. # append result
  83. results.append({'url': href,
  84. 'title': title,
  85. 'content': content,
  86. 'seed': seed,
  87. 'leech': leech,
  88. 'filesize': filesize,
  89. 'files': files,
  90. 'magnetlink': magnetlink,
  91. 'torrentfile': torrentfileurl,
  92. 'template': 'torrent.html'})
  93. # return results sorted by seeder
  94. return sorted(results, key=itemgetter('seed'), reverse=True)