def screenshot(team_id=None, channel=None, params=None): event = params.pop() # needed due to injection of event param try: if len(params) < 2: return send_message(":red_circle: missing `short id` and `path`. The syntax for this method is `screenshot {short_id} {path}`",channel, team_id) max_screenshot_delay = 15 short_id = params.pop(0) path = params.pop(0).replace('<', '').replace('>', '') # fix extra chars added by Slack width = Misc.to_int(Misc.array_pop(params, 0)) height = Misc.to_int(Misc.array_pop(params, 0)) delay = Misc.to_int(Misc.array_pop(params, 0)) if not width : width = 1200 if not height: height = 800 if not delay : delay = 0 if delay > max_screenshot_delay : delay = max_screenshot_delay send_message(":point_right: taking screenshot of notebook `{0}` in server `{1}` with width `{2}`, (min) height `{3}` and delay `{4}`".format(path,short_id,width,height,delay),channel,team_id) payload = {'short_id': short_id, 'path': path,'width': width,'height': height, 'delay' : delay} png_data = Lambda('osbot_jupyter.lambdas.screenshot').invoke(payload) if len(png_data) == 3: send_message(":red_circle: error taking screenshot :{0} ".format(png_data),channel,team_id) else: send_message(":point_right: got screenshot with size `{0}` (sending it to slack) ".format(len(png_data)),channel,team_id) Lambda('gw_bot.lambdas..png_to_slack').invoke({'png_data': png_data, 'team_id': team_id, 'channel': channel}) except Exception as error: send_message(":red_circle: error in screenshot: {0}".format(error),channel,team_id)
def slack(team_id=None, channel=None, params=None): target = Misc.array_pop(params, 0) height = Misc.to_int(Misc.array_pop(params, 0)) width = Misc.to_int(Misc.array_pop(params, 0)) scroll_by = Misc.to_int(Misc.array_pop(params, 0)) delay = Misc.to_int(Misc.array_pop(params, 0)) if target is None: target = 'general' if width is None: width = 800 if height is None: height = 1000 if scroll_by is None: scroll_by = 0 if delay is None: delay = 0 target_url = '/messages/{0}'.format(target) slack_message( ":point_right: Taking screenshot of slack channel: `{0}` with height `{1}`, width `{2}`, scroll_by `{3}` and delay `{4}`" .format(target, height, width, scroll_by, delay), [], channel, team_id) payload = { 'target': target_url, 'channel': channel, 'team_id': team_id, 'width': width, 'height': height, 'scroll_by': scroll_by, 'delay': delay } aws_lambda = Lambda('osbot_browser.lambdas.slack_web') aws_lambda.invoke_async(payload)
def screenshot(team_id=None, channel=None, params=None, headless=True): event = Misc.array_pop( params ) # original slack event object (don't think this is needed anymore) if not params or len(params) == 0: return send_message( ':red_circle: You must provide an Server Id. Please use `jupyter servers` to see the current list of live servers', channel, team_id) from osbot_jupyter.api.Live_Notebook import Live_Notebook short_id = Misc.array_pop(params, 0) path = Misc.array_pop(params, 0) width = Misc.to_int(Misc.array_pop(params, 0)) height = Misc.to_int(Misc.array_pop(params, 0)) delay = Misc.to_int(Misc.array_pop(params, 0)) if not path: path = '/' if not width: width = 1200 if not height: height = 500 if delay is None: delay = 1 # add one second delay if no value is provided notebook = Live_Notebook(headless=headless) if notebook.set_build_from_short_id(short_id) is None: return ':red_circle: Could not find Jupyter server with id `{0}`. Please use `jupyter servers` to see the current list of live servers'.format( short_id) send_message( ':point_right: taking screenshot of `{0}` with width `{1}`, (min) height `{2}` and delay `{3}`' .format(path, width, height, delay), channel, team_id) png_data = notebook.screenshot( path=path, width=width, height=height, delay=delay, apply_ui_fixes=False ) # when calling the sceenshot via the web command, don't apply the UI fixes return send_png_to_slack(png_data, channel, team_id)
def run(event, context): issue_id = event.get('issue_id') channel = event.get('channel') team_id = event.get('team_id') width = event.get('width') height = event.get('height') delay = Misc.to_int(event.get('delay')) headless = event.get('headless', True) if issue_id is None: return { 'error': 'No issue ID provided' } Browser_Lamdba_Helper().load_browser_dependencies() #load_dependencies('syncer,requests,pyppeteer,websocket-client') try: from osbot_browser.browser.sites.Web_Jira import Web_Jira web_jira = Web_Jira(headless=headless).setup() slack_message(':one: Logging in...', [] ,channel) web_jira.login() #return web_jira.screenshot() slack_message(f':two: opening issue `{issue_id}` in headless chrome', [], channel) web_jira.issue(issue_id) if width is None: width = 1200 if height is None: height = 300 if delay: slack_message(f':hourglass: Pausing for {delay} seconds before taking the screenshot',[],channel) sleep(delay) web_jira.fix_issue_remove_ui_elements() #todo: next line breaks when delay is too big (need to find a way to keep the browser alive) png_data = web_jira.screenshot(width, height) if channel: title = "Issue: {0}".format(issue_id) slack_message(f':three: sending screenshot to slack image with size {len(png_data)}', [], channel) Browser_Lamdba_Helper().send_png_data_to_slack(team_id, channel, title, png_data) #return "sent png to slack" except Exception as error: message = f'Error in jira_web lambda: {error}' slack_message(message,[],channel) return message return png_data
def preview(team_id=None, channel=None, params=None, headless=True): event = Misc.array_pop(params) # original slack event object if not params or len(params) < 2: return send_message( ':red_circle: You must provide an Server Id and file to process. Please use `jupyter servers` to see the current list of live servers', channel, team_id) short_id = Misc.array_pop(params, 0) path = Misc.array_pop(params, 0) width = Misc.to_int(Misc.array_pop(params, 0, 1200)) height = Misc.to_int(Misc.array_pop(params, 0, 800)) delay = Misc.to_int(Misc.array_pop(params, 0, 0)) if width is None: width = 1200 if height is None: height = 800 if delay is None: delay = 0 notebook = Live_Notebook(headless=headless) if notebook.set_build_from_short_id(short_id) is None: return ':red_circle: Could not find Jupyter server with id `{0}`. Please use `jupyter servers` to see the current list of live servers'.format( short_id) if '?show-code' in path: path = f'nbconvert/html/{path}' else: path = f'nbconvert/html/{path}?download=false&osbot-no-code' send_message( ':point_right: taking screenshot of `{0}` with width `{1}`, (min) height `{2}` and delay `{3}`' .format(path, width, height, delay), channel, team_id) png_data = notebook.screenshot(path=path, width=width, height=height, delay=delay, apply_ui_fixes=True) return send_png_to_slack(png_data, channel, team_id)
def scroll_messages_by(self,value): value = Misc.to_int(value) if value: # split the scroll in chunks since it was not working as expected when the # scroll amount was bigger than the current browser page window height while value > 0: if value < self.scroll_split: scroll_by = value else: scroll_by = self.scroll_split value -= scroll_by js_code = """value = $('.c-scrollbar__hider').eq(1).scrollTop() - {0} ; $('.c-scrollbar__hider').eq(1).scrollTop(value);""".format(scroll_by) self.js_invoke(js_code) self.wait(0.25) # wait a little bit before sending the next scroll event (this needs a better solution) return self
def render(team_id, channel, params): load_dependencies('syncer,requests,pyppeteer,websocket-client') if params: target = params.pop(0) delay = Misc.to_int(Misc.array_pop(params, 0)) if len(params) == 4: clip = { 'x': int(params[0]), 'y': int(params[1]), 'width': int(params[2]), 'height': int(params[3]) } else: clip = None else: return None slack_message(":point_right: rendering file `{0}`".format(target), [], channel, team_id) return Browser_Lamdba_Helper().setup().render_file(team_id, channel, target, clip=clip, delay=delay)
def mindmap(team_id=None, channel=None, params=None, root_node_text=None): (go_js, graph_data) = Go_Js_Views._get_graph_data(params, "mindmap", headless=True) go_js.load_page(False) (nodes, edges) = Go_Js_Views._get_nodes_and_edges(graph_data, text_field='Summary') data = {"class": "go.TreeModel", "nodeDataArray": []} width = Misc.to_int(Misc.array_pop(params, 0)) height = Misc.to_int(Misc.array_pop(params, 0)) if width and height: go_js.browser().sync__browser_width(width, height) else: go_js.set_browser_width_based_on_nodes(nodes) nodes_indexed = {} if len(nodes) > 0: for index, node in enumerate(nodes): key = node.get('key') #text = "{1} | {0}".format(key,node.get('text')) # need a better way to trigger this text = node.get('text') nodes_indexed[key] = {'index': index, 'text': text} #root_node_text = "{1} | {0}".format(nodes[0].get('key'), nodes[0].get('text')) # and this if root_node_text is None: root_node_text = nodes[0].get('text') data['nodeDataArray'].append({ "key": 0, "text": root_node_text }) # add root node first for edge in edges: from_key = edge['from'] to_key = edge['to'] from_issue = nodes_indexed.get(from_key) to_issue = nodes_indexed.get(to_key) if from_issue: parent = nodes_indexed.get(edge['from']).get('index') else: parent = from_key if to_issue: key = nodes_indexed.get(edge['to']).get('index') text = nodes_indexed.get(edge['to']).get('text') else: key = to_key text = to_key #item = {"key": key, "parent": parent, "text": text, "brush": Misc.get_random_color()} item = { "key": key, "parent": parent, "text": text, "brush": 'darkseagreen' } data['nodeDataArray'].append(item) go_js.invoke_js("create_graph_from_json", data) js_code = 'layoutAll()' go_js.api_browser.sync__await_for_element('#animationFinished') Dev.pprint(go_js.exec_js(js_code)) return go_js.send_screenshot_to_slack(team_id=team_id, channel=channel)
def timeline(team_id=None, channel=None, params=None, headless=True): load_dependencies('syncer,requests,pyppeteer,websocket-client'); from osbot_browser.view_helpers.Am_Charts import Am_Charts am_charts = Am_Charts(headless=headless) Misc.array_pop(params,0) # remove the graph name (which is not used) width = Misc.to_int(Misc.array_pop(params, 0)) height = Misc.to_int(Misc.array_pop(params, 0)) clip = {'x': 0, 'y': 50, 'width': width, 'height': height-100} #clip = None am_charts.browser().sync__browser_width(width, height) try: data = json.loads(" ".join(params)) am_charts.load_page(True) js_code = """ // Create chart instance var chart = am4core.create("chartdiv", am4charts.XYChart); // Create axes var xAxis = chart.xAxes.push(new am4charts.CategoryAxis()); xAxis.dataFields.category = "x"; xAxis.renderer.grid.template.disabled = true; xAxis.renderer.labels.template.disabled = true; xAxis.tooltip.disabled = true; var yAxis = chart.yAxes.push(new am4charts.ValueAxis()); yAxis.min = 0; yAxis.max = 1.99; yAxis.renderer.grid.template.disabled = true; yAxis.renderer.labels.template.disabled = true; yAxis.renderer.baseGrid.disabled = true; yAxis.tooltip.disabled = true; // Create series var series = chart.series.push(new am4charts.LineSeries()); series.dataFields.categoryX = "x"; series.dataFields.valueY = "y"; series.strokeWidth = 4; series.sequencedInterpolation = true; var bullet = series.bullets.push(new am4charts.CircleBullet()); bullet.setStateOnChildren = true; bullet.states.create("hover"); bullet.circle.radius = 10; bullet.circle.states.create("hover").properties.radius = 15; var labelBullet = series.bullets.push(new am4charts.LabelBullet()); labelBullet.setStateOnChildren = true; labelBullet.states.create("hover").properties.scale = 1.2; labelBullet.label.text = "{text}"; labelBullet.label.maxWidth = 150; labelBullet.label.wrap = true; labelBullet.label.truncate = false; labelBullet.label.textAlign = "middle"; labelBullet.label.paddingTop = 20; labelBullet.label.paddingBottom = 20; labelBullet.label.fill = am4core.color("#999"); labelBullet.label.states.create("hover").properties.fill = am4core.color("#000"); labelBullet.label.propertyFields.verticalCenter = "center"; chart.cursor = new am4charts.XYCursor(); chart.cursor.lineX.disabled = true; chart.cursor.lineY.disabled = true; """ # data = [{"x": "1","y": 1,"text": "[bold]2018 Q1[/]\nAAAA There seems to be some furry animal living in the neighborhood.", "center": "bottom"}, { # "x": "2", # "y": 1, # "text": "[bold]2018 Q2[/]\nWe're now mostly certain it's a fox.", # "center": "top" # }, { # "x": "3", # "y": 1, # "text": "[bold]2018 Q3[/]\nOur dog does not seem to mind the newcomer at all.", # "center": "bottom" # }, { # "x": "4", # "y": 1, # "text": "[bold]2018 Q4[/]\nThe quick brown fox jumps over the lazy dog.", # "center": "top" # }]; am_charts.exec_js(js_code) am_charts.assign_variable_js('window.chart.data', data) return am_charts.send_screenshot_to_slack(team_id, channel,clip=clip) #return "ok {0}".format(data) except Exception as error: return "error: {0}".format(error)