mirror of
https://github.com/searxng/searxng.git
synced 2026-07-17 21:41:24 +00:00
Refactor engines that parse ISO 8601 dates with strptime to use
fromisoformat instead. In most cases this is a direct replacement of
strptime(text, "format") with fromisoformat(text).
For engines where the source has a trailing "Z" that strptime consumed
as a literal (e.g. "%Y-%m-%dT%H:%M:%S.%fZ" in huggingface.py), add
rstrip("Z") to keep the output naive and preserve the existing behavior.
In sogou.py the date is extracted with a regular expression, which can
yield strings like "2026-7-11". strptime accepts this via its format
string, but fromisoformat does not. To preserve the existing behavior
and satisfy the format fromisoformat expects, add zero-padding for the
month and day.
Closes: #6098
---------
Signed-off-by: OneVth <onebrotravel@gmail.com>
59 lines
1.7 KiB
Python
59 lines
1.7 KiB
Python
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
"""Tootfinder (social media)"""
|
|
|
|
from datetime import datetime
|
|
from json import loads
|
|
from searx.utils import html_to_text
|
|
|
|
about = {
|
|
'website': "https://www.tootfinder.ch",
|
|
'official_api_documentation': "https://wiki.tootfinder.ch/index.php?name=the-tootfinder-rest-api",
|
|
'use_official_api': True,
|
|
'require_api_key': False,
|
|
'results': "JSON",
|
|
}
|
|
categories = ['social media']
|
|
|
|
base_url = "https://www.tootfinder.ch"
|
|
|
|
|
|
def request(query, params):
|
|
params['url'] = f"{base_url}/rest/api/search/{query}"
|
|
return params
|
|
|
|
|
|
def response(resp):
|
|
results = []
|
|
|
|
# the API of tootfinder has an issue that errors on server side are appended to the API response as HTML
|
|
# thus we're only looking for the line that contains the actual json data and ignore everything else
|
|
json_str = ""
|
|
for line in resp.text.split("\n"):
|
|
if line.startswith("[{"):
|
|
json_str = line
|
|
break
|
|
|
|
for result in loads(json_str):
|
|
thumbnail = None
|
|
|
|
attachments = result.get('media_attachments', [])
|
|
images = [attachment['preview_url'] for attachment in attachments if attachment['type'] == 'image']
|
|
if len(images) > 0:
|
|
thumbnail = images[0]
|
|
|
|
title = result.get('card', {}).get('title')
|
|
if not title:
|
|
title = html_to_text(result['content'])[:75]
|
|
|
|
results.append(
|
|
{
|
|
'url': result['url'],
|
|
'title': title,
|
|
'content': html_to_text(result['content']),
|
|
'thumbnail': thumbnail,
|
|
'publishedDate': datetime.fromisoformat(result['created_at']),
|
|
}
|
|
)
|
|
|
|
return results
|