2015-05-02 15:45:17 +02:00
|
|
|
"""
|
|
|
|
Bing (News)
|
|
|
|
|
|
|
|
@website https://www.bing.com/news
|
|
|
|
@provide-api yes (http://datamarket.azure.com/dataset/bing/search),
|
|
|
|
max. 5000 query/month
|
|
|
|
|
|
|
|
@using-api no (because of query limit)
|
2015-06-04 18:30:08 +02:00
|
|
|
@results RSS (using search portal)
|
|
|
|
@stable yes (except perhaps for the images)
|
|
|
|
@parse url, title, content, publishedDate, thumbnail
|
2015-05-02 15:45:17 +02:00
|
|
|
"""
|
2014-09-01 14:38:59 +02:00
|
|
|
|
2015-06-04 18:30:08 +02:00
|
|
|
from datetime import datetime
|
2014-09-01 14:38:59 +02:00
|
|
|
from dateutil import parser
|
2020-08-06 17:42:46 +02:00
|
|
|
from urllib.parse import urlencode, urlparse, parse_qsl
|
2015-06-04 18:30:08 +02:00
|
|
|
from lxml import etree
|
2018-03-01 05:30:48 +01:00
|
|
|
from searx.utils import list_get, match_language
|
2020-11-02 11:19:53 +01:00
|
|
|
from searx.engines.bing import language_aliases
|
2020-11-16 09:43:23 +01:00
|
|
|
from searx.engines.bing import _fetch_supported_languages, supported_languages_url # NOQA # pylint: disable=unused-import
|
2020-03-01 08:01:36 +01:00
|
|
|
|
2014-09-01 14:38:59 +02:00
|
|
|
# engine dependent config
|
2014-03-04 13:10:04 +01:00
|
|
|
categories = ['news']
|
|
|
|
paging = True
|
|
|
|
language_support = True
|
2016-10-30 18:14:42 +01:00
|
|
|
time_range_support = True
|
2014-03-04 13:10:04 +01:00
|
|
|
|
2014-09-01 14:38:59 +02:00
|
|
|
# search-url
|
|
|
|
base_url = 'https://www.bing.com/'
|
2015-06-04 18:30:08 +02:00
|
|
|
search_string = 'news/search?{query}&first={offset}&format=RSS'
|
2016-10-30 18:14:42 +01:00
|
|
|
search_string_with_time = 'news/search?{query}&first={offset}&qft=interval%3d"{interval}"&format=RSS'
|
|
|
|
time_range_dict = {'day': '7',
|
|
|
|
'week': '8',
|
|
|
|
'month': '9'}
|
2015-06-04 18:30:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
# remove click
|
|
|
|
def url_cleanup(url_string):
|
|
|
|
parsed_url = urlparse(url_string)
|
|
|
|
if parsed_url.netloc == 'www.bing.com' and parsed_url.path == '/news/apiclick.aspx':
|
|
|
|
query = dict(parse_qsl(parsed_url.query))
|
|
|
|
return query.get('url', None)
|
|
|
|
return url_string
|
|
|
|
|
|
|
|
|
|
|
|
# replace the http://*bing4.com/th?id=... by https://www.bing.com/th?id=...
|
|
|
|
def image_url_cleanup(url_string):
|
|
|
|
parsed_url = urlparse(url_string)
|
|
|
|
if parsed_url.netloc.endswith('bing4.com') and parsed_url.path == '/th':
|
|
|
|
query = dict(parse_qsl(parsed_url.query))
|
|
|
|
return "https://www.bing.com/th?id=" + query.get('id')
|
|
|
|
return url_string
|
2014-03-04 13:10:04 +01:00
|
|
|
|
2014-09-02 17:13:44 +02:00
|
|
|
|
2016-10-30 18:14:42 +01:00
|
|
|
def _get_url(query, language, offset, time_range):
|
|
|
|
if time_range in time_range_dict:
|
|
|
|
search_path = search_string_with_time.format(
|
|
|
|
query=urlencode({'q': query, 'setmkt': language}),
|
|
|
|
offset=offset,
|
|
|
|
interval=time_range_dict[time_range])
|
|
|
|
else:
|
2020-02-25 18:44:28 +01:00
|
|
|
# e.g. setmkt=de-de&setlang=de
|
2016-10-30 18:14:42 +01:00
|
|
|
search_path = search_string.format(
|
2020-03-01 11:07:59 +01:00
|
|
|
query=urlencode({'q': query, 'setmkt': language}),
|
2016-10-30 18:14:42 +01:00
|
|
|
offset=offset)
|
|
|
|
return base_url + search_path
|
|
|
|
|
|
|
|
|
2014-09-01 14:38:59 +02:00
|
|
|
# do search-request
|
2014-03-04 13:10:04 +01:00
|
|
|
def request(query, params):
|
2016-12-11 16:41:14 +01:00
|
|
|
if params['time_range'] and params['time_range'] not in time_range_dict:
|
|
|
|
return params
|
|
|
|
|
2014-03-04 13:10:04 +01:00
|
|
|
offset = (params['pageno'] - 1) * 10 + 1
|
2014-09-01 14:38:59 +02:00
|
|
|
|
2019-01-06 15:27:46 +01:00
|
|
|
if params['language'] == 'all':
|
|
|
|
language = 'en-US'
|
|
|
|
else:
|
|
|
|
language = match_language(params['language'], supported_languages, language_aliases)
|
2014-09-01 14:38:59 +02:00
|
|
|
|
2016-10-30 18:14:42 +01:00
|
|
|
params['url'] = _get_url(query, language, offset, params['time_range'])
|
2015-01-29 20:56:57 +01:00
|
|
|
|
2014-03-04 13:10:04 +01:00
|
|
|
return params
|
|
|
|
|
|
|
|
|
2014-09-01 14:38:59 +02:00
|
|
|
# get response from search-request
|
2014-03-04 13:10:04 +01:00
|
|
|
def response(resp):
|
|
|
|
results = []
|
2014-09-01 14:38:59 +02:00
|
|
|
|
2017-05-22 15:36:52 +02:00
|
|
|
rss = etree.fromstring(resp.content)
|
2015-06-04 18:30:08 +02:00
|
|
|
|
|
|
|
ns = rss.nsmap
|
2014-09-01 14:38:59 +02:00
|
|
|
|
|
|
|
# parse results
|
2015-06-04 18:30:08 +02:00
|
|
|
for item in rss.xpath('./channel/item'):
|
|
|
|
# url / title / content
|
|
|
|
url = url_cleanup(item.xpath('./link/text()')[0])
|
|
|
|
title = list_get(item.xpath('./title/text()'), 0, url)
|
|
|
|
content = list_get(item.xpath('./description/text()'), 0, '')
|
|
|
|
|
|
|
|
# publishedDate
|
|
|
|
publishedDate = list_get(item.xpath('./pubDate/text()'), 0)
|
|
|
|
try:
|
|
|
|
publishedDate = parser.parse(publishedDate, dayfirst=False)
|
|
|
|
except TypeError:
|
|
|
|
publishedDate = datetime.now()
|
|
|
|
except ValueError:
|
|
|
|
publishedDate = datetime.now()
|
|
|
|
|
|
|
|
# thumbnail
|
|
|
|
thumbnail = list_get(item.xpath('./News:Image/text()', namespaces=ns), 0)
|
|
|
|
if thumbnail is not None:
|
|
|
|
thumbnail = image_url_cleanup(thumbnail)
|
2014-12-07 16:37:56 +01:00
|
|
|
|
2014-09-01 14:38:59 +02:00
|
|
|
# append result
|
2015-06-04 18:30:08 +02:00
|
|
|
if thumbnail is not None:
|
2017-02-12 14:58:49 +01:00
|
|
|
results.append({'url': url,
|
2015-06-04 18:30:08 +02:00
|
|
|
'title': title,
|
|
|
|
'publishedDate': publishedDate,
|
|
|
|
'content': content,
|
2017-02-12 14:58:49 +01:00
|
|
|
'img_src': thumbnail})
|
2015-06-04 18:30:08 +02:00
|
|
|
else:
|
|
|
|
results.append({'url': url,
|
|
|
|
'title': title,
|
|
|
|
'publishedDate': publishedDate,
|
|
|
|
'content': content})
|
2014-09-01 14:38:59 +02:00
|
|
|
|
|
|
|
# return results
|
2014-03-04 13:10:04 +01:00
|
|
|
return results
|