gigablast.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """
  2. Gigablast (Web)
  3. @website https://gigablast.com
  4. @provide-api yes (https://gigablast.com/api.html)
  5. @using-api yes
  6. @results XML
  7. @stable yes
  8. @parse url, title, content
  9. """
  10. # pylint: disable=missing-function-docstring, invalid-name
  11. from time import time
  12. from json import loads
  13. from searx.url_utils import urlencode
  14. # engine dependent config
  15. categories = ['general']
  16. paging = True
  17. number_of_results = 10
  18. language_support = True
  19. safesearch = True
  20. # search-url
  21. base_url = 'https://gigablast.com/'
  22. # do search-request
  23. def request(query, params): # pylint: disable=unused-argument
  24. # see API http://www.gigablast.com/api.html#/search
  25. # Take into account, that the API has some quirks ..
  26. query_args = dict(
  27. c = 'main'
  28. , n = number_of_results
  29. , format = 'json'
  30. , q = query
  31. # The gigablast HTTP client sends a random number and a nsga argument
  32. # (the values don't seem to matter)
  33. , rand = int(time() * 1000)
  34. , nsga = int(time() * 1000)
  35. )
  36. page_no = (params['pageno'] - 1)
  37. if page_no:
  38. # API quirk; adds +2 to the number_of_results
  39. offset = (params['pageno'] - 1) * number_of_results
  40. query_args['s'] = offset
  41. if params['language'] and params['language'] != 'all':
  42. query_args['qlangcountry'] = params['language']
  43. query_args['qlang'] = params['language'].split('-')[0]
  44. if params['safesearch'] >= 1:
  45. query_args['ff'] = 1
  46. search_url = 'search?' + urlencode(query_args)
  47. params['url'] = base_url + search_url
  48. return params
  49. # get response from search-request
  50. def response(resp):
  51. results = []
  52. response_json = loads(resp.text)
  53. for result in response_json['results']:
  54. # see "Example JSON Output (&format=json)"
  55. # at http://www.gigablast.com/api.html#/search
  56. # sort out meaningless result
  57. title = result.get('title')
  58. if len(title) < 2:
  59. continue
  60. url = result.get('url')
  61. if len(url) < 9:
  62. continue
  63. content = result.get('sum')
  64. if len(content) < 5:
  65. continue
  66. # extend fields
  67. subtitle = result.get('title')
  68. if len(subtitle) > 3:
  69. title += " - " + subtitle
  70. results.append(dict(
  71. url = url
  72. , title = title
  73. , content = content
  74. ))
  75. return results