diff options
author | Markus Heiser <markus.heiser@darmarit.de> | 2024-01-13 14:02:13 +0100 |
---|---|---|
committer | Markus Heiser <markus.heiser@darmarIT.de> | 2024-01-15 19:23:26 +0100 |
commit | a2c269bbac95879be02423cb834833654e22521b (patch) | |
tree | 52054ccbee6d16eafb096cb1787191741801058f | |
parent | 0a78f59aba6cf57e6eaf50b34a3c949201ce5175 (diff) | |
download | searxng-a2c269bbac95879be02423cb834833654e22521b.tar.gz searxng-a2c269bbac95879be02423cb834833654e22521b.zip |
[mod] presearch: hardening engine's response against KeyErrors
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
-rw-r--r-- | searx/engines/presearch.py | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/searx/engines/presearch.py b/searx/engines/presearch.py index 42429a4fc..747393829 100644 --- a/searx/engines/presearch.py +++ b/searx/engines/presearch.py @@ -113,7 +113,9 @@ def parse_search_query(json_results): value = _strip_leading_strings(value) attributes.append({'label': label, 'value': value}) content = [] - for item in [info['subtitle'], info['description']]: + for item in [info.get('subtitle'), info.get('description')]: + if not item: + continue item = _strip_leading_strings(html_to_text(item)) if item: content.append(item) @@ -135,7 +137,7 @@ def response(resp): json_resp = resp.json() if search_type == 'search': - results = parse_search_query(json_resp['results']) + results = parse_search_query(json_resp.get('results')) elif search_type == 'images': for item in json_resp.get('images', []): @@ -143,9 +145,9 @@ def response(resp): { 'template': 'images.html', 'title': item['title'], - 'url': item['link'], - 'img_src': item['image'], - 'thumbnail_src': item['thumbnail'], + 'url': item.get('link'), + 'img_src': item.get('image'), + 'thumbnail_src': item.get('thumbnail'), } ) @@ -158,7 +160,7 @@ def response(resp): results.append( { 'title': item['title'], - 'url': item['link'], + 'url': item.get('link'), 'content': '', 'metadata': ' / '.join(metadata), 'img_src': item.get('image'), @@ -171,8 +173,8 @@ def response(resp): results.append( { 'title': item['title'], - 'url': item['link'], - 'content': item['description'], + 'url': item.get('link'), + 'content': item.get('description', ''), 'metadata': ' / '.join(metadata), 'img_src': item.get('image'), } |