def _handle_get_screenshot(self, param): self.save_progress("In action handler for: {0}".format( self.get_action_identifier())) action_result = self.add_action_result(ActionResult(dict(param))) s_url = self._handle_py_ver_compat_for_input_str(param['url']) ftype = self._handle_py_ver_compat_for_input_str(param['type']) quality = param['quality'] # Validation of the quality parameter ret_val, quality = self._validate_integer(action_result, quality, QUALITY_KEY) if phantom.is_fail(ret_val): return action_result.get_status() fullpage = param.get('fullpage', True) followRefresh = param.get('followrefresh', True) jpeg_query = { "url": s_url, "options": { "type": ftype, "quality": "{}".format(quality), "fullPage": "{}".format(fullpage) } } png_query = { "url": s_url, "options": { "type": ftype, "fullPage": "{}".format(fullpage) } } if ftype == "png": query = png_query elif ftype == "jpeg": query = jpeg_query else: return action_result.set_status( phantom.APP_ERROR, "Invalid input. The 'type' parameter value should be either png or jpeg" ) if followRefresh: query["gotoOptions"] = {"waitUntil": "networkidle2"} # make rest call ret_val, response = self._make_rest_call('/screenshot', action_result, data=json.dumps(query)) if phantom.is_fail(ret_val): return action_result.get_status() try: file_name = "{}_screenshot.{}".format(s_url, ftype) if hasattr(Vault, 'create_attachment'): vault_ret = Vault.create_attachment(response, self.get_container_id(), file_name=file_name) else: if hasattr(Vault, 'get_vault_tmp_dir'): temp_dir = Vault.get_vault_tmp_dir() else: temp_dir = '/opt/phantom/vault/tmp' temp_dir = '{}/{}'.format(temp_dir, hashlib.md5(file_name).hexdigest()) os.makedirs(temp_dir) file_path = os.path.join(temp_dir, 'tmpfile') with open(file_path, 'wb') as f: f.write(response) vault_ret = Vault.add_attachment(file_path, self.get_container_id(), file_name=file_name) if vault_ret.get('succeeded'): action_result.set_status(phantom.APP_SUCCESS, 'Downloaded Screenshot') summary = { phantom.APP_JSON_VAULT_ID: vault_ret[phantom.APP_JSON_HASH], phantom.APP_JSON_NAME: file_name, 'vault_file_path': Vault.get_file_path(vault_ret[phantom.APP_JSON_HASH]), phantom.APP_JSON_SIZE: vault_ret.get(phantom.APP_JSON_SIZE) } action_result.update_summary(summary) except Exception as e: err_msg = self._get_error_message_from_exception(e) return action_result.set_status(phantom.APP_ERROR, err_msg) return action_result.get_status()
def _handle_get_pdf(self, param): self.save_progress("In action handler for: {0}".format( self.get_action_identifier())) action_result = self.add_action_result(ActionResult(dict(param))) s_url = self._handle_py_ver_compat_for_input_str(param['url']) headerfooter = param.get('headerfooter', True) printbackground = param.get('printbackground', False) landscape = param.get('landscape', False) followRefresh = param.get('followrefresh', True) query = { "url": s_url, "options": { "displayHeaderFooter": "{}".format(headerfooter), "printBackground": "{}".format(printbackground), "landscape": "{}".format(landscape) } } if followRefresh: query["gotoOptions"] = {"waitUntil": "networkidle2"} # make rest call ret_val, response = self._make_rest_call('/pdf', action_result, data=json.dumps(query)) if phantom.is_fail(ret_val): return action_result.get_status() try: file_name = "{}_screenshot.pdf".format(s_url) if hasattr(Vault, 'create_attachment'): vault_ret = Vault.create_attachment(response, self.get_container_id(), file_name=file_name) else: if hasattr(Vault, 'get_vault_tmp_dir'): temp_dir = Vault.get_vault_tmp_dir() else: temp_dir = '/opt/phantom/vault/tmp' temp_dir = '{}/{}'.format(temp_dir, hashlib.md5(file_name).hexdigest()) os.makedirs(temp_dir) file_path = os.path.join(temp_dir, 'tmpfile') with open(file_path, 'wb') as f: f.write(response) vault_ret = Vault.add_attachment(file_path, self.get_container_id(), file_name=file_name) if vault_ret.get('succeeded'): action_result.set_status(phantom.APP_SUCCESS, 'Downloaded PDF') summary = { phantom.APP_JSON_VAULT_ID: vault_ret[phantom.APP_JSON_HASH], phantom.APP_JSON_NAME: file_name, 'vault_file_path': Vault.get_file_path(vault_ret[phantom.APP_JSON_HASH]), phantom.APP_JSON_SIZE: vault_ret.get(phantom.APP_JSON_SIZE) } action_result.update_summary(summary) except Exception as e: err_msg = self._get_error_message_from_exception(e) return action_result.set_status(phantom.APP_ERROR, err_msg) return action_result.get_status()