| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							| 
									
										
										
										
											2022-10-22 20:19:52 +02:00
										 |  |  | """Bing-News: description see :py:obj:`searx.engines.bing`.
 | 
					
						
							| 
									
										
										
										
											2023-09-27 18:24:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. hint:: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Bing News is *different* in some ways! | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | from urllib.parse import urlencode | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from lxml import html | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-27 18:24:33 +02:00
										 |  |  | from searx.utils import eval_xpath, extract_text, eval_xpath_list, eval_xpath_getindex | 
					
						
							| 
									
										
										
										
											2022-10-15 21:17:39 +02:00
										 |  |  | from searx.enginelib.traits import EngineTraits | 
					
						
							| 
									
										
										
										
											2023-09-27 18:24:33 +02:00
										 |  |  | from searx.engines.bing import set_bing_cookies | 
					
						
							| 
									
										
										
										
											2022-10-22 20:19:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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 | 
					
						
							| 
									
										
										
										
											2023-09-27 18:24:33 +02:00
										 |  |  | """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."""
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-30 18:14:42 +01:00
										 |  |  | time_range_support = True | 
					
						
							| 
									
										
										
										
											2022-10-22 20:19:52 +02:00
										 |  |  | time_map = { | 
					
						
							| 
									
										
										
										
											2023-09-27 18:24:33 +02:00
										 |  |  |     'day': 'interval="4"', | 
					
						
							|  |  |  |     'week': 'interval="7"', | 
					
						
							|  |  |  |     'month': 'interval="9"', | 
					
						
							| 
									
										
										
										
											2022-10-22 20:19:52 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-09-27 18:24:33 +02:00
										 |  |  | """A string '4' means *last hour*.  We use *last hour* for ``day`` here since the
 | 
					
						
							| 
									
										
										
										
											2022-10-22 20:19:52 +02:00
										 |  |  | difference of *last day* and *last week* in the result list is just marginally. | 
					
						
							| 
									
										
										
										
											2023-09-27 18:24:33 +02:00
										 |  |  | Bing does not have news range ``year`` / we use ``month`` instead."""
 | 
					
						
							| 
									
										
										
										
											2022-10-22 20:19:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | base_url = 'https://www.bing.com/news/infinitescrollajax' | 
					
						
							|  |  |  | """Bing (News) search URL""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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.""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-27 18:24:33 +02:00
										 |  |  |     engine_region = traits.get_region(params['searxng_locale'], traits.all_locale)  # type: ignore | 
					
						
							|  |  |  |     engine_language = traits.get_language(params['searxng_locale'], 'en')  # type: ignore | 
					
						
							| 
									
										
										
										
											2023-09-20 17:39:42 +02:00
										 |  |  |     set_bing_cookies(params, engine_language, engine_region) | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-27 18:24:33 +02:00
										 |  |  |     page = int(params.get('pageno', 1)) - 1 | 
					
						
							| 
									
										
										
										
											2022-10-22 20:19:52 +02:00
										 |  |  |     query_params = { | 
					
						
							|  |  |  |         'q': query, | 
					
						
							|  |  |  |         'InfiniteScroll': 1, | 
					
						
							|  |  |  |         # to simplify the page count lets use the default of 10 images per page | 
					
						
							| 
									
										
										
										
											2023-09-27 18:24:33 +02:00
										 |  |  |         'first': page * 10 + 1, | 
					
						
							|  |  |  |         'SFX': page, | 
					
						
							|  |  |  |         'form': 'PTFTNR', | 
					
						
							|  |  |  |         'setlang': engine_region.split('-')[0], | 
					
						
							|  |  |  |         'cc': engine_region.split('-')[-1], | 
					
						
							| 
									
										
										
										
											2022-10-22 20:19:52 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if params['time_range']: | 
					
						
							| 
									
										
										
										
											2023-09-27 18:24:33 +02:00
										 |  |  |         query_params['qft'] = time_map.get(params['time_range'], 'interval="9"') | 
					
						
							| 
									
										
										
										
											2022-10-22 20:19:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     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) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-27 18:24:33 +02:00
										 |  |  |     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) | 
					
						
							| 
									
										
										
										
											2022-10-22 20:19:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         thumbnail = None | 
					
						
							| 
									
										
										
										
											2023-09-27 18:24:33 +02:00
										 |  |  |         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') | 
					
						
							| 
									
										
										
										
											2022-10-22 20:19:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         results.append( | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 'url': url, | 
					
						
							|  |  |  |                 'title': title, | 
					
						
							|  |  |  |                 'content': content, | 
					
						
							|  |  |  |                 'img_src': thumbnail, | 
					
						
							|  |  |  |                 '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): | 
					
						
							| 
									
										
										
										
											2023-09-27 18:24:33 +02:00
										 |  |  |     """Fetch languages and regions from Bing-News.""" | 
					
						
							|  |  |  |     # pylint: disable=import-outside-toplevel | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     from searx.engines.bing import fetch_traits as _f | 
					
						
							| 
									
										
										
										
											2022-10-15 21:17:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-27 18:24:33 +02:00
										 |  |  |     _f(engine_traits) | 
					
						
							| 
									
										
										
										
											2022-10-15 21:17:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-27 18:24:33 +02:00
										 |  |  |     # fix market codes not known by bing news: | 
					
						
							| 
									
										
										
										
											2022-10-15 21:17:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-27 18:24:33 +02:00
										 |  |  |     # 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. | 
					
						
							| 
									
										
										
										
											2022-10-15 21:17:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-27 18:24:33 +02:00
										 |  |  |     # HINT: 'en-hk' is the region code it does not indicate the language en!! | 
					
						
							|  |  |  |     engine_traits.regions['zh-CN'] = 'en-hk' |