2021-01-13 11:31:25 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2021-12-17 12:24:28 +01:00
|
|
|
# lint: pylint
|
2022-10-22 20:19:52 +02:00
|
|
|
"""Bing-News: description see :py:obj:`searx.engines.bing`.
|
2015-05-02 15:45:17 +02:00
|
|
|
"""
|
2014-09-01 14:38:59 +02:00
|
|
|
|
2022-10-22 20:19:52 +02:00
|
|
|
# pylint: disable=invalid-name
|
|
|
|
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
import uuid
|
|
|
|
from urllib.parse import urlencode
|
|
|
|
|
|
|
|
from lxml import html
|
|
|
|
|
2022-10-15 21:17:39 +02:00
|
|
|
from searx.enginelib.traits import EngineTraits
|
2022-10-22 20:19:52 +02:00
|
|
|
from searx.engines.bing import (
|
|
|
|
set_bing_cookies,
|
|
|
|
_fetch_traits,
|
|
|
|
)
|
|
|
|
from searx.engines.bing import send_accept_language_header # pylint: disable=unused-import
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
import logging
|
|
|
|
|
|
|
|
logger: logging.Logger
|
|
|
|
|
|
|
|
traits: EngineTraits
|
|
|
|
|
2020-03-01 08:01:36 +01:00
|
|
|
|
2021-01-13 11:31:25 +01:00
|
|
|
# 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',
|
|
|
|
}
|
|
|
|
|
2014-09-01 14:38:59 +02:00
|
|
|
# engine dependent config
|
2014-03-04 13:10:04 +01:00
|
|
|
categories = ['news']
|
|
|
|
paging = True
|
2016-10-30 18:14:42 +01:00
|
|
|
time_range_support = True
|
2022-10-22 20:19:52 +02:00
|
|
|
time_map = {
|
|
|
|
'day': '4',
|
|
|
|
'week': '8',
|
|
|
|
'month': '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.
|
|
|
|
"""
|
|
|
|
|
|
|
|
base_url = 'https://www.bing.com/news/infinitescrollajax'
|
|
|
|
"""Bing (News) search URL"""
|
|
|
|
|
|
|
|
bing_traits_url = 'https://learn.microsoft.com/en-us/bing/search-apis/bing-news-search/reference/market-codes'
|
|
|
|
"""Bing (News) search API description"""
|
|
|
|
|
|
|
|
mkt_alias = {
|
|
|
|
'zh': 'en-WW',
|
|
|
|
'zh-CN': 'en-WW',
|
|
|
|
}
|
|
|
|
"""Bing News has an official market code 'zh-CN' but we won't get a result with
|
|
|
|
this market code. For 'zh' and 'zh-CN' we better use the *Worldwide aggregate*
|
|
|
|
market code (en-WW).
|
|
|
|
"""
|
2016-10-30 18:14:42 +01:00
|
|
|
|
2021-12-27 09:26:22 +01:00
|
|
|
|
2014-03-04 13:10:04 +01:00
|
|
|
def request(query, params):
|
2022-10-22 20:19:52 +02:00
|
|
|
"""Assemble a Bing-News request."""
|
|
|
|
|
|
|
|
sxng_locale = params['searxng_locale']
|
|
|
|
engine_region = traits.get_region(mkt_alias.get(sxng_locale, sxng_locale), traits.all_locale)
|
|
|
|
engine_language = traits.get_language(sxng_locale, 'en')
|
|
|
|
|
|
|
|
SID = uuid.uuid1().hex.upper()
|
|
|
|
set_bing_cookies(params, engine_language, engine_region, SID)
|
2021-12-17 12:24:28 +01:00
|
|
|
|
2022-10-22 20:19:52 +02:00
|
|
|
# build URL query
|
|
|
|
#
|
|
|
|
# example: https://www.bing.com/news/infinitescrollajax?q=london&first=1
|
2016-12-11 16:41:14 +01:00
|
|
|
|
2022-10-22 20:19:52 +02:00
|
|
|
query_params = {
|
|
|
|
# fmt: off
|
|
|
|
'q': query,
|
|
|
|
'InfiniteScroll': 1,
|
|
|
|
# to simplify the page count lets use the default of 10 images per page
|
|
|
|
'first' : (int(params.get('pageno', 1)) - 1) * 10 + 1,
|
|
|
|
# fmt: on
|
|
|
|
}
|
|
|
|
|
|
|
|
if params['time_range']:
|
|
|
|
# qft=interval:"7"
|
|
|
|
query_params['qft'] = 'qft=interval="%s"' % time_map.get(params['time_range'], '9')
|
|
|
|
|
|
|
|
params['url'] = base_url + '?' + urlencode(query_params)
|
2015-01-29 20:56:57 +01:00
|
|
|
|
2014-03-04 13:10:04 +01:00
|
|
|
return params
|
|
|
|
|
2021-12-27 09:26:22 +01:00
|
|
|
|
2014-03-04 13:10:04 +01:00
|
|
|
def response(resp):
|
2022-10-22 20:19:52 +02:00
|
|
|
"""Get response from Bing-Video"""
|
2021-12-17 12:24:28 +01:00
|
|
|
results = []
|
2022-10-22 20:19:52 +02:00
|
|
|
|
|
|
|
if not resp.ok or not resp.text:
|
|
|
|
return results
|
|
|
|
|
|
|
|
dom = html.fromstring(resp.text)
|
|
|
|
|
|
|
|
for newsitem in dom.xpath('//div[contains(@class, "newsitem")]'):
|
|
|
|
|
|
|
|
url = newsitem.xpath('./@url')[0]
|
|
|
|
title = ' '.join(newsitem.xpath('.//div[@class="caption"]//a[@class="title"]/text()')).strip()
|
|
|
|
content = ' '.join(newsitem.xpath('.//div[@class="snippet"]/text()')).strip()
|
|
|
|
thumbnail = None
|
|
|
|
author = newsitem.xpath('./@data-author')[0]
|
|
|
|
metadata = ' '.join(newsitem.xpath('.//div[@class="source"]/span/text()')).strip()
|
|
|
|
|
|
|
|
img_src = newsitem.xpath('.//a[@class="imagelink"]//img/@src')
|
|
|
|
if img_src:
|
|
|
|
thumbnail = 'https://www.bing.com/' + img_src[0]
|
|
|
|
|
|
|
|
results.append(
|
|
|
|
{
|
|
|
|
'url': url,
|
|
|
|
'title': title,
|
|
|
|
'content': content,
|
|
|
|
'img_src': thumbnail,
|
|
|
|
'author': author,
|
|
|
|
'metadata': metadata,
|
|
|
|
}
|
|
|
|
)
|
2014-09-01 14:38:59 +02:00
|
|
|
|
2014-03-04 13:10:04 +01:00
|
|
|
return results
|
2022-10-15 21:17:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
def fetch_traits(engine_traits: EngineTraits):
|
2022-10-22 20:19:52 +02:00
|
|
|
"""Fetch languages and regions from Bing-News.
|
2022-10-15 21:17:39 +02:00
|
|
|
|
2022-10-22 20:19:52 +02:00
|
|
|
The :py:obj:`description <searx.engines.bing_news.bing_traits_url>` of the
|
|
|
|
first table says *"query parameter when calling the Video Search API."*
|
|
|
|
.. thats why I use the 4. table "News Category API markets" for the
|
|
|
|
``xpath_market_codes``.
|
2022-10-15 21:17:39 +02:00
|
|
|
|
2022-10-22 20:19:52 +02:00
|
|
|
"""
|
2022-10-15 21:17:39 +02:00
|
|
|
|
|
|
|
xpath_market_codes = '//table[4]/tbody/tr/td[3]'
|
|
|
|
# xpath_country_codes = '//table[2]/tbody/tr/td[2]'
|
|
|
|
xpath_language_codes = '//table[3]/tbody/tr/td[2]'
|
|
|
|
|
2022-10-22 20:19:52 +02:00
|
|
|
_fetch_traits(engine_traits, bing_traits_url, xpath_language_codes, xpath_market_codes)
|