gigablast.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """
  4. Gigablast (Web)
  5. """
  6. # pylint: disable=missing-function-docstring, invalid-name
  7. import re
  8. from json import loads
  9. from urllib.parse import urlencode
  10. # from searx import logger
  11. from searx.network import get
  12. # about
  13. about = {
  14. "website": 'https://www.gigablast.com',
  15. "wikidata_id": 'Q3105449',
  16. "official_api_documentation": 'https://gigablast.com/api.html',
  17. "use_official_api": True,
  18. "require_api_key": False,
  19. "results": 'JSON',
  20. }
  21. # engine dependent config
  22. categories = ['general']
  23. # gigablast's pagination is totally damaged, don't use it
  24. paging = False
  25. safesearch = True
  26. # search-url
  27. base_url = 'https://gigablast.com'
  28. # ugly hack: gigablast requires a random extra parameter which can be extracted
  29. # from the source code of the gigablast HTTP client
  30. extra_param = ''
  31. extra_param_path='/search?c=main&qlangcountry=en-us&q=south&s=10'
  32. def parse_extra_param(text):
  33. # example:
  34. #
  35. # var uxrl='/search?c=main&qlangcountry=en-us&q=south&s=10&rand=1590740241635&n';
  36. # uxrl=uxrl+'sab=730863287';
  37. #
  38. # extra_param --> "rand=1590740241635&nsab=730863287"
  39. global extra_param # pylint: disable=global-statement
  40. re_var= None
  41. for line in text.splitlines():
  42. if re_var is None and extra_param_path in line:
  43. var = line.split("=")[0].split()[1] # e.g. var --> 'uxrl'
  44. re_var = re.compile(var + "\\s*=\\s*" + var + "\\s*\\+\\s*'" + "(.*)" + "'(.*)")
  45. extra_param = line.split("'")[1][len(extra_param_path):]
  46. continue
  47. if re_var is not None and re_var.search(line):
  48. extra_param += re_var.search(line).group(1)
  49. break
  50. # logger.debug('gigablast extra_param="%s"', extra_param)
  51. def init(engine_settings=None): # pylint: disable=unused-argument
  52. parse_extra_param(get(base_url + extra_param_path).text)
  53. # do search-request
  54. def request(query, params): # pylint: disable=unused-argument
  55. # see API http://www.gigablast.com/api.html#/search
  56. # Take into account, that the API has some quirks ..
  57. query_args = dict(
  58. c = 'main'
  59. , format = 'json'
  60. , q = query
  61. , dr = 1
  62. , showgoodimages = 0
  63. )
  64. if params['language'] and params['language'] != 'all':
  65. query_args['qlangcountry'] = params['language']
  66. query_args['qlang'] = params['language'].split('-')[0]
  67. if params['safesearch'] >= 1:
  68. query_args['ff'] = 1
  69. search_url = '/search?' + urlencode(query_args)
  70. params['url'] = base_url + search_url + extra_param
  71. return params
  72. # get response from search-request
  73. def response(resp):
  74. results = []
  75. response_json = loads(resp.text)
  76. # logger.debug('gigablast returns %s results', len(response_json['results']))
  77. for result in response_json['results']:
  78. # see "Example JSON Output (&format=json)"
  79. # at http://www.gigablast.com/api.html#/search
  80. # sort out meaningless result
  81. title = result.get('title')
  82. if len(title) < 2:
  83. continue
  84. url = result.get('url')
  85. if len(url) < 9:
  86. continue
  87. content = result.get('sum')
  88. if len(content) < 5:
  89. continue
  90. # extend fields
  91. subtitle = result.get('title')
  92. if len(subtitle) > 3 and subtitle != title:
  93. title += " - " + subtitle
  94. results.append(dict(
  95. url = url
  96. , title = title
  97. , content = content
  98. ))
  99. return results