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

from browshot import BrowshotClient
import time

browshot = BrowshotClient('my_api_key', 1) # more debug information


screenshot = browshot.screenshot_create('https://browshot.com/', { 'details': 3, 'instance': 24, 'screen_width': 1280, 'screen_height': 1024 }) # extra details
# the call to screenshot/create sends the same response as screenshot/information
# so screenshot/create and screenshot/info can be interchanged as long as the cache value is long enough
while screenshot['status'] != 'finished' and  screenshot['status'] != 'error':
  print "Wait...\n"
  time.sleep(10)
  screenshot = browshot.screenshot_info(screenshot['id'], { 'details': 3 })
  # or $screenshot = $browshot->screenshot_create(array('url' => 'https://browshot.com/', 'details' => 3, 'instance' => 24, 'screen_width' => 1280, 'screen_height' => 1024));

# screenshot is done: finished (sucessful) or error (failed)
if screenshot['status'] == 'error':
  print "Screenshot failed: %s\n" % screenshot['error'] # display the reason for the error
  
  # If you call $browshot->screenshot_create with the same parameters, a new screenshot will be requested.
else: # screenshot is finished
  # did the page load (200 OK) or was mnissing (404 Not Found) or some error (40X, 50X);
  if screenshot['response_code'] != 200:
    print "The page may not have be reached, HTTP response code was: %s\n" % screenshot['response_code']
  
  # Was it an HTML page, PDF or image?
  if screenshot['content_type'] != 'text/html':
    print "The page is not HTML, it is %s\n" % screenshot['content_type']
  
  # with details = 3, you can check what asserts were loaded on the page: images, javascript, frames, etc.
  if 'images' in screenshot and isinstance(screenshot['images'], list):
    print "There are %d images on this page:\n" % len(screenshot['images'])
    for url in screenshot['images']:
      print "%s\n" % url