.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
ansa.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Engine for Ansa, Italy's oldest news agency.
3
4To use this engine add the following entry to your engines
5list in ``settings.yml``:
6
7.. code:: yaml
8
9 - name: ansa
10 engine: ansa
11 shortcut: ans
12 disabled: false
13
14"""
15
16from urllib.parse import urlencode
17from lxml import html
18from searx.result_types import EngineResults, MainResult
19from searx.utils import eval_xpath, eval_xpath_list, extract_text
20
21engine_type = 'online'
22language_support = False
23categories = ['news']
24paging = True
25page_size = 12
26base_url = 'https://www.ansa.it'
27
28time_range_support = True
29time_range_args = {
30 'day': 1,
31 'week': 7,
32 'month': 31,
33 'year': 365,
34}
35# https://www.ansa.it/ricerca/ansait/search.shtml?start=0&any=houthi&periodo=&sort=data%3Adesc
36search_api = 'https://www.ansa.it/ricerca/ansait/search.shtml?'
37
38about = {
39 'website': 'https://www.ansa.it',
40 'wikidata_id': 'Q392934',
41 'official_api_documentation': None,
42 'use_official_api': False,
43 'require_api_key': False,
44 'results': 'HTML',
45 'language': 'it',
46}
47
48
49def request(query, params):
50 query_params = {
51 'any': query,
52 'start': (params['pageno'] - 1) * page_size,
53 'sort': "data:desc",
54 }
55
56 if params['time_range']:
57 query_params['periodo'] = time_range_args.get(params['time_range'])
58
59 params['url'] = search_api + urlencode(query_params)
60 return params
61
62
63def response(resp) -> EngineResults:
64 res = EngineResults()
65 doc = html.fromstring(resp.text)
66
67 for result in eval_xpath_list(doc, "//div[@class='article']"):
68
69 res_obj = MainResult(
70 title=extract_text(eval_xpath(result, "./div[@class='content']/h2[@class='title']/a")),
71 content=extract_text(eval_xpath(result, "./div[@class='content']/div[@class='text']")),
72 url=base_url + extract_text(eval_xpath(result, "./div[@class='content']/h2[@class='title']/a/@href")),
73 )
74
75 thumbnail = extract_text(eval_xpath(result, "./div[@class='image']/a/img/@src"))
76 if thumbnail:
77 res_obj.thumbnail = base_url + thumbnail
78
79 res.append(res_obj)
80
81 return res
EngineResults response(resp)
Definition ansa.py:63
request(query, params)
Definition ansa.py:49