kickass.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. from searx.utils import get_torrent_size, convert_str_to_int
  17. # engine dependent config
  18. categories = ['videos', 'music', 'files']
  19. paging = True
  20. # search-url
  21. url = 'https://kickass.cd/'
  22. search_url = url + 'search/{search_term}/{pageno}/'
  23. # specific xpath variables
  24. magnet_xpath = './/a[@title="Torrent magnet link"]'
  25. torrent_xpath = './/a[@title="Download torrent file"]'
  26. content_xpath = './/span[@class="font11px lightgrey block"]'
  27. # do search-request
  28. def request(query, params):
  29. params['url'] = search_url.format(search_term=quote(query),
  30. pageno=params['pageno'])
  31. return params
  32. # get response from search-request
  33. def response(resp):
  34. results = []
  35. dom = html.fromstring(resp.text)
  36. search_res = dom.xpath('//table[@class="data"]//tr')
  37. # return empty array if nothing is found
  38. if not search_res:
  39. return []
  40. # parse results
  41. for result in search_res[1:]:
  42. link = result.xpath('.//a[@class="cellMainLink"]')[0]
  43. href = urljoin(url, link.attrib['href'])
  44. title = extract_text(link)
  45. content = escape(extract_text(result.xpath(content_xpath)))
  46. seed = extract_text(result.xpath('.//td[contains(@class, "green")]'))
  47. leech = extract_text(result.xpath('.//td[contains(@class, "red")]'))
  48. filesize_info = extract_text(result.xpath('.//td[contains(@class, "nobr")]'))
  49. files = extract_text(result.xpath('.//td[contains(@class, "center")][2]'))
  50. seed = convert_str_to_int(seed)
  51. leech = convert_str_to_int(leech)
  52. filesize, filesize_multiplier = filesize_info.split()
  53. filesize = get_torrent_size(filesize, filesize_multiplier)
  54. if files.isdigit():
  55. files = int(files)
  56. else:
  57. files = None
  58. magnetlink = result.xpath(magnet_xpath)[0].attrib['href']
  59. torrentfile = result.xpath(torrent_xpath)[0].attrib['href']
  60. torrentfileurl = quote(torrentfile, safe="%/:=&?~#+!$,;'@()*")
  61. # append result
  62. results.append({'url': href,
  63. 'title': title,
  64. 'content': content,
  65. 'seed': seed,
  66. 'leech': leech,
  67. 'filesize': filesize,
  68. 'files': files,
  69. 'magnetlink': magnetlink,
  70. 'torrentfile': torrentfileurl,
  71. 'template': 'torrent.html'})
  72. # return results sorted by seeder
  73. return sorted(results, key=itemgetter('seed'), reverse=True)