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