kickass.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. Kickass Torrent (Videos, Music, Files)
  4. """
  5. from lxml import html
  6. from operator import itemgetter
  7. from urllib.parse import quote, urljoin
  8. from searx.utils import extract_text, get_torrent_size, convert_str_to_int
  9. # about
  10. about = {
  11. "website": 'https://kickass.so',
  12. "wikidata_id": 'Q17062285',
  13. "official_api_documentation": None,
  14. "use_official_api": False,
  15. "require_api_key": False,
  16. "results": 'HTML',
  17. }
  18. # engine dependent config
  19. categories = ['files']
  20. paging = True
  21. # search-url
  22. url = 'https://kickass.cd/'
  23. search_url = url + 'search/{search_term}/{pageno}/'
  24. # specific xpath variables
  25. magnet_xpath = './/a[@title="Torrent magnet link"]'
  26. torrent_xpath = './/a[@title="Download torrent file"]'
  27. content_xpath = './/span[@class="font11px lightgrey block"]'
  28. # do search-request
  29. def request(query, params):
  30. params['url'] = search_url.format(search_term=quote(query), 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 = 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(
  63. {
  64. 'url': href,
  65. 'title': title,
  66. 'content': content,
  67. 'seed': seed,
  68. 'leech': leech,
  69. 'filesize': filesize,
  70. 'files': files,
  71. 'magnetlink': magnetlink,
  72. 'torrentfile': torrentfileurl,
  73. 'template': 'torrent.html',
  74. }
  75. )
  76. # return results sorted by seeder
  77. return sorted(results, key=itemgetter('seed'), reverse=True)