gigablast.py 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. """
  2. Gigablast (Web)
  3. @website http://gigablast.com
  4. @provide-api yes (http://gigablast.com/api.html)
  5. @using-api yes
  6. @results XML
  7. @stable yes
  8. @parse url, title, content
  9. """
  10. from urllib import urlencode
  11. from cgi import escape
  12. from lxml import etree
  13. # engine dependent config
  14. categories = ['general']
  15. paging = True
  16. number_of_results = 5
  17. # search-url, invalid HTTPS certificate
  18. base_url = 'http://gigablast.com/'
  19. search_string = 'search?{query}&n={number_of_results}&s={offset}&xml=1&qh=0'
  20. # specific xpath variables
  21. results_xpath = '//response//result'
  22. url_xpath = './/url'
  23. title_xpath = './/title'
  24. content_xpath = './/sum'
  25. # do search-request
  26. def request(query, params):
  27. offset = (params['pageno'] - 1) * number_of_results
  28. search_path = search_string.format(
  29. query=urlencode({'q': query}),
  30. offset=offset,
  31. number_of_results=number_of_results)
  32. params['url'] = base_url + search_path
  33. return params
  34. # get response from search-request
  35. def response(resp):
  36. results = []
  37. dom = etree.fromstring(resp.content)
  38. # parse results
  39. for result in dom.xpath(results_xpath):
  40. url = result.xpath(url_xpath)[0].text
  41. title = result.xpath(title_xpath)[0].text
  42. content = escape(result.xpath(content_xpath)[0].text)
  43. # append result
  44. results.append({'url': url,
  45. 'title': title,
  46. 'content': content})
  47. # return results
  48. return results