summaryrefslogtreecommitdiff
path: root/searx/engines/kickass.py
blob: 562b832794eeda8a8efd22c0b29eed24abe72af0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# SPDX-License-Identifier: AGPL-3.0-or-later
# lint: pylint
"""Kickass Torrent (Videos, Music, Files)"""

import random
from operator import itemgetter
from urllib.parse import quote

from lxml import html
from searx.utils import (
    eval_xpath,
    eval_xpath_getindex,
    eval_xpath_list,
    extract_text,
    get_torrent_size,
    int_or_zero,
)

about = {
    "website": 'https://kickasstorrents.to',
    "wikidata_id": 'Q17062285',
    "official_api_documentation": None,
    "use_official_api": False,
    "require_api_key": False,
    "results": 'HTML',
}

categories = ['files']
paging = True

# base_url can be overwritten by a list of URLs in the settings.yml
base_url = 'https://kickasstorrents.to'


def request(query, params):
    params['base_url'] = random.choice(base_url) if isinstance(base_url, list) else base_url
    params['url'] = params['base_url'] + f'/usearch/{quote(query)}/{params["pageno"]}/'

    return params


def response(resp):
    results = []
    dom = html.fromstring(resp.text)

    search_res = eval_xpath_list(dom, '//table[contains(@class, "data")]//tr', None)
    if search_res is None:
        return []

    for tag in search_res[1:]:
        result = {'template': 'torrent.html'}
        url = eval_xpath_getindex(tag, './/a[contains(@class, "cellMainLink")]/@href', 0, None)
        result['url'] = resp.search_params['base_url'] + url
        result['title'] = extract_text(eval_xpath(tag, './/a[contains(@class, "cellMainLink")]'))
        result['content'] = extract_text(eval_xpath(tag, './/span[@class="font11px lightgrey block"]'))
        result['seed'] = int_or_zero(extract_text(eval_xpath(tag, './/td[contains(@class, "green")]')))
        result['leech'] = int_or_zero(extract_text(eval_xpath(tag, './/td[contains(@class, "red")]')))
        result['filesize'] = get_torrent_size(*extract_text(eval_xpath(tag, './/td[contains(@class, "nobr")]')).split())

        results.append(result)

    # results sorted by seeder count
    return sorted(results, key=itemgetter('seed'), reverse=True)