.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
lemmy.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""This engine uses the Lemmy API (https://lemmy.ml/api/v3/search), which is
3documented at `lemmy-js-client`_ / `Interface Search`_. Since Lemmy is
4federated, results are from many different, independent lemmy instances, and not
5only the official one.
6
7.. _lemmy-js-client: https://join-lemmy.org/api/modules.html
8.. _Interface Search: https://join-lemmy.org/api/interfaces/Search.html
9
10Configuration
11=============
12
13The engine has the following additional settings:
14
15- :py:obj:`base_url`
16- :py:obj:`lemmy_type`
17
18This implementation is used by different lemmy engines in the :ref:`settings.yml
19<settings engine>`:
20
21.. code:: yaml
22
23 - name: lemmy communities
24 lemmy_type: Communities
25 ...
26 - name: lemmy users
27 lemmy_type: Users
28 ...
29 - name: lemmy posts
30 lemmy_type: Posts
31 ...
32 - name: lemmy comments
33 lemmy_type: Comments
34 ...
35
36Implementations
37===============
38
39"""
40
41from datetime import datetime
42from urllib.parse import urlencode
43
44from flask_babel import gettext
45
46from searx.utils import markdown_to_text
47
48about = {
49 "website": 'https://lemmy.ml/',
50 "wikidata_id": 'Q84777032',
51 "official_api_documentation": "https://join-lemmy.org/api/",
52 "use_official_api": True,
53 "require_api_key": False,
54 "results": 'JSON',
55}
56paging = True
57categories = ['social media']
58
59base_url = "https://lemmy.ml/"
60"""By default, https://lemmy.ml is used for providing the results. If you want
61to use a different lemmy instance, you can specify ``base_url``.
62"""
63
64lemmy_type = "Communities"
65"""Any of ``Communities``, ``Users``, ``Posts``, ``Comments``"""
66
67
68def request(query, params):
69 args = {
70 'q': query,
71 'page': params['pageno'],
72 'type_': lemmy_type,
73 }
74
75 params['url'] = f"{base_url}api/v3/search?{urlencode(args)}"
76 return params
77
78
80 results = []
81
82 for result in json["communities"]:
83 counts = result['counts']
84 metadata = (
85 f"{gettext('subscribers')}: {counts.get('subscribers', 0)}"
86 f" | {gettext('posts')}: {counts.get('posts', 0)}"
87 f" | {gettext('active users')}: {counts.get('users_active_half_year', 0)}"
88 )
89 results.append(
90 {
91 'url': result['community']['actor_id'],
92 'title': result['community']['title'],
93 'content': markdown_to_text(result['community'].get('description', '')),
94 'img_src': result['community'].get('icon', result['community'].get('banner')),
95 'publishedDate': datetime.strptime(counts['published'][:19], '%Y-%m-%dT%H:%M:%S'),
96 'metadata': metadata,
97 }
98 )
99 return results
100
101
102def _get_users(json):
103 results = []
104
105 for result in json["users"]:
106 results.append(
107 {
108 'url': result['person']['actor_id'],
109 'title': result['person']['name'],
110 'content': markdown_to_text(result['person'].get('bio', '')),
111 }
112 )
113
114 return results
115
116
117def _get_posts(json):
118 results = []
119
120 for result in json["posts"]:
121 user = result['creator'].get('display_name', result['creator']['name'])
122
123 img_src = None
124 if result['post'].get('thumbnail_url'):
125 img_src = result['post']['thumbnail_url'] + '?format=webp&thumbnail=208'
126
127 metadata = (
128 f"&#x25B2; {result['counts']['upvotes']} &#x25BC; {result['counts']['downvotes']}"
129 f" | {gettext('user')}: {user}"
130 f" | {gettext('comments')}: {result['counts']['comments']}"
131 f" | {gettext('community')}: {result['community']['title']}"
132 )
133
134 content = result['post'].get('body', '').strip()
135 if content:
136 content = markdown_to_text(content)
137
138 results.append(
139 {
140 'url': result['post']['ap_id'],
141 'title': result['post']['name'],
142 'content': content,
143 'img_src': img_src,
144 'publishedDate': datetime.strptime(result['post']['published'][:19], '%Y-%m-%dT%H:%M:%S'),
145 'metadata': metadata,
146 }
147 )
148
149 return results
150
151
153 results = []
154
155 for result in json["comments"]:
156 user = result['creator'].get('display_name', result['creator']['name'])
157
158 content = result['comment'].get('content', '').strip()
159 if content:
160 content = markdown_to_text(content)
161
162 metadata = (
163 f"&#x25B2; {result['counts']['upvotes']} &#x25BC; {result['counts']['downvotes']}"
164 f" | {gettext('user')}: {user}"
165 f" | {gettext('community')}: {result['community']['title']}"
166 )
167
168 results.append(
169 {
170 'url': result['comment']['ap_id'],
171 'title': result['post']['name'],
172 'content': markdown_to_text(result['comment']['content']),
173 'publishedDate': datetime.strptime(result['comment']['published'][:19], '%Y-%m-%dT%H:%M:%S'),
174 'metadata': metadata,
175 }
176 )
177
178 return results
179
180
181def response(resp):
182 json = resp.json()
183
184 if lemmy_type == "Communities":
185 return _get_communities(json)
186
187 if lemmy_type == "Users":
188 return _get_users(json)
189
190 if lemmy_type == "Posts":
191 return _get_posts(json)
192
193 if lemmy_type == "Comments":
194 return _get_comments(json)
195
196 raise ValueError(f"Unsupported lemmy type: {lemmy_type}")
_get_comments(json)
Definition lemmy.py:152
request(query, params)
Definition lemmy.py:68
_get_communities(json)
Definition lemmy.py:79