kickass.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 = ['videos', 'music', '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),
  31. pageno=params['pageno'])
  32. return params
  33. # get response from search-request
  34. def response(resp):
  35. results = []
  36. dom = html.fromstring(resp.text)
  37. search_res = dom.xpath('//table[@class="data"]//tr')
  38. # return empty array if nothing is found
  39. if not search_res:
  40. return []
  41. # parse results
  42. for result in search_res[1:]:
  43. link = result.xpath('.//a[@class="cellMainLink"]')[0]
  44. href = urljoin(url, link.attrib['href'])
  45. title = extract_text(link)
  46. content = extract_text(result.xpath(content_xpath))
  47. seed = extract_text(result.xpath('.//td[contains(@class, "green")]'))
  48. leech = extract_text(result.xpath('.//td[contains(@class, "red")]'))
  49. filesize_info = extract_text(result.xpath('.//td[contains(@class, "nobr")]'))
  50. files = extract_text(result.xpath('.//td[contains(@class, "center")][2]'))
  51. seed = convert_str_to_int(seed)
  52. leech = convert_str_to_int(leech)
  53. filesize, filesize_multiplier = filesize_info.split()
  54. filesize = get_torrent_size(filesize, filesize_multiplier)
  55. if files.isdigit():
  56. files = int(files)
  57. else:
  58. files = None
  59. magnetlink = result.xpath(magnet_xpath)[0].attrib['href']
  60. torrentfile = result.xpath(torrent_xpath)[0].attrib['href']
  61. torrentfileurl = quote(torrentfile, safe="%/:=&?~#+!$,;'@()*")
  62. # append result
  63. results.append({'url': href,
  64. 'title': title,
  65. 'content': content,
  66. 'seed': seed,
  67. 'leech': leech,
  68. 'filesize': filesize,
  69. 'files': files,
  70. 'magnetlink': magnetlink,
  71. 'torrentfile': torrentfileurl,
  72. 'template': 'torrent.html'})
  73. # return results sorted by seeder
  74. return sorted(results, key=itemgetter('seed'), reverse=True)