2021-01-13 11:31:25 +01:00
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2021-12-18 11:40:12 +01:00
|
|
|
|
# lint: pylint
|
2022-10-27 19:21:17 +02:00
|
|
|
|
"""This is the implementation of the Bing-WEB engine. Some of this
|
|
|
|
|
implementations are shared by other engines:
|
2021-12-18 13:41:12 +01:00
|
|
|
|
|
2022-10-27 19:21:17 +02:00
|
|
|
|
- :ref:`bing images engine`
|
|
|
|
|
- :ref:`bing news engine`
|
|
|
|
|
- :ref:`bing videos engine`
|
|
|
|
|
|
|
|
|
|
On the `preference page`_ Bing offers a lot of languages an regions (see section
|
|
|
|
|
'Search results languages' and 'Country/region'). However, the abundant choice
|
|
|
|
|
does not correspond to reality, where Bing has a full-text indexer only for a
|
|
|
|
|
limited number of languages. By example: you can select a language like Māori
|
|
|
|
|
but you never get a result in this language.
|
|
|
|
|
|
|
|
|
|
What comes a bit closer to the truth are the `search-APIs`_ but they don`t seem
|
|
|
|
|
to be completely correct either (if you take a closer look you will find some
|
|
|
|
|
inaccuracies there too):
|
2014-09-01 14:38:59 +02:00
|
|
|
|
|
2022-10-27 19:21:17 +02:00
|
|
|
|
- :py:obj:`searx.engines.bing.bing_traits_url`
|
|
|
|
|
- :py:obj:`searx.engines.bing_videos.bing_traits_url`
|
|
|
|
|
- :py:obj:`searx.engines.bing_images.bing_traits_url`
|
|
|
|
|
- :py:obj:`searx.engines.bing_news.bing_traits_url`
|
|
|
|
|
|
|
|
|
|
.. _preference page: https://www.bing.com/account/general
|
|
|
|
|
.. _search-APIs: https://learn.microsoft.com/en-us/bing/search-apis/
|
|
|
|
|
|
|
|
|
|
"""
|
2022-10-15 21:17:39 +02:00
|
|
|
|
# pylint: disable=too-many-branches, invalid-name
|
|
|
|
|
|
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
import datetime
|
2019-08-05 16:15:40 +02:00
|
|
|
|
import re
|
2022-10-15 21:17:39 +02:00
|
|
|
|
import uuid
|
|
|
|
|
from urllib.parse import urlencode
|
2014-02-05 20:24:31 +01:00
|
|
|
|
from lxml import html
|
2022-10-15 21:17:39 +02:00
|
|
|
|
import babel
|
|
|
|
|
import babel.languages
|
2013-10-24 23:52:57 +02:00
|
|
|
|
|
2022-10-15 21:17:39 +02:00
|
|
|
|
from searx.utils import eval_xpath, extract_text, eval_xpath_list, eval_xpath_getindex
|
|
|
|
|
from searx.locales import language_tag, region_tag
|
2022-10-03 10:06:17 +02:00
|
|
|
|
from searx.enginelib.traits import EngineTraits
|
|
|
|
|
|
2022-10-15 21:17:39 +02:00
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
logger: logging.Logger
|
|
|
|
|
|
2022-10-03 10:06:17 +02:00
|
|
|
|
traits: EngineTraits
|
|
|
|
|
|
2021-01-13 11:31:25 +01:00
|
|
|
|
about = {
|
|
|
|
|
"website": 'https://www.bing.com',
|
|
|
|
|
"wikidata_id": 'Q182496',
|
|
|
|
|
"official_api_documentation": 'https://www.microsoft.com/en-us/bing/apis/bing-web-search-api',
|
|
|
|
|
"use_official_api": False,
|
|
|
|
|
"require_api_key": False,
|
|
|
|
|
"results": 'HTML',
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-15 21:17:39 +02:00
|
|
|
|
send_accept_language_header = True
|
|
|
|
|
"""Bing tries to guess user's language and territory from the HTTP
|
|
|
|
|
Accept-Language. Optional the user can select a search-language (can be
|
|
|
|
|
different to the UI language) and a region (market code)."""
|
|
|
|
|
|
2014-09-01 14:38:59 +02:00
|
|
|
|
# engine dependent config
|
2021-12-22 16:58:52 +01:00
|
|
|
|
categories = ['general', 'web']
|
2014-01-29 21:14:38 +01:00
|
|
|
|
paging = True
|
2022-10-15 21:17:39 +02:00
|
|
|
|
time_range_support = True
|
|
|
|
|
safesearch = True
|
|
|
|
|
safesearch_types = {2: 'STRICT', 1: 'DEMOTE', 0: 'OFF'} # cookie: ADLT=STRICT
|
2021-12-18 13:41:12 +01:00
|
|
|
|
|
2022-10-15 21:17:39 +02:00
|
|
|
|
base_url = 'https://www.bing.com/search'
|
|
|
|
|
"""Bing (Web) search URL"""
|
2021-12-18 13:41:12 +01:00
|
|
|
|
|
2022-10-15 21:17:39 +02:00
|
|
|
|
bing_traits_url = 'https://learn.microsoft.com/en-us/bing/search-apis/bing-web-search/reference/market-codes'
|
|
|
|
|
"""Bing (Web) search API description"""
|
2013-10-24 23:52:57 +02:00
|
|
|
|
|
2021-12-27 09:26:22 +01:00
|
|
|
|
|
2019-08-05 16:15:40 +02:00
|
|
|
|
def _get_offset_from_pageno(pageno):
|
|
|
|
|
return (pageno - 1) * 10 + 1
|
|
|
|
|
|
2021-12-27 09:26:22 +01:00
|
|
|
|
|
2022-10-15 21:17:39 +02:00
|
|
|
|
def set_bing_cookies(params, engine_language, engine_region, SID):
|
|
|
|
|
|
|
|
|
|
# set cookies
|
|
|
|
|
# -----------
|
|
|
|
|
|
|
|
|
|
params['cookies']['_EDGE_V'] = '1'
|
|
|
|
|
|
|
|
|
|
# _EDGE_S: F=1&SID=3A5253BD6BCA609509B741876AF961CA&mkt=zh-tw
|
|
|
|
|
_EDGE_S = [
|
|
|
|
|
'F=1',
|
|
|
|
|
'SID=%s' % SID,
|
|
|
|
|
'mkt=%s' % engine_region.lower(),
|
|
|
|
|
'ui=%s' % engine_language.lower(),
|
|
|
|
|
]
|
|
|
|
|
params['cookies']['_EDGE_S'] = '&'.join(_EDGE_S)
|
|
|
|
|
logger.debug("cookie _EDGE_S=%s", params['cookies']['_EDGE_S'])
|
|
|
|
|
|
|
|
|
|
# "_EDGE_CD": "m=zh-tw",
|
2014-09-01 14:38:59 +02:00
|
|
|
|
|
2022-10-15 21:17:39 +02:00
|
|
|
|
_EDGE_CD = [ # pylint: disable=invalid-name
|
|
|
|
|
'm=%s' % engine_region.lower(), # search region: zh-cn
|
|
|
|
|
'u=%s' % engine_language.lower(), # UI: en-us
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
params['cookies']['_EDGE_CD'] = '&'.join(_EDGE_CD) + ';'
|
|
|
|
|
logger.debug("cookie _EDGE_CD=%s", params['cookies']['_EDGE_CD'])
|
2021-12-18 13:41:12 +01:00
|
|
|
|
|
2022-10-15 21:17:39 +02:00
|
|
|
|
SRCHHPGUSR = [ # pylint: disable=invalid-name
|
|
|
|
|
'SRCHLANG=%s' % engine_language,
|
|
|
|
|
# Trying to set ADLT cookie here seems not to have any effect, I assume
|
|
|
|
|
# there is some age verification by a cookie (and/or session ID) needed,
|
|
|
|
|
# to disable the SafeSearch.
|
|
|
|
|
'ADLT=%s' % safesearch_types.get(params['safesearch'], 'DEMOTE'),
|
|
|
|
|
]
|
|
|
|
|
params['cookies']['SRCHHPGUSR'] = '&'.join(SRCHHPGUSR)
|
|
|
|
|
logger.debug("cookie SRCHHPGUSR=%s", params['cookies']['SRCHHPGUSR'])
|
2021-12-18 13:41:12 +01:00
|
|
|
|
|
|
|
|
|
|
2022-10-15 21:17:39 +02:00
|
|
|
|
def request(query, params):
|
|
|
|
|
"""Assemble a Bing-Web request."""
|
2016-12-30 18:17:14 +01:00
|
|
|
|
|
2022-10-15 21:17:39 +02:00
|
|
|
|
engine_region = traits.get_region(params['searxng_locale'], 'en-US')
|
|
|
|
|
engine_language = traits.get_language(params['searxng_locale'], 'en')
|
2014-09-01 14:38:59 +02:00
|
|
|
|
|
2022-10-15 21:17:39 +02:00
|
|
|
|
SID = uuid.uuid1().hex.upper()
|
|
|
|
|
CVID = uuid.uuid1().hex.upper()
|
2018-05-21 01:10:22 +02:00
|
|
|
|
|
2022-10-15 21:17:39 +02:00
|
|
|
|
set_bing_cookies(params, engine_language, engine_region, SID)
|
|
|
|
|
|
|
|
|
|
# build URL query
|
|
|
|
|
# ---------------
|
|
|
|
|
|
|
|
|
|
# query term
|
|
|
|
|
page = int(params.get('pageno', 1))
|
|
|
|
|
query_params = {
|
|
|
|
|
# fmt: off
|
|
|
|
|
'q': query,
|
|
|
|
|
'pq': query,
|
|
|
|
|
'cvid': CVID,
|
|
|
|
|
'qs': 'n',
|
|
|
|
|
'sp': '-1'
|
|
|
|
|
# fmt: on
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# page
|
|
|
|
|
if page > 1:
|
|
|
|
|
referer = base_url + '?' + urlencode(query_params)
|
2021-12-18 13:41:12 +01:00
|
|
|
|
params['headers']['Referer'] = referer
|
2021-12-27 09:26:22 +01:00
|
|
|
|
logger.debug("headers.Referer --> %s", referer)
|
2013-10-24 23:52:57 +02:00
|
|
|
|
|
2022-10-15 21:17:39 +02:00
|
|
|
|
query_params['first'] = _get_offset_from_pageno(page)
|
|
|
|
|
|
|
|
|
|
if page == 2:
|
|
|
|
|
query_params['FORM'] = 'PERE'
|
|
|
|
|
elif page > 2:
|
|
|
|
|
query_params['FORM'] = 'PERE%s' % (page - 2)
|
|
|
|
|
|
|
|
|
|
filters = ''
|
|
|
|
|
if params['time_range']:
|
|
|
|
|
query_params['filt'] = 'custom'
|
|
|
|
|
|
|
|
|
|
if params['time_range'] == 'day':
|
|
|
|
|
filters = 'ex1:"ez1"'
|
|
|
|
|
elif params['time_range'] == 'week':
|
|
|
|
|
filters = 'ex1:"ez2"'
|
|
|
|
|
elif params['time_range'] == 'month':
|
|
|
|
|
filters = 'ex1:"ez3"'
|
|
|
|
|
elif params['time_range'] == 'year':
|
|
|
|
|
epoch_1970 = datetime.date(1970, 1, 1)
|
|
|
|
|
today_no = (datetime.date.today() - epoch_1970).days
|
|
|
|
|
filters = 'ex1:"ez5_%s_%s"' % (today_no - 365, today_no)
|
|
|
|
|
|
|
|
|
|
params['url'] = base_url + '?' + urlencode(query_params)
|
|
|
|
|
if filters:
|
|
|
|
|
params['url'] = params['url'] + '&filters=' + filters
|
2021-12-18 13:41:12 +01:00
|
|
|
|
return params
|
2013-10-24 23:52:57 +02:00
|
|
|
|
|
2021-12-27 09:26:22 +01:00
|
|
|
|
|
2013-10-24 23:52:57 +02:00
|
|
|
|
def response(resp):
|
2023-06-25 12:37:31 +02:00
|
|
|
|
# pylint: disable=too-many-locals,import-outside-toplevel
|
|
|
|
|
|
|
|
|
|
from searx.network import Request, multi_requests # see https://github.com/searxng/searxng/issues/762
|
|
|
|
|
|
2013-10-24 23:52:57 +02:00
|
|
|
|
results = []
|
2019-08-05 16:15:40 +02:00
|
|
|
|
result_len = 0
|
2014-09-01 14:38:59 +02:00
|
|
|
|
|
2015-08-28 14:51:32 +02:00
|
|
|
|
dom = html.fromstring(resp.text)
|
2021-12-18 11:40:12 +01:00
|
|
|
|
|
2014-09-01 14:38:59 +02:00
|
|
|
|
# parse results again if nothing is found yet
|
2022-05-21 18:24:47 +02:00
|
|
|
|
|
|
|
|
|
url_to_resolve = []
|
|
|
|
|
url_to_resolve_index = []
|
2023-01-08 19:12:52 +01:00
|
|
|
|
i = 0
|
|
|
|
|
for result in eval_xpath_list(dom, '//ol[@id="b_results"]/li[contains(@class, "b_algo")]'):
|
2021-12-18 11:40:12 +01:00
|
|
|
|
|
2023-01-08 19:12:52 +01:00
|
|
|
|
link = eval_xpath_getindex(result, './/h2/a', 0, None)
|
|
|
|
|
if link is None:
|
|
|
|
|
continue
|
2013-10-25 01:37:48 +02:00
|
|
|
|
url = link.attrib.get('href')
|
2015-01-25 20:14:37 +01:00
|
|
|
|
title = extract_text(link)
|
2023-01-03 22:59:01 +01:00
|
|
|
|
|
|
|
|
|
content = eval_xpath(result, '(.//p)[1]')
|
|
|
|
|
for p in content:
|
2023-04-08 09:43:04 +02:00
|
|
|
|
# Make sure that the element is free of <a href> links
|
2023-01-03 22:59:01 +01:00
|
|
|
|
for e in p.xpath('.//a'):
|
|
|
|
|
e.getparent().remove(e)
|
|
|
|
|
content = extract_text(content)
|
2014-09-01 14:38:59 +02:00
|
|
|
|
|
2022-05-21 18:24:47 +02:00
|
|
|
|
# get the real URL either using the URL shown to user or following the Bing URL
|
|
|
|
|
if url.startswith('https://www.bing.com/ck/a?'):
|
|
|
|
|
url_cite = extract_text(eval_xpath(result, './/div[@class="b_attribution"]/cite'))
|
|
|
|
|
# Bing can shorten the URL either at the end or in the middle of the string
|
|
|
|
|
if (
|
2022-10-15 21:17:39 +02:00
|
|
|
|
url_cite
|
|
|
|
|
and url_cite.startswith('https://')
|
2022-05-21 18:24:47 +02:00
|
|
|
|
and '…' not in url_cite
|
|
|
|
|
and '...' not in url_cite
|
|
|
|
|
and '›' not in url_cite
|
|
|
|
|
):
|
|
|
|
|
# no need for an additional HTTP request
|
|
|
|
|
url = url_cite
|
|
|
|
|
else:
|
|
|
|
|
# resolve the URL with an additional HTTP request
|
|
|
|
|
url_to_resolve.append(url.replace('&ntb=1', '&ntb=F'))
|
|
|
|
|
url_to_resolve_index.append(i)
|
|
|
|
|
url = None # remove the result if the HTTP Bing redirect raise an exception
|
|
|
|
|
|
2014-09-01 14:38:59 +02:00
|
|
|
|
# append result
|
2021-12-27 09:26:22 +01:00
|
|
|
|
results.append({'url': url, 'title': title, 'content': content})
|
2023-01-08 19:12:52 +01:00
|
|
|
|
# increment result pointer for the next iteration in this loop
|
|
|
|
|
i += 1
|
2014-09-01 14:38:59 +02:00
|
|
|
|
|
2022-05-21 18:24:47 +02:00
|
|
|
|
# resolve all Bing redirections in parallel
|
|
|
|
|
request_list = [
|
2023-06-25 12:37:31 +02:00
|
|
|
|
Request.get(u, allow_redirects=False, headers=resp.search_params['headers']) for u in url_to_resolve
|
2022-05-21 18:24:47 +02:00
|
|
|
|
]
|
2023-06-25 12:37:31 +02:00
|
|
|
|
response_list = multi_requests(request_list)
|
2022-05-21 18:24:47 +02:00
|
|
|
|
for i, redirect_response in enumerate(response_list):
|
|
|
|
|
if not isinstance(redirect_response, Exception):
|
|
|
|
|
results[url_to_resolve_index[i]]['url'] = redirect_response.headers['location']
|
|
|
|
|
|
|
|
|
|
# get number_of_results
|
2019-08-05 16:15:40 +02:00
|
|
|
|
try:
|
2020-01-02 22:28:47 +01:00
|
|
|
|
result_len_container = "".join(eval_xpath(dom, '//span[@class="sb_count"]//text()'))
|
2019-08-05 16:15:40 +02:00
|
|
|
|
if "-" in result_len_container:
|
2021-12-18 11:40:12 +01:00
|
|
|
|
|
2019-08-05 16:15:40 +02:00
|
|
|
|
# Remove the part "from-to" for paginated request ...
|
2021-12-27 09:26:22 +01:00
|
|
|
|
result_len_container = result_len_container[result_len_container.find("-") * 2 + 2 :]
|
2019-08-05 16:15:40 +02:00
|
|
|
|
|
|
|
|
|
result_len_container = re.sub('[^0-9]', '', result_len_container)
|
2021-12-18 11:40:12 +01:00
|
|
|
|
|
2019-08-05 16:15:40 +02:00
|
|
|
|
if len(result_len_container) > 0:
|
|
|
|
|
result_len = int(result_len_container)
|
2021-12-18 11:40:12 +01:00
|
|
|
|
|
|
|
|
|
except Exception as e: # pylint: disable=broad-except
|
2019-08-05 16:15:40 +02:00
|
|
|
|
logger.debug('result error :\n%s', e)
|
|
|
|
|
|
2020-01-02 22:28:47 +01:00
|
|
|
|
if result_len and _get_offset_from_pageno(resp.search_params.get("pageno", 0)) > result_len:
|
2019-08-05 16:15:40 +02:00
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
results.append({'number_of_results': result_len})
|
2013-10-24 23:52:57 +02:00
|
|
|
|
return results
|
2016-11-06 03:51:38 +01:00
|
|
|
|
|
|
|
|
|
|
2022-10-15 21:17:39 +02:00
|
|
|
|
def fetch_traits(engine_traits: EngineTraits):
|
|
|
|
|
"""Fetch languages and regions from Bing-Web."""
|
2022-10-03 10:06:17 +02:00
|
|
|
|
|
2022-10-15 21:17:39 +02:00
|
|
|
|
xpath_market_codes = '//table[1]/tbody/tr/td[3]'
|
|
|
|
|
# xpath_country_codes = '//table[2]/tbody/tr/td[2]'
|
|
|
|
|
xpath_language_codes = '//table[3]/tbody/tr/td[2]'
|
2022-10-03 10:06:17 +02:00
|
|
|
|
|
2022-10-15 21:17:39 +02:00
|
|
|
|
_fetch_traits(engine_traits, bing_traits_url, xpath_language_codes, xpath_market_codes)
|
2022-10-03 10:06:17 +02:00
|
|
|
|
|
|
|
|
|
|
2022-10-15 21:17:39 +02:00
|
|
|
|
def _fetch_traits(engine_traits: EngineTraits, url: str, xpath_language_codes: str, xpath_market_codes: str):
|
2023-06-25 12:37:31 +02:00
|
|
|
|
# pylint: disable=too-many-locals,import-outside-toplevel
|
|
|
|
|
|
|
|
|
|
from searx.network import get # see https://github.com/searxng/searxng/issues/762
|
2022-10-03 10:06:17 +02:00
|
|
|
|
|
2022-10-15 21:17:39 +02:00
|
|
|
|
# insert alias to map from a language (zh) to a language + script (zh_Hans)
|
|
|
|
|
engine_traits.languages['zh'] = 'zh-hans'
|
2022-10-03 10:06:17 +02:00
|
|
|
|
|
2023-06-25 12:37:31 +02:00
|
|
|
|
resp = get(url)
|
2022-10-03 10:06:17 +02:00
|
|
|
|
|
2023-06-25 12:37:31 +02:00
|
|
|
|
if not resp.ok: # type: ignore
|
2022-10-03 10:06:17 +02:00
|
|
|
|
print("ERROR: response from peertube is not OK.")
|
|
|
|
|
|
2023-06-25 12:37:31 +02:00
|
|
|
|
dom = html.fromstring(resp.text) # type: ignore
|
2022-10-03 10:06:17 +02:00
|
|
|
|
|
2022-10-15 21:17:39 +02:00
|
|
|
|
map_lang = {'jp': 'ja'}
|
|
|
|
|
for td in eval_xpath(dom, xpath_language_codes):
|
|
|
|
|
eng_lang = td.text
|
2022-10-03 10:06:17 +02:00
|
|
|
|
|
2022-10-15 21:17:39 +02:00
|
|
|
|
if eng_lang in ('en-gb', 'pt-br'):
|
|
|
|
|
# language 'en' is already in the list and a language 'en-gb' can't
|
|
|
|
|
# be handled in SearXNG, same with pt-br which is covered by pt-pt.
|
2022-10-03 10:06:17 +02:00
|
|
|
|
continue
|
|
|
|
|
|
2022-10-15 21:17:39 +02:00
|
|
|
|
babel_lang = map_lang.get(eng_lang, eng_lang).replace('-', '_')
|
2022-10-03 10:06:17 +02:00
|
|
|
|
try:
|
2022-10-15 21:17:39 +02:00
|
|
|
|
sxng_tag = language_tag(babel.Locale.parse(babel_lang))
|
2022-10-03 10:06:17 +02:00
|
|
|
|
except babel.UnknownLocaleError:
|
2022-10-15 21:17:39 +02:00
|
|
|
|
print("ERROR: language (%s) is unknown by babel" % (eng_lang))
|
2022-10-03 10:06:17 +02:00
|
|
|
|
continue
|
|
|
|
|
conflict = engine_traits.languages.get(sxng_tag)
|
|
|
|
|
if conflict:
|
|
|
|
|
if conflict != eng_lang:
|
|
|
|
|
print("CONFLICT: babel %s --> %s, %s" % (sxng_tag, conflict, eng_lang))
|
|
|
|
|
continue
|
|
|
|
|
engine_traits.languages[sxng_tag] = eng_lang
|
|
|
|
|
|
2022-10-15 21:17:39 +02:00
|
|
|
|
map_region = {
|
|
|
|
|
'en-ID': 'id_ID',
|
|
|
|
|
'no-NO': 'nb_NO',
|
|
|
|
|
}
|
2022-10-03 10:06:17 +02:00
|
|
|
|
|
2022-10-15 21:17:39 +02:00
|
|
|
|
for td in eval_xpath(dom, xpath_market_codes):
|
|
|
|
|
eng_region = td.text
|
|
|
|
|
babel_region = map_region.get(eng_region, eng_region).replace('-', '_')
|
2022-10-03 10:06:17 +02:00
|
|
|
|
|
2022-10-15 21:17:39 +02:00
|
|
|
|
if eng_region == 'en-WW':
|
|
|
|
|
engine_traits.all_locale = eng_region
|
2022-10-03 10:06:17 +02:00
|
|
|
|
continue
|
|
|
|
|
|
2022-10-15 21:17:39 +02:00
|
|
|
|
try:
|
|
|
|
|
sxng_tag = region_tag(babel.Locale.parse(babel_region))
|
|
|
|
|
except babel.UnknownLocaleError:
|
|
|
|
|
print("ERROR: region (%s) is unknown by babel" % (eng_region))
|
2022-10-03 10:06:17 +02:00
|
|
|
|
continue
|
2022-10-15 21:17:39 +02:00
|
|
|
|
conflict = engine_traits.regions.get(sxng_tag)
|
|
|
|
|
if conflict:
|
|
|
|
|
if conflict != eng_region:
|
|
|
|
|
print("CONFLICT: babel %s --> %s, %s" % (sxng_tag, conflict, eng_region))
|
|
|
|
|
continue
|
|
|
|
|
engine_traits.regions[sxng_tag] = eng_region
|