recoll.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """.. sidebar:: info
  3. - `Recoll <https://www.lesbonscomptes.com/recoll/>`_
  4. - `recoll-webui <https://framagit.org/medoc92/recollwebui.git>`_
  5. - :origin:`searx/engines/recoll.py`
  6. Recoll_ is a desktop full-text search tool based on Xapian. By itself Recoll_
  7. does not offer WEB or API access, this can be achieved using recoll-webui_
  8. Configuration
  9. =============
  10. You must configure the following settings:
  11. ``base_url``:
  12. Location where recoll-webui can be reached.
  13. ``mount_prefix``:
  14. Location where the file hierarchy is mounted on your *local* filesystem.
  15. ``dl_prefix``:
  16. Location where the file hierarchy as indexed by recoll can be reached.
  17. ``search_dir``:
  18. Part of the indexed file hierarchy to be search, if empty the full domain is
  19. searched.
  20. Example
  21. =======
  22. Scenario:
  23. #. Recoll indexes a local filesystem mounted in ``/export/documents/reference``,
  24. #. the Recoll search interface can be reached at https://recoll.example.org/ and
  25. #. the contents of this filesystem can be reached though https://download.example.org/reference
  26. .. code:: yaml
  27. base_url: https://recoll.example.org/
  28. mount_prefix: /export/documents
  29. dl_prefix: https://download.example.org
  30. search_dir: ''
  31. Implementations
  32. ===============
  33. """
  34. from datetime import date, timedelta
  35. from json import loads
  36. from urllib.parse import urlencode, quote
  37. # about
  38. about = {
  39. "website": None,
  40. "wikidata_id": 'Q15735774',
  41. "official_api_documentation": 'https://www.lesbonscomptes.com/recoll/',
  42. "use_official_api": True,
  43. "require_api_key": False,
  44. "results": 'JSON',
  45. }
  46. # engine dependent config
  47. paging = True
  48. time_range_support = True
  49. # parameters from settings.yml
  50. base_url = None
  51. search_dir = ''
  52. mount_prefix = None
  53. dl_prefix = None
  54. # embedded
  55. embedded_url = '<{ttype} controls height="166px" ' + 'src="{url}" type="{mtype}"></{ttype}>'
  56. # helper functions
  57. def get_time_range(time_range):
  58. sw = {'day': 1, 'week': 7, 'month': 30, 'year': 365} # pylint: disable=invalid-name
  59. offset = sw.get(time_range, 0)
  60. if not offset:
  61. return ''
  62. return (date.today() - timedelta(days=offset)).isoformat()
  63. # do search-request
  64. def request(query, params):
  65. search_after = get_time_range(params['time_range'])
  66. search_url = base_url + 'json?{query}&highlight=0'
  67. params['url'] = search_url.format(
  68. query=urlencode({'query': query, 'page': params['pageno'], 'after': search_after, 'dir': search_dir})
  69. )
  70. return params
  71. # get response from search-request
  72. def response(resp):
  73. results = []
  74. response_json = loads(resp.text)
  75. if not response_json:
  76. return []
  77. for result in response_json.get('results', []):
  78. title = result['label']
  79. url = result['url'].replace('file://' + mount_prefix, dl_prefix)
  80. content = '{}'.format(result['snippet'])
  81. # append result
  82. item = {'url': url, 'title': title, 'content': content, 'template': 'files.html'}
  83. if result['size']:
  84. item['size'] = int(result['size'])
  85. for parameter in ['filename', 'abstract', 'author', 'mtype', 'time']:
  86. if result[parameter]:
  87. item[parameter] = result[parameter]
  88. # facilitate preview support for known mime types
  89. if 'mtype' in result and '/' in result['mtype']:
  90. (mtype, subtype) = result['mtype'].split('/')
  91. item['mtype'] = mtype
  92. item['subtype'] = subtype
  93. if mtype in ['audio', 'video']:
  94. item['embedded'] = embedded_url.format(
  95. ttype=mtype, url=quote(url.encode('utf8'), '/:'), mtype=result['mtype']
  96. )
  97. if mtype in ['image'] and subtype in ['bmp', 'gif', 'jpeg', 'png']:
  98. item['thumbnail'] = url
  99. results.append(item)
  100. if 'nres' in response_json:
  101. results.append({'number_of_results': response_json['nres']})
  102. return results