# -*- coding: utf-8 -*-
#######################
# WARNING
# Running this code sample will cost you Browshot credits
#######################

from browshot import BrowshotClient
import time

browshot = BrowshotClient('my_api_key')

screenshot = browshot.screenshot_create('http://www.google.com/', { 'instance_id': 12, 'size': 'page' }) # all default parameters, instance_id = 12 (free)
# If the screenshot is already in cache, it could be finished already. Otherwise, wait longer
while screenshot['status'] != 'finished' and screenshot['status'] != 'error':
  print "Wait...";
  time.sleep(10)
  screenshot = browshot.screenshot_info(screenshot['id'])

# screenshot is done: finished (sucessful) or error (failed)
if screenshot['status'] == 'error':
  print  "Screenshot failed: " + screenshot['error'] # display the reason for the error
else: # request the thumbnail
  image = browshot.screenshot_thumbnail(screenshot['id'])
  
  # save the screenshot
  fp = open("browshot.png", mode='wb')
  fp.write(image)
  fp.close()
