gigablast.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """
  4. Gigablast (Web)
  5. """
  6. # pylint: disable=invalid-name
  7. import re
  8. from time import time
  9. from json import loads
  10. from urllib.parse import urlencode
  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. search_path = '/search?'
  29. # ugly hack: gigablast requires a random extra parameter which can be extracted
  30. # from the source code of the gigablast HTTP client
  31. extra_param = ''
  32. # timestamp of the last fetch of extra_param
  33. extra_param_ts = 0
  34. # after how many seconds extra_param expire
  35. extra_param_expiration_delay = 3000
  36. def fetch_extra_param(query_args, headers):
  37. # example:
  38. #
  39. # var uxrl='/search?c=main&qlangcountry=en-us&q=south&s=10&rand=1590740241635&n';
  40. # uxrl=uxrl+'sab=730863287';
  41. #
  42. # extra_param --> "rand=1590740241635&nsab=730863287"
  43. global extra_param, extra_param_ts # pylint: disable=global-statement
  44. extra_param_ts = time()
  45. extra_param_path = search_path + urlencode(query_args)
  46. text = get(base_url + extra_param_path, headers=headers).text
  47. re_var = None
  48. for line in text.splitlines():
  49. if re_var is None and extra_param_path in line:
  50. var = line.split("=")[0].split()[1] # e.g. var --> 'uxrl'
  51. re_var = re.compile(var + "\\s*=\\s*" + var + "\\s*\\+\\s*'" + "(.*)" + "'(.*)")
  52. extra_param = line.split("'")[1][len(extra_param_path) :]
  53. continue
  54. if re_var is not None and re_var.search(line):
  55. extra_param += re_var.search(line).group(1)
  56. break
  57. # do search-request
  58. def request(query, params): # pylint: disable=unused-argument
  59. query_args = dict(c='main', q=query, dr=1, showgoodimages=0)
  60. if params['language'] and params['language'] != 'all':
  61. query_args['qlangcountry'] = params['language']
  62. query_args['qlang'] = params['language'].split('-')[0]
  63. if params['safesearch'] >= 1:
  64. query_args['ff'] = 1
  65. # see API http://www.gigablast.com/api.html#/search
  66. # Take into account, that the API has some quirks ..
  67. if time() > (extra_param_ts + extra_param_expiration_delay):
  68. fetch_extra_param(query_args, params['headers'])
  69. query_args['format'] = 'json'
  70. params['url'] = base_url + search_path + urlencode(query_args) + 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(url=url, title=title, content=content))
  95. return results