.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
tootfinder.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Tootfinder (social media)
3"""
4
5from datetime import datetime
6from json import loads
7from searx.utils import html_to_text
8
9about = {
10 'website': "https://www.tootfinder.ch",
11 'official_api_documentation': "https://wiki.tootfinder.ch/index.php?name=the-tootfinder-rest-api",
12 'use_official_api': True,
13 'require_api_key': False,
14 'results': "JSON",
15}
16categories = ['social media']
17
18base_url = "https://www.tootfinder.ch"
19
20
21def request(query, params):
22 params['url'] = f"{base_url}/rest/api/search/{query}"
23 return params
24
25
26def response(resp):
27 results = []
28
29 # the API of tootfinder has an issue that errors on server side are appended to the API response as HTML
30 # thus we're only looking for the line that contains the actual json data and ignore everything else
31 json_str = ""
32 for line in resp.text.split("\n"):
33 if line.startswith("[{"):
34 json_str = line
35 break
36
37 for result in loads(json_str):
38 thumbnail = None
39
40 attachments = result.get('media_attachments', [])
41 images = [attachment['preview_url'] for attachment in attachments if attachment['type'] == 'image']
42 if len(images) > 0:
43 thumbnail = images[0]
44
45 title = result.get('card', {}).get('title')
46 if not title:
47 title = html_to_text(result['content'])[:75]
48
49 results.append(
50 {
51 'url': result['url'],
52 'title': title,
53 'content': html_to_text(result['content']),
54 'thumbnail': thumbnail,
55 'publishedDate': datetime.strptime(result['created_at'], '%Y-%m-%d %H:%M:%S'),
56 }
57 )
58
59 return results
request(query, params)
Definition tootfinder.py:21