Add extra features to Gigablast engine:

* fast can be enabled to results are returned quicker
* collection can be configured
* search_type can be changed to images or news

Closes #3078
This commit is contained in:
Noémi Ványi 2022-01-22 19:14:45 +01:00
parent 01e28757d3
commit a164585118
2 changed files with 30 additions and 12 deletions

View File

@ -5,10 +5,10 @@
# pylint: disable=missing-function-docstring, invalid-name # pylint: disable=missing-function-docstring, invalid-name
import re import re
from json import loads from json import loads, JSONDecodeError
from urllib.parse import urlencode from urllib.parse import urlencode
# from searx import logger
from searx.network import get from searx.network import get
from searx.exceptions import SearxEngineResponseException
# about # about
about = { about = {
@ -22,6 +22,9 @@ about = {
# engine dependent config # engine dependent config
categories = ['general'] categories = ['general']
collections = 'main'
search_type = ''
fast = 0
# gigablast's pagination is totally damaged, don't use it # gigablast's pagination is totally damaged, don't use it
paging = False paging = False
safesearch = True safesearch = True
@ -34,6 +37,8 @@ base_url = 'https://gigablast.com'
extra_param = '' extra_param = ''
extra_param_path='/search?c=main&qlangcountry=en-us&q=south&s=10' extra_param_path='/search?c=main&qlangcountry=en-us&q=south&s=10'
_wait_for_results_msg = 'Loading results takes too long. Please enable fast option in gigablast engine.'
def parse_extra_param(text): def parse_extra_param(text):
# example: # example:
@ -54,7 +59,6 @@ def parse_extra_param(text):
if re_var is not None and re_var.search(line): if re_var is not None and re_var.search(line):
extra_param += re_var.search(line).group(1) extra_param += re_var.search(line).group(1)
break break
# logger.debug('gigablast extra_param="%s"', extra_param)
def init(engine_settings=None): # pylint: disable=unused-argument def init(engine_settings=None): # pylint: disable=unused-argument
parse_extra_param(get(base_url + extra_param_path).text) parse_extra_param(get(base_url + extra_param_path).text)
@ -65,14 +69,17 @@ def request(query, params): # pylint: disable=unused-argument
# see API http://www.gigablast.com/api.html#/search # see API http://www.gigablast.com/api.html#/search
# Take into account, that the API has some quirks .. # Take into account, that the API has some quirks ..
query_args = {
'c': collections,
'format': 'json',
'q': query,
'dr': 1 ,
'showgoodimages': 0,
'fast': fast,
}
query_args = dict( if search_type != '':
c = 'main' query_args['searchtype'] = search_type
, format = 'json'
, q = query
, dr = 1
, showgoodimages = 0
)
if params['language'] and params['language'] != 'all': if params['language'] and params['language'] != 'all':
query_args['qlangcountry'] = params['language'] query_args['qlangcountry'] = params['language']
@ -90,9 +97,13 @@ def request(query, params): # pylint: disable=unused-argument
def response(resp): def response(resp):
results = [] results = []
response_json = loads(resp.text) try:
response_json = loads(resp.text)
except JSONDecodeError as e:
if 'Waiting for results' in resp.text:
raise SearxEngineResponseException(message=_wait_for_results_msg) # pylint: disable=raise-missing-from
raise e
# logger.debug('gigablast returns %s results', len(response_json['results']))
for result in response_json['results']: for result in response_json['results']:
# see "Example JSON Output (&format=json)" # see "Example JSON Output (&format=json)"

View File

@ -505,6 +505,13 @@ engines:
engine : gigablast engine : gigablast
shortcut : gb shortcut : gb
timeout : 3.0 timeout : 3.0
# Collection names to search in, separated by whitespaces.
# By default searx, queries the main collection.
#collections: main
# Search types, not required. possible values: images, news
#search_type: images
# Enable fast to get results quicker.
#fast : 1
disabled: True disabled: True
additional_tests: additional_tests:
rosebud: *test_rosebud rosebud: *test_rosebud