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