205 params[
'headers'][
'Accept-Encoding'] =
'gzip, deflate'
211 args[
'spellcheck'] =
'1'
213 if brave_category
in (
'search',
'goggles'):
214 if params.get(
'pageno', 1) - 1:
215 args[
'offset'] = params.get(
'pageno', 1) - 1
216 if time_range_map.get(params[
'time_range']):
217 args[
'tf'] = time_range_map.get(params[
'time_range'])
219 if brave_category ==
'goggles':
220 args[
'goggles_id'] = Goggles
222 params[
"url"] = f
"{base_url}{brave_category}?{urlencode(args)}"
226 params[
'cookies'][
'safesearch'] = safesearch_map.get(params[
'safesearch'],
'off')
228 params[
'cookies'][
'useLocation'] =
'0'
229 params[
'cookies'][
'summarizer'] =
'0'
231 engine_region = traits.get_region(params[
'searxng_locale'],
'all')
232 params[
'cookies'][
'country'] = engine_region.split(
'-')[-1].lower()
234 ui_lang = locales.get_engine_locale(params[
'searxng_locale'], traits.custom[
"ui_lang"],
'en-us')
235 params[
'cookies'][
'ui_lang'] = ui_lang
237 logger.debug(
"cookies %s", params[
'cookies'])
278 dom = html.fromstring(resp.text)
280 answer_tag = eval_xpath_getindex(dom,
'//div[@class="answer"]', 0, default=
None)
282 url = eval_xpath_getindex(dom,
'//div[@id="featured_snippet"]/a[@class="result-header"]/@href', 0, default=
None)
283 result_list.append({
'answer': extract_text(answer_tag),
'url': url})
286 xpath_results =
'//div[contains(@class, "snippet ")]'
288 for result
in eval_xpath_list(dom, xpath_results):
290 url = eval_xpath_getindex(result,
'.//a[contains(@class, "h")]/@href', 0, default=
None)
291 title_tag = eval_xpath_getindex(
292 result,
'.//a[contains(@class, "h")]//div[contains(@class, "title")]', 0, default=
None
294 if url
is None or title_tag
is None or not urlparse(url).netloc:
297 content_tag = eval_xpath_getindex(result,
'.//div[contains(@class, "snippet-description")]', 0, default=
'')
298 pub_date_raw = eval_xpath(result,
'substring-before(.//div[contains(@class, "snippet-description")], "-")')
299 img_src = eval_xpath_getindex(result,
'.//img[contains(@class, "thumb")]/@src', 0, default=
'')
303 'title': extract_text(title_tag),
304 'content': extract_text(content_tag),
309 video_tag = eval_xpath_getindex(
310 result,
'.//div[contains(@class, "video-snippet") and @data-macro="video"]', 0, default=
None
312 if video_tag
is not None:
319 item[
'iframe_src'] = iframe_src
320 item[
'template'] =
'videos.html'
321 item[
'thumbnail'] = eval_xpath_getindex(video_tag,
'.//img/@src', 0, default=
'')
322 pub_date_raw = extract_text(
323 eval_xpath(video_tag,
'.//div[contains(@class, "snippet-attributes")]/div/text()')
327 item[
'img_src'] = eval_xpath_getindex(video_tag,
'.//img/@src', 0, default=
'')
329 result_list.append(item)
408 """Fetch :ref:`languages <brave languages>` and :ref:`regions <brave
409 regions>` from Brave."""
413 import babel.languages
417 engine_traits.custom[
"ui_lang"] = {}
420 'Accept-Encoding':
'gzip, deflate',
422 lang_map = {
'no':
'nb'}
426 resp = get(
'https://search.brave.com/settings', headers=headers)
429 print(
"ERROR: response from Brave is not OK.")
430 dom = html.fromstring(resp.text)
432 for option
in dom.xpath(
'//div[@id="language-select"]//option'):
434 ui_lang = option.get(
'value')
437 sxng_tag = region_tag(babel.Locale.parse(ui_lang, sep=
'-'))
439 sxng_tag = language_tag(babel.Locale.parse(ui_lang))
441 except babel.UnknownLocaleError:
442 print(
"ERROR: can't determine babel locale of Brave's (UI) language %s" % ui_lang)
445 conflict = engine_traits.custom[
"ui_lang"].get(sxng_tag)
447 if conflict != ui_lang:
448 print(
"CONFLICT: babel %s --> %s, %s" % (sxng_tag, conflict, ui_lang))
450 engine_traits.custom[
"ui_lang"][sxng_tag] = ui_lang
454 resp = get(
'https://cdn.search.brave.com/serp/v2/_app/immutable/chunks/parameters.734c106a.js', headers=headers)
457 print(
"ERROR: response from Brave is not OK.")
459 country_js = resp.text[resp.text.index(
"options:{all") + len(
'options:') :]
460 country_js = country_js[: country_js.index(
"},k={default")]
461 country_tags = js_variable_to_python(country_js)
463 for k, v
in country_tags.items():
465 engine_traits.all_locale =
'all'
467 country_tag = v[
'value']
470 for lang_tag
in babel.languages.get_official_languages(country_tag, de_facto=
True):
471 lang_tag = lang_map.get(lang_tag, lang_tag)
472 sxng_tag = region_tag(babel.Locale.parse(
'%s_%s' % (lang_tag, country_tag.upper())))
475 conflict = engine_traits.regions.get(sxng_tag)
477 if conflict != country_tag:
478 print(
"CONFLICT: babel %s --> %s, %s" % (sxng_tag, conflict, country_tag))
480 engine_traits.regions[sxng_tag] = country_tag