recoll.py 3.9 KB

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