on master help sign in
[mod] json_engine: add content_html_to_text and title_html_to_text

Some JSON API returns HTML in either in the HTML or the content. This commit adds two new parameters to the json_engine: content_html_to_text and title_html_to_text, False by default. If True, then the searx.utils.html_to_text removes the HTML tags. Update crossref, openairedatasets and openairepublications engines

commitff84a1af35c04855ae6e5c2463b978111d8c9fb1 authorAlexandre Flament <alex@al-f.net> dateWed 10 Feb 2021, 16:40 (6 years ago) parent436d366
2 files changed+19−5
searx/engines/json_engine.py+15−5 searx/settings.yml+4
searx/engines/json_engine.py+15−5
@@ -3,13 +3,15 @@
33from collections.abc import Iterable
44from json import loads
55from urllib.parse import urlencode
6−from searx.utils import to_string
6+from searx.utils import to_string, html_to_text
77
88
99search_url = None
1010url_query = None
1111content_query = None
1212title_query = None
13+content_html_to_text = False
14+title_html_to_text = False
1315paging = False
1416suggestion_query = ''
1517results_query = ''
@@ -92,9 +94,17 @@ def request(query, params):
9294 return params
9395
9496
97+def identity(arg):
98+ return arg
99+
100+
95101def response(resp):
96102 results = []
97103 json = loads(resp.text)
104+
105+ title_filter = html_to_text if title_html_to_text else identity
106+ content_filter = html_to_text if content_html_to_text else identity
107+
98108 if results_query:
99109 rs = query(json, results_query)
100110 if not len(rs):
@@ -111,8 +121,8 @@ def response(resp):
111121 content = ""
112122 results.append({
113123 'url': to_string(url),
114− 'title': to_string(title),
115− 'content': to_string(content),
124+ 'title': title_filter(to_string(title)),
125+ 'content': content_filter(to_string(content)),
116126 })
117127 else:
118128 for url, title, content in zip(
@@ -122,8 +132,8 @@ def response(resp):
122132 ):
123133 results.append({
124134 'url': to_string(url),
125− 'title': to_string(title),
126− 'content': to_string(content),
135+ 'title': title_filter(to_string(title)),
136+ 'content': content_filter(to_string(content)),
127137 })
128138
129139 if not suggestion_query:
searx/settings.yml+4
@@ -267,7 +267,9 @@ engines:
267267 search_url : https://search.crossref.org/dois?q={query}&page={pageno}
268268 url_query : doi
269269 title_query : title
270+ title_html_to_text: True
270271 content_query : fullCitation
272+ content_html_to_text: True
271273 categories : science
272274 shortcut : cr
273275 about:
@@ -757,6 +759,7 @@ engines:
757759 url_query : metadata/oaf:entity/oaf:result/children/instance/webresource/url/$
758760 title_query : metadata/oaf:entity/oaf:result/title/$
759761 content_query : metadata/oaf:entity/oaf:result/description/$
762+ content_html_to_text: True
760763 categories : science
761764 shortcut : oad
762765 timeout: 5.0
@@ -776,6 +779,7 @@ engines:
776779 url_query : metadata/oaf:entity/oaf:result/children/instance/webresource/url/$
777780 title_query : metadata/oaf:entity/oaf:result/title/$
778781 content_query : metadata/oaf:entity/oaf:result/description/$
782+ content_html_to_text: True
779783 categories : science
780784 shortcut : oap
781785 timeout: 5.0