92def get_google_info(params, eng_traits):
93 """Composing various (language) properties for the google engines (:ref:`google
94 API`).
95
96 This function is called by the various google engines (:ref:`google web
97 engine`, :ref:`google images engine`, :ref:`google news engine` and
98 :ref:`google videos engine`).
99
100 :param dict param: Request parameters of the engine. At least
101 a ``searxng_locale`` key should be in the dictionary.
102
103 :param eng_traits: Engine's traits fetched from google preferences
104 (:py:obj:`searx.enginelib.traits.EngineTraits`)
105
106 :rtype: dict
107 :returns:
108 Py-Dictionary with the key/value pairs:
109
110 language:
111 The language code that is used by google (e.g. ``lang_en`` or
112 ``lang_zh-TW``)
113
114 country:
115 The country code that is used by google (e.g. ``US`` or ``TW``)
116
117 locale:
118 A instance of :py:obj:`babel.core.Locale` build from the
119 ``searxng_locale`` value.
120
121 subdomain:
122 Google subdomain :py:obj:`google_domains` that fits to the country
123 code.
124
125 params:
126 Py-Dictionary with additional request arguments (can be passed to
127 :py:func:`urllib.parse.urlencode`).
128
129 - ``hl`` parameter: specifies the interface language of user interface.
130 - ``lr`` parameter: restricts search results to documents written in
131 a particular language.
132 - ``cr`` parameter: restricts search results to documents
133 originating in a particular country.
134 - ``ie`` parameter: sets the character encoding scheme that should
135 be used to interpret the query string ('utf8').
136 - ``oe`` parameter: sets the character encoding scheme that should
137 be used to decode the XML result ('utf8').
138
139 headers:
140 Py-Dictionary with additional HTTP headers (can be passed to
141 request's headers)
142
143 - ``Accept: '*/*``
144
145 """
146
147 ret_val = {
148 'language': None,
149 'country': None,
150 'subdomain': None,
151 'params': {},
152 'headers': {},
153 'cookies': {},
154 'locale': None,
155 }
156
157 sxng_locale = params.get('searxng_locale', 'all')
158 try:
159 locale = babel.Locale.parse(sxng_locale, sep='-')
160 except babel.core.UnknownLocaleError:
161 locale = None
162
163 eng_lang = eng_traits.get_language(sxng_locale, 'lang_en')
164 lang_code = eng_lang.split('_')[-1]
165 country = eng_traits.get_region(sxng_locale, eng_traits.all_locale)
166
167
168
169
170
171
172
173
174
175 ret_val['language'] = eng_lang
176 ret_val['country'] = country
177 ret_val['locale'] = locale
178 ret_val['subdomain'] = eng_traits.custom['supported_domains'].get(country.upper(), 'www.google.com')
179
180
181
182
183
184
185
186
187
188
189
190 ret_val['params']['hl'] = f'{lang_code}-{country}'
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206 ret_val['params']['lr'] = eng_lang
207 if sxng_locale == 'all':
208 ret_val['params']['lr'] = ''
209
210
211
212
213
214
215
216
217 ret_val['params']['cr'] = ''
218 if len(sxng_locale.split('-')) > 1:
219 ret_val['params']['cr'] = 'country' + country
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240 ret_val['params']['ie'] = 'utf8'
241
242
243
244
245
246
247 ret_val['params']['oe'] = 'utf8'
248
249
250
251
252
253
254
255
256
257
258
259
260 ret_val['headers']['Accept'] = '*/*'
261
262
263
264
265
266 ret_val['cookies']['CONSENT'] = "YES+"
267
268 return ret_val
269
270