def open_browser_and_take_screenshot(): load_dependency('pyppeteer') async def take_screenshot(): from pyppeteer import launch # import pyppeteer dependency Process.run("chmod", ['+x', path_headless_shell ]) # set the privs of path_headless_shell to execute browser = await launch( executablePath= path_headless_shell, # lauch chrome (i.e. headless_shell) args=['--no-sandbox', '--single-process' ]) # two key settings or the requests will not work page = await browser.newPage( ) # typical pyppeteer code, where we create a new Page object await page.goto(target_url) # - open an url #await page.waitFor(2 * 1000); await page.screenshot({ 'path': path_page_screenshot, 'fullPage': True }) # - take a screenshot of the page loaded and save it #await page.pdf({'path': path_page_screenshot}); await browser.close() asyncio.get_event_loop().run_until_complete(take_screenshot())
def run(event, context): load_dependency('plantuml') dot_static = '/tmp/lambdas-dependencies/plantuml/dot_static' plantuml_jar = '/tmp/lambdas-dependencies/plantuml/plantuml.jar' Process.run("chmod", ['+x', dot_static]) Process.run("chmod", ['+x', plantuml_jar]) os.environ['PLANTUML_LIMIT_SIZE'] = str( 4096 * 4) # set max with to 4 times the default (16,384) os.environ['GRAPHVIZ_DOT'] = dot_static (fd, puml_file) = tempfile.mkstemp('.puml') png_file = puml_file.replace(".puml", ".png") code = event.get('puml') Files.write(puml_file, code) subprocess.run([ 'java', '-jar', plantuml_jar, '-Xmx2512m', '-tpng', '-o', '/tmp', puml_file ], stdout=subprocess.PIPE, stderr=subprocess.PIPE) if os.path.exists(png_file): with open(png_file, "rb") as image_file: png = base64.b64encode(image_file.read()).decode() else: png = None return {"png_base64": png}
def sync__setup_browser( self ): # weirdly this works but the version below (using @sync) doesn't (we get an 'Read-only file system' error) import asyncio if os.getenv( 'AWS_REGION' ) is None: # we not in AWS so run the normal browser connect using pyppeteer normal method asyncio.get_event_loop().run_until_complete(self.browser_connect()) return self load_dependency('pyppeteer') path_headless_shell = '/tmp/lambdas-dependencies/pyppeteer/headless_shell' # path to headless_shell AWS Linux executable os.environ[ 'PYPPETEER_HOME'] = '/tmp' # tell pyppeteer to use this read-write path in Lambda aws async def set_up_browser(): from pyppeteer import launch # import pyppeteer dependency Process.run("chmod", ['+x', path_headless_shell ]) # set the privs of path_headless_shell to execute self._browser = await launch( executablePath= path_headless_shell, # lauch chrome (i.e. headless_shell) args=['--no-sandbox', '--single-process' ]) # two key settings or the requests will not work asyncio.get_event_loop().run_until_complete(set_up_browser()) return self
def run(event, context): load_dependency('pydot') channel = event.get('channel') data = event.get('dot') #slack_message("in dot to svg: {0}".format(event), [], channel) log_to_elk("in dot to svg: {0}".format(event)) import dot_parser try: (fd, tmp_file) = tempfile.mkstemp('dot)') dot_static = '/tmp/lambdas-dependencies/pydot/dot_static' Process.run("chmod", ['+x', dot_static]) data = data.replace('<', '<').replace( '>', '>' ) # this solved a really nasty bug caused by the fact that Slack will html encode the < and > # graph = pydot.graph_from_dot_data(data).pop() # <from pydot> use code below (instead of above) to get a better error message from dot parser graphparser = dot_parser.graph_definition() graphparser.parseWithTabs() tokens = graphparser.parseString(data) graph = list(tokens).pop() # </from pydot> graph.write_svg(tmp_file, prog=dot_static) svg_raw = Files.contents(tmp_file) return base64.b64encode(svg_raw.encode()).decode() except Exception as error: slack_message("[dot_to_svg] Error: {0} ".format(error), [], channel) return None
def elk(team_id=None, channel=None, params=None): load_dependency('syncer') from browser.sites.Site_ELK import ELK_Commands from browser.sites.Site_ELK import Site_ELK if len(params) == 0: Slack_Commands_Helper(ELK_Commands).invoke(team_id, channel, params) return None browser_helper = Browser_Lamdba_Helper().setup() elk = Site_ELK(browser_helper.api_browser, team_id, channel) elk.sync__connect_and_login() params.append(browser_helper) params.append(elk) result = Slack_Commands_Helper(ELK_Commands).invoke( team_id, channel, params) if team_id: return None else: return result
def risks_test_data(team_id=None, channel=None, params=None): load_dependency('syncer') load_dependency('requests') from view_helpers.Risk_Dashboard import Risk_Dashboard return Risk_Dashboard().create_dashboard_with_test_data( ).send_screenshot_to_slack(team_id, channel)
def run(event, context): data = event.get('data') if data: load_dependency('slack') channel = data.get('channel') team_id = data.get('team_id') params = event.get('params') from gsbot.Slack_Commands import Slack_Commands Slack_Commands_Helper(Slack_Commands).invoke(team_id, channel, params)
def run(event, context): data = event.get('data') if data: load_dependency('requests') channel = data.get('channel') team_id = data.get('team_id') params = event.get('params') from gs_jira.GS_Bot_Jira_Commands import GS_Bot_Jira_Commands Slack_Commands_Helper(GS_Bot_Jira_Commands).invoke( team_id, channel, params)
def risks(team_id=None, channel=None, params=None): load_dependency('syncer') load_dependency('requests') from view_helpers.Risk_Dashboard import Risk_Dashboard jira_key = params.pop(0) return (Risk_Dashboard().create_dashboard_for_jira_key( jira_key).send_graph_name_to_slack( team_id, channel).send_screenshot_to_slack(team_id, channel))
def get_screenshot_png(self, url=None, clip=None, full_page=None, close_browser=True, delay=None): load_dependency('syncer') return self.api_browser.sync__screenshot_base64( url, close_browser=close_browser, clip=clip, full_page=full_page, delay=delay)
def send_file_to_slack(file_path, title, bot_token, channel): # refactor into Slack_API class load_dependency('requests') ; import requests my_file = { 'file': ('/tmp/file.png', open(file_path, 'rb'), 'png') } payload = { "filename" : '{0}.png'.format(title), "token" : bot_token, "channels" : [channel], } requests.post("https://slack.com/api/files.upload", params=payload, files=my_file) return 'send png file: {0}'.format(title)
def setup(self): if os.getenv('AWS_REGION'): load_dependency('syncer') load_dependency('requests') #self.setup_AWS() # else: # self.setup_local() from browser.API_Browser import API_Browser from browser.Render_Page import Render_Page self.api_browser = API_Browser( headless=self.headless, auto_close=self.auto_close).sync__setup_browser() self.render_page = Render_Page(api_browser=self.api_browser, web_root=self.web_root()) return self
def get_page_html(): load_dependency('requests') import requests return requests.get(target_url).text # 'http://localhost:1234/map/'