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