summaryrefslogtreecommitdiff
path: root/searx/engines/sogou.py
blob: 026f3dea78f3d771ad900165e7aafb84d732dcb3 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Sogou search engine for searxng"""

import re
from datetime import datetime
from urllib.parse import urlencode
from lxml import html

from searx.exceptions import SearxEngineCaptchaException
from searx.utils import extract_text

# Metadata
about = {
    "website": "https://www.sogou.com/",
    "wikidata_id": "Q7554565",
    "use_official_api": False,
    "require_api_key": False,
    "results": "HTML",
}
language = "zh"

# Engine Configuration
categories = ["general"]
paging = True
time_range_support = True

time_range_dict = {
    "day": "inttime_day",
    "week": "inttime_week",
    "month": "inttime_month",
    "year": "inttime_year",
}

# Base URL
base_url = "https://www.sogou.com"


def request(query, params):
    query_params = {
        "query": query,
        "page": params["pageno"],
    }

    if time_range_dict.get(params["time_range"]):
        query_params["s_from"] = time_range_dict.get(params["time_range"])
        query_params["tsn"] = 1

    params["allow_redirects"] = False
    params["url"] = f"{base_url}/web?{urlencode(query_params)}"
    return params


def response(resp):
    if (
        resp.status_code == 302
        and resp.next_request is not None
        and str(resp.next_request.url).startswith("http://www.sogou.com/antispider")
    ):
        raise SearxEngineCaptchaException()

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

    # pylint: disable=line-too-long
    for item in dom.xpath(
        '//div[contains(@class, "rb")] | //div[contains(@class, "vrwrap") and not(.//div[contains(@class, "special-wrap")])]'
    ):
        item_html = html.tostring(item, encoding="unicode")

        if item.xpath('.//h3[@class="pt"]/a'):
            result = _parse_results(item, item_html)
        elif item.xpath('.//h3[contains(@class, "vr-title")]/a'):
            result = _parse_results_with_image(item, item_html)
        else:
            continue

        if result["title"] and result["url"]:
            results.append(result)

    return results


def _extract_url(url, item_html):
    if url and url.startswith("/link?url="):
        match = re.search(r'data-url="([^"]+)"', item_html)
        if match:
            return match.group(1)
        return f"{base_url}{url}"
    return url


def _parse_date(text):
    if text:
        text = text.strip().lstrip("-").strip()
        date_match = re.search(r"(\d{4}-\d{1,2}-\d{1,2})", text)
        if date_match:
            try:
                y, m, d = date_match.group(1).split("-")
                return datetime(year=int(y), month=int(m), day=int(d))
            except (ValueError, TypeError):
                pass
    return None


def _parse_results(item, item_html):
    title = extract_text(item.xpath('.//h3[@class="pt"]/a'))
    content = extract_text(item.xpath('.//div[@class="ft"]'))
    url = _extract_url(extract_text(item.xpath('.//h3[@class="pt"]/a/@href')), item_html)
    publishedDate = _parse_date(extract_text(item.xpath(".//cite")))
    return {
        "title": title,
        "url": url,
        "content": content,
        "publishedDate": publishedDate,
    }


def _parse_results_with_image(item, item_html):
    title = extract_text(item.xpath('.//h3[contains(@class, "vr-title")]/a'))
    content = extract_text(item.xpath('.//div[contains(@class, "attribute-centent")]'))
    if not content:
        content = extract_text(item.xpath('.//div[contains(@class, "fz-mid space-txt")]'))
    url = _extract_url(extract_text(item.xpath('.//h3[contains(@class, "vr-title")]/a/@href')), item_html)
    publishedDate = _parse_date(extract_text(item.xpath('.//span[@class="cite-date"]')))

    thumbnail = None
    try:
        thumbnail_src = extract_text(item.xpath('.//div[contains(@class, "img-layout")]//img/@src'))
        if thumbnail_src:
            thumbnail = thumbnail_src.replace("http://", "https://")
    except (ValueError, TypeError):
        pass

    return {
        "title": title,
        "url": url,
        "content": content,
        "publishedDate": publishedDate,
        "thumbnail": thumbnail,
    }