json_engine.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """The JSON engine is a *generic* engine with which it is possible to configure
  3. engines in the settings.
  4. Configuration
  5. =============
  6. Request:
  7. - :py:obj:`search_url`
  8. - :py:obj:`method`
  9. - :py:obj:`request_body`
  10. - :py:obj:`cookies`
  11. - :py:obj:`headers`
  12. Paging:
  13. - :py:obj:`paging`
  14. - :py:obj:`page_size`
  15. - :py:obj:`first_page_num`
  16. Response:
  17. - :py:obj:`title_html_to_text`
  18. - :py:obj:`content_html_to_text`
  19. JSON query:
  20. - :py:obj:`results_query`
  21. - :py:obj:`url_query`
  22. - :py:obj:`url_prefix`
  23. - :py:obj:`title_query`
  24. - :py:obj:`content_query`
  25. - :py:obj:`suggestion_query`
  26. Example
  27. =======
  28. Here is a simple example of a JSON engine configure in the :ref:`settings
  29. engine` section, further read :ref:`engines-dev`.
  30. .. code:: yaml
  31. - name : mdn
  32. engine : json_engine
  33. paging : True
  34. search_url : https://developer.mozilla.org/api/v1/search?q={query}&page={pageno}
  35. results_query : documents
  36. url_query : mdn_url
  37. url_prefix : https://developer.mozilla.org
  38. title_query : title
  39. content_query : summary
  40. Implementations
  41. ===============
  42. """
  43. from collections.abc import Iterable
  44. from json import loads
  45. from urllib.parse import urlencode
  46. from searx.utils import to_string, html_to_text
  47. search_url = None
  48. """
  49. Search URL of the engine. Example::
  50. https://example.org/?search={query}&page={pageno}
  51. Replacements are:
  52. ``{query}``:
  53. Search terms from user.
  54. ``{pageno}``:
  55. Page number if engine supports paging :py:obj:`paging`
  56. """
  57. method = 'GET'
  58. '''Some engines might require to do POST requests for search.'''
  59. request_body = ''
  60. '''The body of the request. This can only be used if different :py:obj:`method`
  61. is set, e.g. ``POST``. For formatting see the documentation of :py:obj:`search_url`.
  62. Note: Curly brackets which aren't encapsulating a replacement placeholder
  63. must be escaped by doubling each ``{`` and ``}``.
  64. .. code:: yaml
  65. request_body: >-
  66. {{
  67. "search": "{query}",
  68. "page": {pageno},
  69. "extra": {{
  70. "time_range": {time_range},
  71. "rating": "{safe_search}"
  72. }}
  73. }}
  74. '''
  75. cookies = {}
  76. '''Some engines might offer different result based on cookies.
  77. Possible use-case: To set safesearch cookie.'''
  78. headers = {}
  79. '''Some engines might offer different result based on cookies or headers.
  80. Possible use-case: To set safesearch cookie or header to moderate.'''
  81. paging = False
  82. '''Engine supports paging [True or False].'''
  83. page_size = 1
  84. '''Number of results on each page. Only needed if the site requires not a page
  85. number, but an offset.'''
  86. first_page_num = 1
  87. '''Number of the first page (usually 0 or 1).'''
  88. results_query = ''
  89. '''JSON query for the list of result items.
  90. The query string is a slash `/` separated path of JSON key names.
  91. Array entries can be specified using the index or can be omitted entirely,
  92. in which case each entry is considered -
  93. most implementations will default to the first entry in this case.
  94. '''
  95. url_query = None
  96. '''JSON query of result's ``url``. For the query string documentation see :py:obj:`results_query`'''
  97. url_prefix = ""
  98. '''String to prepend to the result's ``url``.'''
  99. title_query = None
  100. '''JSON query of result's ``title``. For the query string documentation see :py:obj:`results_query`'''
  101. content_query = None
  102. '''JSON query of result's ``content``. For the query string documentation see :py:obj:`results_query`'''
  103. suggestion_query = ''
  104. '''JSON query of result's ``suggestion``. For the query string documentation see :py:obj:`results_query`'''
  105. title_html_to_text = False
  106. '''Extract text from a HTML title string'''
  107. content_html_to_text = False
  108. '''Extract text from a HTML content string'''
  109. def iterate(iterable):
  110. if isinstance(iterable, dict):
  111. items = iterable.items()
  112. else:
  113. items = enumerate(iterable)
  114. for index, value in items:
  115. yield str(index), value
  116. def is_iterable(obj):
  117. if isinstance(obj, str):
  118. return False
  119. return isinstance(obj, Iterable)
  120. def parse(query): # pylint: disable=redefined-outer-name
  121. q = [] # pylint: disable=invalid-name
  122. for part in query.split('/'):
  123. if part == '':
  124. continue
  125. q.append(part)
  126. return q
  127. def do_query(data, q): # pylint: disable=invalid-name
  128. ret = []
  129. if not q:
  130. return ret
  131. qkey = q[0]
  132. for key, value in iterate(data):
  133. if len(q) == 1:
  134. if key == qkey:
  135. ret.append(value)
  136. elif is_iterable(value):
  137. ret.extend(do_query(value, q))
  138. else:
  139. if not is_iterable(value):
  140. continue
  141. if key == qkey:
  142. ret.extend(do_query(value, q[1:]))
  143. else:
  144. ret.extend(do_query(value, q))
  145. return ret
  146. def query(data, query_string):
  147. q = parse(query_string)
  148. return do_query(data, q)
  149. def request(query, params): # pylint: disable=redefined-outer-name
  150. '''Build request parameters (see :ref:`engine request`).'''
  151. fp = {'query': urlencode({'q': query})[2:]} # pylint: disable=invalid-name
  152. if paging and search_url.find('{pageno}') >= 0:
  153. fp['pageno'] = (params['pageno'] - 1) * page_size + first_page_num
  154. params['cookies'].update(cookies)
  155. params['headers'].update(headers)
  156. params['url'] = search_url.format(**fp)
  157. params['method'] = method
  158. if request_body:
  159. # don't url-encode the query if it's in the request body
  160. fp['query'] = query
  161. params['data'] = request_body.format(**fp)
  162. return params
  163. def identity(arg):
  164. return arg
  165. def response(resp):
  166. '''Scrap *results* from the response (see :ref:`engine results`).'''
  167. results = []
  168. if not resp.text:
  169. return results
  170. json = loads(resp.text)
  171. title_filter = html_to_text if title_html_to_text else identity
  172. content_filter = html_to_text if content_html_to_text else identity
  173. if results_query:
  174. rs = query(json, results_query) # pylint: disable=invalid-name
  175. if not rs:
  176. return results
  177. for result in rs[0]:
  178. try:
  179. url = query(result, url_query)[0]
  180. title = query(result, title_query)[0]
  181. except: # pylint: disable=bare-except
  182. continue
  183. try:
  184. content = query(result, content_query)[0]
  185. except: # pylint: disable=bare-except
  186. content = ""
  187. results.append(
  188. {
  189. 'url': url_prefix + to_string(url),
  190. 'title': title_filter(to_string(title)),
  191. 'content': content_filter(to_string(content)),
  192. }
  193. )
  194. else:
  195. for result in json:
  196. url = query(result, url_query)[0]
  197. title = query(result, title_query)[0]
  198. content = query(result, content_query)[0]
  199. results.append(
  200. {
  201. 'url': url_prefix + to_string(url),
  202. 'title': title_filter(to_string(title)),
  203. 'content': content_filter(to_string(content)),
  204. }
  205. )
  206. if not suggestion_query:
  207. return results
  208. for suggestion in query(json, suggestion_query):
  209. results.append({'suggestion': suggestion})
  210. return results