mirror of
https://github.com/searxng/searxng.git
synced 2024-06-11 01:39:35 +00:00
We have been using a static type checker (pyright) for a long time, but its check was not yet a prerequisite for passing the quality gate. It was checked in the CI, but the error messages were only logged. As is always the case in life, with checks that you have to do but which have no consequences; you neglect them :-) We didn't activate the checks back then because we (even today) have too much monkey patching in our code (not only in the engines, httpx and others objects are also affected). We want to replace monkey patching with clear interfaces for a long time, the basis for this is increased typing and we can only achieve this if we make type checking an integral part of the quality gate. This PR activates the type check; in order to pass the check, a few typings were corrected in the code, but most type inconsistencies were deactivated via inline comments. This was particularly necessary in places where the code uses properties that stick to the objects (monkey patching). The sticking of properties only happens in a few places, but the access to these properties extends over the entire code, which is why there are many `# type: ignore` markers in the code ... which we will hopefully be able to remove again successively in the future. Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
75 lines
1.8 KiB
Python
75 lines
1.8 KiB
Python
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
"""Ask.com"""
|
|
|
|
from urllib.parse import urlencode
|
|
import dateutil.parser
|
|
from lxml import html
|
|
from searx import utils
|
|
|
|
# Metadata
|
|
about = {
|
|
"website": "https://www.ask.com/",
|
|
"wikidata_id": 'Q847564',
|
|
"official_api_documentation": None,
|
|
"use_official_api": False,
|
|
"require_api_key": False,
|
|
"results": "HTML",
|
|
}
|
|
|
|
# Engine Configuration
|
|
categories = ['general']
|
|
paging = True
|
|
max_page = 5
|
|
|
|
# Base URL
|
|
base_url = "https://www.ask.com/web"
|
|
|
|
|
|
def request(query, params):
|
|
|
|
query_params = {
|
|
"q": query,
|
|
"page": params["pageno"],
|
|
}
|
|
|
|
params["url"] = f"{base_url}?{urlencode(query_params)}"
|
|
return params
|
|
|
|
|
|
def response(resp):
|
|
|
|
start_tag = 'window.MESON.initialState = {'
|
|
end_tag = '}};'
|
|
|
|
dom = html.fromstring(resp.text)
|
|
script = utils.eval_xpath_getindex(dom, '//script', 0, default=None).text
|
|
|
|
pos = script.index(start_tag) + len(start_tag) - 1
|
|
script = script[pos:]
|
|
pos = script.index(end_tag) + len(end_tag) - 1
|
|
script = script[:pos]
|
|
|
|
json_resp = utils.js_variable_to_python(script)
|
|
|
|
results = []
|
|
|
|
for item in json_resp['search']['webResults']['results']:
|
|
|
|
pubdate_original = item.get('pubdate_original')
|
|
if pubdate_original:
|
|
pubdate_original = dateutil.parser.parse(pubdate_original)
|
|
metadata = [item.get(field) for field in ['category_l1', 'catsy'] if item.get(field)]
|
|
|
|
results.append(
|
|
{
|
|
"url": item['url'].split('&ueid')[0],
|
|
"title": item['title'],
|
|
"content": item['abstract'],
|
|
"publishedDate": pubdate_original,
|
|
# "img_src": item.get('image_url') or None, # these are not thumbs / to large
|
|
"metadata": ' | '.join(metadata),
|
|
}
|
|
)
|
|
|
|
return results
|