summaryrefslogtreecommitdiff
path: root/searx/engines/sogou_images.py
blob: fec3ac12ca92f6a40d23c44fc09f99cb18bfa092 (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
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Sogou-Images: A search engine for retrieving images from Sogou."""

import json
import re
from urllib.parse import urlencode

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

# engine dependent config
categories = ["images"]
paging = True

base_url = "https://pic.sogou.com"


def request(query, params):
    query_params = {
        "query": query,
        "start": (params["pageno"] - 1) * 48,
    }

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


def response(resp):
    results = []
    match = re.search(r'window\.__INITIAL_STATE__\s*=\s*({.*?});', resp.text, re.S)
    if not match:
        return results

    data = json.loads(match.group(1))
    if "searchList" in data and "searchList" in data["searchList"]:
        for item in data["searchList"]["searchList"]:
            results.append(
                {
                    "template": "images.html",
                    "url": item.get("url", ""),
                    "thumbnail_src": item.get("picUrl", ""),
                    "img_src": item.get("picUrl", ""),
                    "content": item.get("content_major", ""),
                    "title": item.get("title", ""),
                    "source": item.get("ch_site_name", ""),
                }
            )

    return results