kickass.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Kickass Torrent (Videos, Music, Files)"""
  3. import random
  4. from operator import itemgetter
  5. from urllib.parse import quote
  6. from lxml import html
  7. from searx.utils import (
  8. eval_xpath,
  9. eval_xpath_getindex,
  10. eval_xpath_list,
  11. extract_text,
  12. int_or_zero,
  13. )
  14. about = {
  15. "website": 'https://kickasstorrents.to',
  16. "wikidata_id": 'Q17062285',
  17. "official_api_documentation": None,
  18. "use_official_api": False,
  19. "require_api_key": False,
  20. "results": 'HTML',
  21. }
  22. categories = ['files']
  23. paging = True
  24. # base_url can be overwritten by a list of URLs in the settings.yml
  25. base_url = 'https://kickasstorrents.to'
  26. def request(query, params):
  27. params['base_url'] = random.choice(base_url) if isinstance(base_url, list) else base_url
  28. params['url'] = params['base_url'] + f'/usearch/{quote(query)}/{params["pageno"]}/'
  29. return params
  30. def response(resp):
  31. results = []
  32. dom = html.fromstring(resp.text)
  33. search_res = eval_xpath_list(dom, '//table[contains(@class, "data")]//tr[descendant::a]', None)
  34. if search_res is None:
  35. return []
  36. for tag in search_res[1:]:
  37. result = {'template': 'torrent.html'}
  38. url = eval_xpath_getindex(tag, './/a[contains(@class, "cellMainLink")]/@href', 0, None)
  39. result['url'] = resp.search_params['base_url'] + url
  40. result['title'] = extract_text(eval_xpath(tag, './/a[contains(@class, "cellMainLink")]'))
  41. result['content'] = extract_text(eval_xpath(tag, './/span[@class="font11px lightgrey block"]'))
  42. result['seed'] = int_or_zero(extract_text(eval_xpath(tag, './/td[contains(@class, "green")]')))
  43. result['leech'] = int_or_zero(extract_text(eval_xpath(tag, './/td[contains(@class, "red")]')))
  44. result['filesize'] = extract_text(eval_xpath(tag, './/td[contains(@class, "nobr")]'))
  45. results.append(result)
  46. # results sorted by seeder count
  47. return sorted(results, key=itemgetter('seed'), reverse=True)