.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
google_images.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""This is the implementation of the Google Images engine using the internal
3Google API used by the Google Go Android app.
4
5This internal API offer results in
6
7- JSON (``_fmt:json``)
8- Protobuf_ (``_fmt:pb``)
9- Protobuf_ compressed? (``_fmt:pc``)
10- HTML (``_fmt:html``)
11- Protobuf_ encoded in JSON (``_fmt:jspb``).
12
13.. _Protobuf: https://en.wikipedia.org/wiki/Protocol_Buffers
14"""
15
16from typing import TYPE_CHECKING
17
18from urllib.parse import urlencode
19from json import loads
20
21from searx.engines.google import fetch_traits # pylint: disable=unused-import
22from searx.engines.google import (
23 get_google_info,
24 time_range_dict,
25 detect_google_sorry,
26)
27
28if TYPE_CHECKING:
29 import logging
30 from searx.enginelib.traits import EngineTraits
31
32 logger: logging.Logger
33 traits: EngineTraits
34
35
36# about
37about = {
38 "website": 'https://images.google.com',
39 "wikidata_id": 'Q521550',
40 "official_api_documentation": 'https://developers.google.com/custom-search',
41 "use_official_api": False,
42 "require_api_key": False,
43 "results": 'JSON',
44}
45
46# engine dependent config
47categories = ['images', 'web']
48paging = True
49max_page = 50
50time_range_support = True
51safesearch = True
52send_accept_language_header = True
53
54filter_mapping = {0: 'images', 1: 'active', 2: 'active'}
55
56
57def request(query, params):
58 """Google-Image search request"""
59
60 google_info = get_google_info(params, traits)
61
62 query_url = (
63 'https://'
64 + google_info['subdomain']
65 + '/search'
66 + "?"
67 + urlencode(
68 {
69 'q': query,
70 'tbm': "isch",
71 **google_info['params'],
72 'asearch': 'isch',
73 'async': '_fmt:json,p:1,ijn:' + str(params['pageno']),
74 }
75 )
76 )
77
78 if params['time_range'] in time_range_dict:
79 query_url += '&' + urlencode({'tbs': 'qdr:' + time_range_dict[params['time_range']]})
80 if params['safesearch']:
81 query_url += '&' + urlencode({'safe': filter_mapping[params['safesearch']]})
82 params['url'] = query_url
83
84 params['cookies'] = google_info['cookies']
85 params['headers'].update(google_info['headers'])
86 return params
87
88
89def response(resp):
90 """Get response from google's search request"""
91 results = []
92
93 detect_google_sorry(resp)
94
95 json_start = resp.text.find('{"ischj":')
96 json_data = loads(resp.text[json_start:])
97
98 for item in json_data["ischj"].get("metadata", []):
99
100 result_item = {
101 'url': item["result"]["referrer_url"],
102 'title': item["result"]["page_title"],
103 'content': item["text_in_grid"]["snippet"],
104 'source': item["result"]["site_title"],
105 'resolution': f'{item["original_image"]["width"]} x {item["original_image"]["height"]}',
106 'img_src': item["original_image"]["url"],
107 'thumbnail_src': item["thumbnail"]["url"],
108 'template': 'images.html',
109 }
110
111 author = item["result"].get('iptc', {}).get('creator')
112 if author:
113 result_item['author'] = ', '.join(author)
114
115 copyright_notice = item["result"].get('iptc', {}).get('copyright_notice')
116 if copyright_notice:
117 result_item['source'] += ' | ' + copyright_notice
118
119 freshness_date = item["result"].get("freshness_date")
120 if freshness_date:
121 result_item['source'] += ' | ' + freshness_date
122
123 file_size = item.get('gsa', {}).get('file_size')
124 if file_size:
125 result_item['source'] += ' (%s)' % file_size
126
127 results.append(result_item)
128
129 return results