kickass.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 urllib import quote
  12. from lxml import html
  13. from operator import itemgetter
  14. from searx.engines.xpath import extract_text
  15. from searx.utils import get_torrent_size, convert_str_to_int
  16. # engine dependent config
  17. categories = ['videos', 'music', 'files']
  18. paging = True
  19. # search-url
  20. url = 'https://kickass.cd/'
  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 = extract_text(result.xpath(content_xpath))
  45. seed = extract_text(result.xpath('.//td[contains(@class, "green")]'))
  46. leech = extract_text(result.xpath('.//td[contains(@class, "red")]'))
  47. filesize_info = extract_text(result.xpath('.//td[contains(@class, "nobr")]'))
  48. files = extract_text(result.xpath('.//td[contains(@class, "center")][2]'))
  49. seed = convert_str_to_int(seed)
  50. leech = convert_str_to_int(leech)
  51. filesize, filesize_multiplier = filesize_info.split()
  52. filesize = get_torrent_size(filesize, filesize_multiplier)
  53. if files.isdigit():
  54. files = int(files)
  55. else:
  56. files = None
  57. magnetlink = result.xpath(magnet_xpath)[0].attrib['href']
  58. torrentfile = result.xpath(torrent_xpath)[0].attrib['href']
  59. torrentfileurl = quote(torrentfile, safe="%/:=&?~#+!$,;'@()*")
  60. # append result
  61. results.append({'url': href,
  62. 'title': title,
  63. 'content': content,
  64. 'seed': seed,
  65. 'leech': leech,
  66. 'filesize': filesize,
  67. 'files': files,
  68. 'magnetlink': magnetlink,
  69. 'torrentfile': torrentfileurl,
  70. 'template': 'torrent.html'})
  71. # return results sorted by seeder
  72. return sorted(results, key=itemgetter('seed'), reverse=True)