[feat] engine: add engine for italian press agency ansa

This commit is contained in:
Tommaso Colella 2025-03-29 19:54:48 +00:00 committed by Markus Heiser
parent 391bb1268d
commit 0c0df8ae6e
2 changed files with 86 additions and 0 deletions

81
searx/engines/ansa.py Normal file
View File

@ -0,0 +1,81 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Engine for Ansa, Italy's oldest news agency.
To use this engine add the following entry to your engines
list in ``settings.yml``:
.. code:: yaml
- name: ansa
engine: ansa
shortcut: ans
disabled: false
"""
from urllib.parse import urlencode
from lxml import html
from searx.result_types import EngineResults, MainResult
from searx.utils import eval_xpath, eval_xpath_list, extract_text
engine_type = 'online'
language_support = False
categories = ['news']
paging = True
page_size = 12
base_url = 'https://www.ansa.it'
time_range_support = True
time_range_args = {
'day': 1,
'week': 7,
'month': 31,
'year': 365,
}
# https://www.ansa.it/ricerca/ansait/search.shtml?start=0&any=houthi&periodo=&sort=data%3Adesc
search_api = 'https://www.ansa.it/ricerca/ansait/search.shtml?'
about = {
'website': 'https://www.ansa.it',
'wikidata_id': 'Q392934',
'official_api_documentation': None,
'use_official_api': False,
'require_api_key': False,
'results': 'HTML',
'language': 'it',
}
def request(query, params):
query_params = {
'any': query,
'start': (params['pageno'] - 1) * page_size,
'sort': "data:desc",
}
if params['time_range']:
query_params['periodo'] = time_range_args.get(params['time_range'])
params['url'] = search_api + urlencode(query_params)
return params
def response(resp) -> EngineResults:
res = EngineResults()
doc = html.fromstring(resp.text)
for result in eval_xpath_list(doc, "//div[@class='article']"):
res_obj = MainResult(
title=extract_text(eval_xpath(result, "./div[@class='content']/h2[@class='title']/a")),
content=extract_text(eval_xpath(result, "./div[@class='content']/div[@class='text']")),
url=base_url + extract_text(eval_xpath(result, "./div[@class='content']/h2[@class='title']/a/@href")),
)
thumbnail = extract_text(eval_xpath(result, "./div[@class='image']/a/img/@src"))
if thumbnail:
res_obj.thumbnail = base_url + thumbnail
res.append(res_obj)
return res

View File

@ -433,6 +433,11 @@ engines:
disabled: true
shortcut: aa
- name: ansa
engine: ansa
shortcut: ans
disabled: false
# - name: annas articles
# engine: annas_archive
# shortcut: aaa