206 params[
'headers'][
'Accept-Encoding'] =
'gzip, deflate'
212 args[
'spellcheck'] =
'1'
214 if brave_category
in (
'search',
'goggles'):
215 if params.get(
'pageno', 1) - 1:
216 args[
'offset'] = params.get(
'pageno', 1) - 1
217 if time_range_map.get(params[
'time_range']):
218 args[
'tf'] = time_range_map.get(params[
'time_range'])
220 if brave_category ==
'goggles':
221 args[
'goggles_id'] = Goggles
223 params[
"url"] = f
"{base_url}{brave_category}?{urlencode(args)}"
227 params[
'cookies'][
'safesearch'] = safesearch_map.get(params[
'safesearch'],
'off')
229 params[
'cookies'][
'useLocation'] =
'0'
230 params[
'cookies'][
'summarizer'] =
'0'
232 engine_region = traits.get_region(params[
'searxng_locale'],
'all')
233 params[
'cookies'][
'country'] = engine_region.split(
'-')[-1].lower()
235 ui_lang = locales.get_engine_locale(params[
'searxng_locale'], traits.custom[
"ui_lang"],
'en-us')
236 params[
'cookies'][
'ui_lang'] = ui_lang
238 logger.debug(
"cookies %s", params[
'cookies'])
275 dom = html.fromstring(resp.text)
277 answer_tag = eval_xpath_getindex(dom,
'//div[@class="answer"]', 0, default=
None)
279 url = eval_xpath_getindex(dom,
'//div[@id="featured_snippet"]/a[@class="result-header"]/@href', 0, default=
None)
280 result_list.append({
'answer': extract_text(answer_tag),
'url': url})
283 xpath_results =
'//div[contains(@class, "snippet ")]'
285 for result
in eval_xpath_list(dom, xpath_results):
287 url = eval_xpath_getindex(result,
'.//a[contains(@class, "h")]/@href', 0, default=
None)
288 title_tag = eval_xpath_getindex(
289 result,
'.//a[contains(@class, "h")]//div[contains(@class, "title")]', 0, default=
None
291 if url
is None or title_tag
is None or not urlparse(url).netloc:
294 content_tag = eval_xpath_getindex(result,
'.//div[contains(@class, "snippet-description")]', 0, default=
'')
295 pub_date_raw = eval_xpath(result,
'substring-before(.//div[contains(@class, "snippet-description")], "-")')
296 thumbnail = eval_xpath_getindex(result,
'.//img[contains(@class, "thumb")]/@src', 0, default=
'')
300 'title': extract_text(title_tag),
301 'content': extract_text(content_tag),
303 'thumbnail': thumbnail,
306 video_tag = eval_xpath_getindex(
307 result,
'.//div[contains(@class, "video-snippet") and @data-macro="video"]', 0, default=
None
309 if video_tag
is not None:
314 iframe_src = get_embeded_stream_url(url)
316 item[
'iframe_src'] = iframe_src
317 item[
'template'] =
'videos.html'
318 item[
'thumbnail'] = eval_xpath_getindex(video_tag,
'.//img/@src', 0, default=
'')
319 pub_date_raw = extract_text(
320 eval_xpath(video_tag,
'.//div[contains(@class, "snippet-attributes")]/div/text()')
324 item[
'thumbnail'] = eval_xpath_getindex(video_tag,
'.//img/@src', 0, default=
'')
326 result_list.append(item)
396 """Fetch :ref:`languages <brave languages>` and :ref:`regions <brave
397 regions>` from Brave."""
401 import babel.languages
405 engine_traits.custom[
"ui_lang"] = {}
408 'Accept-Encoding':
'gzip, deflate',
410 lang_map = {
'no':
'nb'}
414 resp = get(
'https://search.brave.com/settings', headers=headers)
417 print(
"ERROR: response from Brave is not OK.")
418 dom = html.fromstring(resp.text)
420 for option
in dom.xpath(
'//section//option[@value="en-us"]/../option'):
422 ui_lang = option.get(
'value')
424 l = babel.Locale.parse(ui_lang, sep=
'-')
426 sxng_tag = region_tag(babel.Locale.parse(ui_lang, sep=
'-'))
428 sxng_tag = language_tag(babel.Locale.parse(ui_lang, sep=
'-'))
430 except babel.UnknownLocaleError:
431 print(
"ERROR: can't determine babel locale of Brave's (UI) language %s" % ui_lang)
434 conflict = engine_traits.custom[
"ui_lang"].get(sxng_tag)
436 if conflict != ui_lang:
437 print(
"CONFLICT: babel %s --> %s, %s" % (sxng_tag, conflict, ui_lang))
439 engine_traits.custom[
"ui_lang"][sxng_tag] = ui_lang
443 resp = get(
'https://cdn.search.brave.com/serp/v2/_app/immutable/chunks/parameters.734c106a.js', headers=headers)
446 print(
"ERROR: response from Brave is not OK.")
448 country_js = resp.text[resp.text.index(
"options:{all") + len(
'options:') :]
449 country_js = country_js[: country_js.index(
"},k={default")]
450 country_tags = js_variable_to_python(country_js)
452 for k, v
in country_tags.items():
454 engine_traits.all_locale =
'all'
456 country_tag = v[
'value']
459 for lang_tag
in babel.languages.get_official_languages(country_tag, de_facto=
True):
460 lang_tag = lang_map.get(lang_tag, lang_tag)
461 sxng_tag = region_tag(babel.Locale.parse(
'%s_%s' % (lang_tag, country_tag.upper())))
464 conflict = engine_traits.regions.get(sxng_tag)
466 if conflict != country_tag:
467 print(
"CONFLICT: babel %s --> %s, %s" % (sxng_tag, conflict, country_tag))
469 engine_traits.regions[sxng_tag] = country_tag