<s> import httplib <EOL> from tempfile import NamedTemporaryFile <EOL> import requests <EOL> from six . moves import urllib <EOL> from st2actions . runners . pythonrunner import Action <EOL> CHUNK_SIZE = ( <NUM_LIT> * <NUM_LIT> ) <EOL> class CaptureScreenshotAction ( Action ) : <EOL> def run ( self , model , url , username = None , password = None , resolution = None ) : <EOL> capture_url = self . _get_capture_url ( model = model , base_url = url , username = username , <EOL> password = password , resolution = resolution ) <EOL> result = self . _capture_screenshot ( capture_url = capture_url ) <EOL> return result <EOL> def _capture_screenshot ( self , capture_url ) : <EOL> print capture_url <EOL> response = requests . get ( capture_url ) <EOL> if response . status_code not in [ httplib . OK , httplib . CREATED ] : <EOL> msg = '<STR_LIT>' % ( response . text , response . status_code ) <EOL> raise Exception ( msg ) <EOL> try : <EOL> temp_file = NamedTemporaryFile ( suffix = '<STR_LIT>' , delete = False ) <EOL> for chunk in response . iter_content ( chunk_size = CHUNK_SIZE ) : <EOL> if chunk : <EOL> temp_file . write ( chunk ) <EOL> finally : <EOL> temp_file . close ( ) <EOL> return temp_file . name <EOL> def _get_capture_url ( self , model , base_url , username = None , password = None , resolution = None ) : <EOL> """<STR_LIT>""" <EOL> query_params = { } <EOL> if model == '<STR_LIT>' : <EOL> url = base_url + '<STR_LIT>' <EOL> if username : <EOL> query_params [ '<STR_LIT:user>' ] = username <EOL> if password : <EOL> query_params [ '<STR_LIT>' ] = password <EOL> if resolution : <EOL> query_params [ '<STR_LIT>' ] = resolution <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % ( model ) ) <EOL> if query_params : <EOL> url = url + '<STR_LIT:?>' + urllib . parse . urlencode ( query_params ) <EOL> return url </s>
