From de887c6347db4c810f7d1e11c77633340d4e2fbd Mon Sep 17 00:00:00 2001 From: Alexandre Flament Date: Thu, 26 Nov 2020 15:38:07 +0100 Subject: [PATCH] [mod] bing_news: use eval_xpath_getindex remove unused function searx.utils.list_get --- searx/engines/bing_news.py | 13 +++++++------ searx/utils.py | 18 ------------------ 2 files changed, 7 insertions(+), 24 deletions(-) diff --git a/searx/engines/bing_news.py b/searx/engines/bing_news.py index 5489c7549..b95def48b 100644 --- a/searx/engines/bing_news.py +++ b/searx/engines/bing_news.py @@ -15,7 +15,8 @@ from datetime import datetime from dateutil import parser from urllib.parse import urlencode, urlparse, parse_qsl from lxml import etree -from searx.utils import list_get, match_language +from lxml.etree import XPath +from searx.utils import match_language, eval_xpath_getindex from searx.engines.bing import language_aliases from searx.engines.bing import _fetch_supported_languages, supported_languages_url # NOQA # pylint: disable=unused-import @@ -94,12 +95,12 @@ def response(resp): # parse results 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, '') + url = url_cleanup(eval_xpath_getindex(item, './link/text()', 0, default=None)) + title = eval_xpath_getindex(item, './title/text()', 0, default=url) + content = eval_xpath_getindex(item, './description/text()', 0, default='') # publishedDate - publishedDate = list_get(item.xpath('./pubDate/text()'), 0) + publishedDate = eval_xpath_getindex(item, './pubDate/text()', 0, default=None) try: publishedDate = parser.parse(publishedDate, dayfirst=False) except TypeError: @@ -108,7 +109,7 @@ def response(resp): publishedDate = datetime.now() # thumbnail - thumbnail = list_get(item.xpath('./News:Image/text()', namespaces=ns), 0) + thumbnail = eval_xpath_getindex(item, XPath('./News:Image/text()', namespaces=ns), 0, default=None) if thumbnail is not None: thumbnail = image_url_cleanup(thumbnail) diff --git a/searx/utils.py b/searx/utils.py index 80cb556fd..057e9d004 100644 --- a/searx/utils.py +++ b/searx/utils.py @@ -269,24 +269,6 @@ def dict_subset(d, properties): return result -def list_get(a_list, index, default=None): - """Get element in list or default value - Examples: - >>> list_get(['A', 'B', 'C'], 0) - 'A' - >>> list_get(['A', 'B', 'C'], 3) - None - >>> list_get(['A', 'B', 'C'], 3, 'default') - 'default' - >>> list_get(['A', 'B', 'C'], -1) - 'C' - """ - if len(a_list) > index: - return a_list[index] - else: - return default - - def get_torrent_size(filesize, filesize_multiplier): """