forked from Ponysearch/Ponysearch
[fix] presearch: safesearch, time ranges, crash when no result found
This commit is contained in:
parent
87f18b98ec
commit
0a78f59aba
1 changed files with 7 additions and 6 deletions
|
@ -30,6 +30,7 @@ about = {
|
||||||
"results": "JSON",
|
"results": "JSON",
|
||||||
}
|
}
|
||||||
paging = True
|
paging = True
|
||||||
|
safesearch = True
|
||||||
time_range_support = True
|
time_range_support = True
|
||||||
categories = ["general", "web"] # general, images, videos, news
|
categories = ["general", "web"] # general, images, videos, news
|
||||||
|
|
||||||
|
@ -45,18 +46,18 @@ def init(_):
|
||||||
raise ValueError(f'presearch search_type: {search_type}')
|
raise ValueError(f'presearch search_type: {search_type}')
|
||||||
|
|
||||||
|
|
||||||
def _get_request_id(query, page, time_range, safesearch):
|
def _get_request_id(query, page, time_range, safesearch_param):
|
||||||
args = {
|
args = {
|
||||||
"q": query,
|
"q": query,
|
||||||
"page": page,
|
"page": page,
|
||||||
}
|
}
|
||||||
if time_range:
|
if time_range:
|
||||||
args["time_range"] = time_range
|
args["time"] = time_range
|
||||||
|
|
||||||
url = f"{base_url}/{search_type}?{urlencode(args)}"
|
url = f"{base_url}/{search_type}?{urlencode(args)}"
|
||||||
headers = {
|
headers = {
|
||||||
'User-Agent': gen_useragent(),
|
'User-Agent': gen_useragent(),
|
||||||
'Cookie': f"b=1;presearch_session=;use_safe_search={safesearch_map[safesearch]}",
|
'Cookie': f"b=1;presearch_session=;use_safe_search={safesearch_map[safesearch_param]}",
|
||||||
}
|
}
|
||||||
resp_text = get(url, headers=headers).text # type: ignore
|
resp_text = get(url, headers=headers).text # type: ignore
|
||||||
|
|
||||||
|
@ -137,7 +138,7 @@ def response(resp):
|
||||||
results = parse_search_query(json_resp['results'])
|
results = parse_search_query(json_resp['results'])
|
||||||
|
|
||||||
elif search_type == 'images':
|
elif search_type == 'images':
|
||||||
for item in json_resp['images']:
|
for item in json_resp.get('images', []):
|
||||||
results.append(
|
results.append(
|
||||||
{
|
{
|
||||||
'template': 'images.html',
|
'template': 'images.html',
|
||||||
|
@ -152,7 +153,7 @@ def response(resp):
|
||||||
# The results in the video category are most often links to pages that contain
|
# The results in the video category are most often links to pages that contain
|
||||||
# a video and not to a video stream --> SearXNG can't use the video template.
|
# a video and not to a video stream --> SearXNG can't use the video template.
|
||||||
|
|
||||||
for item in json_resp['videos']:
|
for item in json_resp.get('videos', []):
|
||||||
metadata = [x for x in [item.get('description'), item.get('duration')] if x]
|
metadata = [x for x in [item.get('description'), item.get('duration')] if x]
|
||||||
results.append(
|
results.append(
|
||||||
{
|
{
|
||||||
|
@ -165,7 +166,7 @@ def response(resp):
|
||||||
)
|
)
|
||||||
|
|
||||||
elif search_type == 'news':
|
elif search_type == 'news':
|
||||||
for item in json_resp['news']:
|
for item in json_resp.get('news', []):
|
||||||
metadata = [x for x in [item.get('source'), item.get('time')] if x]
|
metadata = [x for x in [item.get('source'), item.get('time')] if x]
|
||||||
results.append(
|
results.append(
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue