def post(self): id = request.values['page'] page = Page.objects.get_or_404(id=id) # html = requests.get(page.baseurl).text screenshot = None try: phantom = PhantomJS(desired_capabilities={'acceptSslCerts': True}, service_args=['--web-security=false', '--ssl-protocol=any', '--ignore-ssl-errors=true'], port=8888) phantom.set_window_size(1024, 768) phantom.get(page.baseurl) html = phantom.page_source screenshot = phantom.get_screenshot_as_png() phantom.close() except Exception as ex: html = "error when i snap your page ... %s" % ex snap = Snap(html, datetime.datetime.now(), screenshot).save() page.update(push__snaps=snap) snap = Snap(html, datetime.datetime.now(), screenshot).save() page.update(push__snaps=snap) return jsonify({'id': "%s" % snap.id})
def render(gist_id, commit): block_url = 'http://bl.ocks.org/' + gist_id d3_block_rec = {'gist_id': gist_id} try: driver = PhantomJS() driver.get(block_url) time.sleep(RENDER_DELAY) # let it render fullpage_im = Image.open(BytesIO(driver.get_screenshot_as_png())) fimb = BytesIO() fullpage_im.save(fimb, 'png') d3_block_rec['fullpage_base64'] = base64.b64encode(fimb.getvalue()) d3_block_rec['block_url'] = driver.current_url except Exception as e: # we got nothing with LittlePGer('dbname=' + DB_NAME, commit=commit) as pg: d3_block_rec['error'] = str(e) pg.insert('d3_block', values=d3_block_rec) exit(10) try: f = driver.find_element_by_xpath('//iframe') x, y = int(f.location['x']), int(f.location['y']) w, h = x + int(f.size['width']), y + int(f.size['height']) block_im = fullpage_im.crop((x, y, w, h)) bimb = BytesIO() block_im.save(bimb, 'png') d3_block_rec['block_base64'] = base64.b64encode(bimb.getvalue()) d3_block_rec['block_size'] = list(block_im.size) except Exception as e: # at least we got the fullpage im, save it with LittlePGer('dbname=' + DB_NAME, commit=commit) as pg: d3_block_rec['error'] = str(e) pg.insert('d3_block', values=d3_block_rec) exit(11) # all good, save everything with LittlePGer('dbname=' + DB_NAME, commit=commit) as pg: pg.insert('d3_block', values=d3_block_rec)