.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
mastodon.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Mastodon_ is an open source alternative to large social media platforms like
3Twitter/X, Facebook, ...
4
5Since it's federated and self-hostable, there's a large amount of available
6instances, which can be chosen instead by modifying ``base_url``.
7
8We use their official API_ for searching, but unfortunately, their Search API_
9forbids pagination without OAuth.
10
11That's why we use tootfinder.ch for finding posts, which doesn't support searching
12for users, accounts or other types of content on Mastodon however.
13
14.. _Mastodon: https://mastodon.social
15.. _API: https://docs.joinmastodon.org/api/
16
17"""
18
19from urllib.parse import urlencode
20from datetime import datetime
21
22about = {
23 "website": 'https://joinmastodon.org/',
24 "wikidata_id": 'Q27986619',
25 "official_api_documentation": 'https://docs.joinmastodon.org/api/',
26 "use_official_api": True,
27 "require_api_key": False,
28 "results": 'JSON',
29}
30categories = ['social media']
31
32base_url = "https://mastodon.social"
33mastodon_type = "accounts"
34
35# https://github.com/searxng/searxng/pull/2857#issuecomment-1741713999
36page_size = 40
37
38
39def request(query, params):
40 args = {
41 'q': query,
42 'resolve': 'false',
43 'type': mastodon_type,
44 'limit': page_size,
45 }
46 params['url'] = f"{base_url}/api/v2/search?{urlencode(args)}"
47 return params
48
49
50def response(resp):
51 results = []
52
53 json = resp.json()
54
55 for result in json[mastodon_type]:
56 if mastodon_type == "accounts":
57 results.append(
58 {
59 'url': result['uri'],
60 'title': result['username'] + f" ({result['followers_count']} followers)",
61 'content': result['note'],
62 'thumbnail': result.get('avatar'),
63 'publishedDate': datetime.strptime(result['created_at'][:10], "%Y-%m-%d"),
64 }
65 )
66 elif mastodon_type == "hashtags":
67 uses_count = sum(int(entry['uses']) for entry in result['history'])
68 user_count = sum(int(entry['accounts']) for entry in result['history'])
69 results.append(
70 {
71 'url': result['url'],
72 'title': result['name'],
73 'content': f"Hashtag has been used {uses_count} times by {user_count} different users",
74 }
75 )
76 else:
77 raise ValueError(f"Unsupported mastodon type: {mastodon_type}")
78
79 return results
request(query, params)
Definition mastodon.py:39