gigablast.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. from cgi import escape
  11. from json import loads
  12. from random import randint
  13. from time import time
  14. from urllib import urlencode
  15. # engine dependent config
  16. categories = ['general']
  17. paging = True
  18. number_of_results = 10
  19. language_support = True
  20. safesearch = True
  21. # search-url
  22. base_url = 'https://gigablast.com/'
  23. search_string = 'search?{query}'\
  24. '&n={number_of_results}'\
  25. '&c=main'\
  26. '&s={offset}'\
  27. '&format=json'\
  28. '&qh=0'\
  29. '&rxiwd={rxiwd}'\
  30. '&qlang={lang}'\
  31. '&ff={safesearch}'
  32. # specific xpath variables
  33. results_xpath = '//response//result'
  34. url_xpath = './/url'
  35. title_xpath = './/title'
  36. content_xpath = './/sum'
  37. # do search-request
  38. def request(query, params):
  39. offset = (params['pageno'] - 1) * number_of_results
  40. if params['language'] == 'all':
  41. language = 'xx'
  42. else:
  43. language = params['language'][0:2]
  44. if params['safesearch'] >= 1:
  45. safesearch = 1
  46. else:
  47. safesearch = 0
  48. search_path = search_string.format(query=urlencode({'q': query}),
  49. offset=offset,
  50. number_of_results=number_of_results,
  51. rxiwd=1,
  52. # rand=int(time()),
  53. lang=language,
  54. safesearch=safesearch)
  55. params['url'] = base_url + search_path
  56. return params
  57. # get response from search-request
  58. def response(resp):
  59. results = []
  60. # parse results
  61. response_json = loads(resp.text)
  62. for result in response_json['results']:
  63. # append result
  64. results.append({'url': result['url'],
  65. 'title': escape(result['title']),
  66. 'content': escape(result['sum'])})
  67. # return results
  68. return results