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