.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
ask.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Ask.com"""
3
4from urllib.parse import urlencode
5import dateutil
6from lxml import html
7from searx import utils
8
9# Metadata
10about = {
11 "website": "https://www.ask.com/",
12 "wikidata_id": 'Q847564',
13 "official_api_documentation": None,
14 "use_official_api": False,
15 "require_api_key": False,
16 "results": "HTML",
17}
18
19# Engine Configuration
20categories = ['general']
21paging = True
22max_page = 5
23
24# Base URL
25base_url = "https://www.ask.com/web"
26
27
28def request(query, params):
29
30 query_params = {
31 "q": query,
32 "page": params["pageno"],
33 }
34
35 params["url"] = f"{base_url}?{urlencode(query_params)}"
36 return params
37
38
39def response(resp):
40
41 start_tag = 'window.MESON.initialState = {'
42 end_tag = '}};'
43
44 dom = html.fromstring(resp.text)
45 script = utils.eval_xpath_getindex(dom, '//script', 0, default=None).text
46
47 pos = script.index(start_tag) + len(start_tag) - 1
48 script = script[pos:]
49 pos = script.index(end_tag) + len(end_tag) - 1
50 script = script[:pos]
51
52 json_resp = utils.js_variable_to_python(script)
53
54 results = []
55
56 for item in json_resp['search']['webResults']['results']:
57
58 pubdate_original = item.get('pubdate_original')
59 if pubdate_original:
60 pubdate_original = dateutil.parser.parse(pubdate_original)
61 metadata = [item.get(field) for field in ['category_l1', 'catsy'] if item.get(field)]
62
63 results.append(
64 {
65 "url": item['url'].split('&ueid')[0],
66 "title": item['title'],
67 "content": item['abstract'],
68 "publishedDate": pubdate_original,
69 # "thumbnail": item.get('image_url') or None, # these are not thumbs / to large
70 "metadata": ' | '.join(metadata),
71 }
72 )
73
74 return results
request(query, params)
Definition ask.py:28
response(resp)
Definition ask.py:39