mirror of
https://github.com/searxng/searxng.git
synced 2024-06-02 13:29:22 +00:00
86b4d2f2d0
We have been using a static type checker (pyright) for a long time, but its check was not yet a prerequisite for passing the quality gate. It was checked in the CI, but the error messages were only logged. As is always the case in life, with checks that you have to do but which have no consequences; you neglect them :-) We didn't activate the checks back then because we (even today) have too much monkey patching in our code (not only in the engines, httpx and others objects are also affected). We want to replace monkey patching with clear interfaces for a long time, the basis for this is increased typing and we can only achieve this if we make type checking an integral part of the quality gate. This PR activates the type check; in order to pass the check, a few typings were corrected in the code, but most type inconsistencies were deactivated via inline comments. This was particularly necessary in places where the code uses properties that stick to the objects (monkey patching). The sticking of properties only happens in a few places, but the access to these properties extends over the entire code, which is why there are many `# type: ignore` markers in the code ... which we will hopefully be able to remove again successively in the future. Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
151 lines
4.7 KiB
Python
151 lines
4.7 KiB
Python
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
"""Bing-News: description see :py:obj:`searx.engines.bing`.
|
|
|
|
.. hint::
|
|
|
|
Bing News is *different* in some ways!
|
|
|
|
"""
|
|
|
|
# pylint: disable=invalid-name
|
|
|
|
from urllib.parse import urlencode
|
|
|
|
from lxml import html
|
|
|
|
from searx.utils import eval_xpath, extract_text, eval_xpath_list, eval_xpath_getindex
|
|
from searx.enginelib.traits import EngineTraits
|
|
from searx.engines.bing import set_bing_cookies
|
|
|
|
|
|
# about
|
|
about = {
|
|
"website": 'https://www.bing.com/news',
|
|
"wikidata_id": 'Q2878637',
|
|
"official_api_documentation": 'https://www.microsoft.com/en-us/bing/apis/bing-news-search-api',
|
|
"use_official_api": False,
|
|
"require_api_key": False,
|
|
"results": 'RSS',
|
|
}
|
|
|
|
# engine dependent config
|
|
categories = ['news']
|
|
paging = True
|
|
"""If go through the pages and there are actually no new results for another
|
|
page, then bing returns the results from the last page again."""
|
|
|
|
time_range_support = True
|
|
time_map = {
|
|
'day': 'interval="4"',
|
|
'week': 'interval="7"',
|
|
'month': 'interval="9"',
|
|
}
|
|
"""A string '4' means *last hour*. We use *last hour* for ``day`` here since the
|
|
difference of *last day* and *last week* in the result list is just marginally.
|
|
Bing does not have news range ``year`` / we use ``month`` instead."""
|
|
|
|
base_url = 'https://www.bing.com/news/infinitescrollajax'
|
|
"""Bing (News) search URL"""
|
|
|
|
|
|
def request(query, params):
|
|
"""Assemble a Bing-News request."""
|
|
|
|
engine_region = traits.get_region(params['searxng_locale'], traits.all_locale) # type: ignore
|
|
engine_language = traits.get_language(params['searxng_locale'], 'en') # type: ignore
|
|
set_bing_cookies(params, engine_language, engine_region)
|
|
|
|
# build URL query
|
|
#
|
|
# example: https://www.bing.com/news/infinitescrollajax?q=london&first=1
|
|
|
|
page = int(params.get('pageno', 1)) - 1
|
|
query_params = {
|
|
'q': query,
|
|
'InfiniteScroll': 1,
|
|
# to simplify the page count lets use the default of 10 images per page
|
|
'first': page * 10 + 1,
|
|
'SFX': page,
|
|
'form': 'PTFTNR',
|
|
'setlang': engine_region.split('-')[0],
|
|
'cc': engine_region.split('-')[-1],
|
|
}
|
|
|
|
if params['time_range']:
|
|
query_params['qft'] = time_map.get(params['time_range'], 'interval="9"')
|
|
|
|
params['url'] = base_url + '?' + urlencode(query_params)
|
|
|
|
return params
|
|
|
|
|
|
def response(resp):
|
|
"""Get response from Bing-Video"""
|
|
results = []
|
|
|
|
if not resp.ok or not resp.text:
|
|
return results
|
|
|
|
dom = html.fromstring(resp.text)
|
|
|
|
for newsitem in eval_xpath_list(dom, '//div[contains(@class, "newsitem")]'):
|
|
|
|
link = eval_xpath_getindex(newsitem, './/a[@class="title"]', 0, None)
|
|
if link is None:
|
|
continue
|
|
url = link.attrib.get('href')
|
|
title = extract_text(link)
|
|
content = extract_text(eval_xpath(newsitem, './/div[@class="snippet"]'))
|
|
|
|
metadata = []
|
|
source = eval_xpath_getindex(newsitem, './/div[contains(@class, "source")]', 0, None)
|
|
if source is not None:
|
|
for item in (
|
|
eval_xpath_getindex(source, './/span[@aria-label]/@aria-label', 0, None),
|
|
# eval_xpath_getindex(source, './/a', 0, None),
|
|
# eval_xpath_getindex(source, './div/span', 3, None),
|
|
link.attrib.get('data-author'),
|
|
):
|
|
if item is not None:
|
|
t = extract_text(item)
|
|
if t and t.strip():
|
|
metadata.append(t.strip())
|
|
metadata = ' | '.join(metadata)
|
|
|
|
thumbnail = None
|
|
imagelink = eval_xpath_getindex(newsitem, './/a[@class="imagelink"]//img', 0, None)
|
|
if imagelink is not None:
|
|
thumbnail = 'https://www.bing.com/' + imagelink.attrib.get('src')
|
|
|
|
results.append(
|
|
{
|
|
'url': url,
|
|
'title': title,
|
|
'content': content,
|
|
'img_src': thumbnail,
|
|
'metadata': metadata,
|
|
}
|
|
)
|
|
|
|
return results
|
|
|
|
|
|
def fetch_traits(engine_traits: EngineTraits):
|
|
"""Fetch languages and regions from Bing-News."""
|
|
# pylint: disable=import-outside-toplevel
|
|
|
|
from searx.engines.bing import fetch_traits as _f
|
|
|
|
_f(engine_traits)
|
|
|
|
# fix market codes not known by bing news:
|
|
|
|
# In bing the market code 'zh-cn' exists, but there is no 'news' category in
|
|
# bing for this market. Alternatively we use the the market code from Honk
|
|
# Kong. Even if this is not correct, it is better than having no hits at
|
|
# all, or sending false queries to bing that could raise the suspicion of a
|
|
# bot.
|
|
|
|
# HINT: 'en-hk' is the region code it does not indicate the language en!!
|
|
engine_traits.regions['zh-CN'] = 'en-hk'
|