85 """Assemble a Bing-Web request."""
87 engine_region = traits.get_region(params[
'searxng_locale'], traits.all_locale)
88 engine_language = traits.get_language(params[
'searxng_locale'],
'en')
89 set_bing_cookies(params, engine_language, engine_region)
91 page = params.get(
'pageno', 1)
107 query_params[
'FORM'] =
'PERE'
109 query_params[
'FORM'] =
'PERE%s' % (page - 2)
111 params[
'url'] = f
'{base_url}?{urlencode(query_params)}'
113 if params.get(
'time_range'):
114 unix_day = int(time.time() / 86400)
115 time_ranges = {
'day':
'1',
'week':
'2',
'month':
'3',
'year': f
'5_{unix_day-365}_{unix_day}'}
116 params[
'url'] += f
'&filters=ex1:"ez{time_ranges[params["time_range"]]}"'
127 dom = html.fromstring(resp.text)
131 for result
in eval_xpath_list(dom,
'//ol[@id="b_results"]/li[contains(@class, "b_algo")]'):
133 link = eval_xpath_getindex(result,
'.//h2/a', 0,
None)
136 url = link.attrib.get(
'href')
137 title = extract_text(link)
139 content = eval_xpath(result,
'.//p')
143 for e
in p.xpath(
'.//span[@class="algoSlug_icon"]'):
144 e.getparent().remove(e)
145 content = extract_text(content)
148 if url.startswith(
'https://www.bing.com/ck/a?'):
150 url_query = urlparse(url).query
151 parsed_url_query = parse_qs(url_query)
152 param_u = parsed_url_query[
"u"][0]
154 encoded_url = param_u[2:]
156 encoded_url = encoded_url +
'=' * (-len(encoded_url) % 4)
158 url = base64.urlsafe_b64decode(encoded_url).decode()
161 results.append({
'url': url,
'title': title,
'content': content})
165 result_len_container =
"".join(eval_xpath(dom,
'//span[@class="sb_count"]//text()'))
166 if "-" in result_len_container:
169 result_len_container = result_len_container[result_len_container.find(
"-") * 2 + 2 :]
171 result_len_container = re.sub(
'[^0-9]',
'', result_len_container)
173 if len(result_len_container) > 0:
174 result_len = int(result_len_container)
176 except Exception
as e:
177 logger.debug(
'result error :\n%s', e)
179 if result_len
and _page_offset(resp.search_params.get(
"pageno", 0)) > result_len:
186 results.append({
'number_of_results': result_len})
191 """Fetch languages and regions from Bing-Web."""
196 resp = get(
"https://www.bing.com/account/general")
198 print(
"ERROR: response from bing is not OK.")
200 dom = html.fromstring(resp.text)
204 engine_traits.languages[
'zh'] =
'zh-hans'
206 map_lang = {
'prs':
'fa-AF',
'en':
'en-us'}
213 for href
in eval_xpath(dom,
'//div[@id="language-section"]//li/a/@href'):
214 eng_lang = parse_qs(urlparse(href).query)[
'setlang'][0]
215 babel_lang = map_lang.get(eng_lang, eng_lang)
217 sxng_tag = language_tag(babel.Locale.parse(babel_lang.replace(
'-',
'_')))
218 except babel.UnknownLocaleError:
219 print(
"ERROR: language (%s) is unknown by babel" % (babel_lang))
225 bing_ui_lang = eng_lang.lower()
226 if '-' not in bing_ui_lang:
227 bing_ui_lang = bing_ui_lang +
'-' + bing_ui_lang_map.get(bing_ui_lang, bing_ui_lang)
229 conflict = engine_traits.languages.get(sxng_tag)
231 if conflict != bing_ui_lang:
232 print(f
"CONFLICT: babel {sxng_tag} --> {conflict}, {bing_ui_lang}")
234 engine_traits.languages[sxng_tag] = bing_ui_lang
238 engine_traits.regions[
'zh-CN'] =
'zh-cn'
243 for href
in eval_xpath(dom,
'//div[@id="region-section"]//li/a/@href'):
244 cc_tag = parse_qs(urlparse(href).query)[
'cc'][0]
245 if cc_tag ==
'clear':
246 engine_traits.all_locale = cc_tag
250 for lang_tag
in babel.languages.get_official_languages(cc_tag, de_facto=
True):
251 if lang_tag
not in engine_traits.languages.keys():
254 lang_tag = lang_tag.split(
'_')[0]
255 market_code = f
"{lang_tag}-{cc_tag}"
257 market_code = map_market_codes.get(market_code, market_code)
258 sxng_tag = region_tag(babel.Locale.parse(
'%s_%s' % (lang_tag, cc_tag.upper())))
259 conflict = engine_traits.regions.get(sxng_tag)
261 if conflict != market_code:
262 print(
"CONFLICT: babel %s --> %s, %s" % (sxng_tag, conflict, market_code))
264 engine_traits.regions[sxng_tag] = market_code