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

from browshot import BrowshotClient
import time

browshot = BrowshotClient('my_api_key')

# Create a text file with a lsit of URLs
fp = open("batch.txt", mode='w')
fp.write("google.com\n")
fp.write("mobilito.net\n")
fp.close()



batch = browshot.batch_create("batch.txt", { 'instance_id': 65, 'screen_width': 1600, 
  'screen_height': 1200, 'size': 'page', 'width': 600 }); # Get thumbnails
  
if batch['status'] == 'error':
  print "Batch failed: %s" % batch['error']
  exit(0)

print "Batch #%d in process" % int(batch['id'])

time.sleep(30)
while batch['status'] != 'finished' and batch['status'] != 'error':
  time.sleep(15)
  batch = browshot.batch_info(batch['id'])

if batch['status'] == 'error':
  print "Batch failed: %s" % batch['error']
  exit(0)

# Check how many screenshot failed
print "%d/%d screenshots failed" % (int(batch['failed']), int(batch['count']))

if 'urls' in batch:
  # The batch succeeded, download the archive. There may be fore than 1 URL
  for url in batch['urls']:
    print "Downloading %s..." % url 
    # Download the archive

  if len(batch['urls']) > 1:
    # Run 7a if the archive was split into multiple files
    print "7a ..."