gigablast.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """
  4. Gigablast (Web)
  5. """
  6. # pylint: disable=invalid-name, use-dict-literal
  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', 'web']
  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. gb_userid = ''
  37. gb_code = ''
  38. def fetch_extra_param(query_args, headers):
  39. # example:
  40. #
  41. # var uxrl='/search?c=main&qlangcountry=en-us&q=south&s=10&rand=1590740241635&n';
  42. # uxrl=uxrl+'sab=730863287';
  43. #
  44. # extra_param --> "rand=1590740241635&nsab=730863287"
  45. global extra_param, extra_param_ts # pylint: disable=global-statement
  46. extra_param_ts = time()
  47. extra_param_path = search_path + urlencode(query_args)
  48. text = get(base_url + extra_param_path, headers=headers).text
  49. re_var = None
  50. for line in text.splitlines():
  51. if re_var is None and extra_param_path in line:
  52. var = line.split("=")[0].split()[1] # e.g. var --> 'uxrl'
  53. re_var = re.compile(var + "\\s*=\\s*" + var + "\\s*\\+\\s*'" + "(.*)" + "'(.*)")
  54. extra_param = line.split("'")[1][len(extra_param_path) :]
  55. continue
  56. if re_var is not None and re_var.search(line):
  57. extra_param += re_var.search(line).group(1)
  58. break
  59. # do search-request
  60. def request(query, params): # pylint: disable=unused-argument
  61. query_args = dict(c='main', q=query, dr=1, showgoodimages=0)
  62. if gb_userid and gb_code:
  63. query_args['userid'] = gb_userid
  64. query_args['code'] = gb_code
  65. if params['language'] and params['language'] != 'all':
  66. query_args['qlangcountry'] = params['language']
  67. query_args['qlang'] = params['language'].split('-')[0]
  68. if params['safesearch'] >= 1:
  69. query_args['ff'] = 1
  70. # see API http://www.gigablast.com/api.html#/search
  71. # Take into account, that the API has some quirks ..
  72. if time() > (extra_param_ts + extra_param_expiration_delay):
  73. fetch_extra_param(query_args, params['headers'])
  74. query_args['format'] = 'json'
  75. params['url'] = base_url + search_path + urlencode(query_args) + extra_param
  76. return params
  77. # get response from search-request
  78. def response(resp):
  79. results = []
  80. response_json = loads(resp.text)
  81. # logger.debug('gigablast returns %s results', len(response_json['results']))
  82. for result in response_json['results']:
  83. # see "Example JSON Output (&format=json)"
  84. # at http://www.gigablast.com/api.html#/search
  85. # sort out meaningless result
  86. title = result.get('title')
  87. if len(title) < 2:
  88. continue
  89. url = result.get('url')
  90. if len(url) < 9:
  91. continue
  92. content = result.get('sum')
  93. if len(content) < 5:
  94. continue
  95. # extend fields
  96. subtitle = result.get('title')
  97. if len(subtitle) > 3 and subtitle != title:
  98. title += " - " + subtitle
  99. results.append(dict(url=url, title=title, content=content))
  100. return results