Beispiel #1
0
def snap(conn, url, cookie_name, cookie_value, width, height, loaded, hides, selector):
    """Handle all the work of taking the page snapshot.

    The first parameter is a connection object for a `multiprocessing.Pipe`
    that we use to send back the file name written to. The remaining parameters
    are `multiprocessing.Value`s.
    
    """
    ghost = None
    try:
        ghost = Ghost(viewport_size=(width.value, height.value))
        ghost.wait_timeout = 20

        headers = {}
        if cookie_name.value and cookie_value.value:
            headers = {
                'Cookie': str('%s=%s' % (cookie_name.value, cookie_value.value)),
            }

        ghost.open(url.value, headers=headers)

        if loaded.value:
            try:
                ghost.wait_for_selector('%s' % loaded.value)
            except:
                # if the selector never appears, we don't care
                pass

        selectors = hides.value.split(',')
        if len(selectors):
            hide_js = r'''
                if (jQuery) {
                    $(document).ready(function() {
                        %s
                    });
                }
            ''' % '\n'.join([r"$('%s').hide();" % sel for sel in selectors])
            ghost.evaluate(hide_js)

        handle, file_path = mkstemp(prefix='ansel_snap', suffix='.png')
        ghost.capture_to(file_path, selector=selector.value)

        conn.send(file_path)
    finally:
        del ghost
        conn.close()
Beispiel #2
0
def heroku(api_key, year, month):
    ghost = None

    file_path = '/tmp/heroku_invoice_%d-%d.png' % (year, month)
    if os.path.exists(file_path):
        os.remove(file_path)

    # TODO: make dimensions configurable? Automatic (is that possible?)?
    ghost = Ghost(viewport_size=(1000, 1600))
    ghost.wait_timeout = 20

    ghost.open('https://id.heroku.com/login')
    ghost.fill("form", dict(email="", password=api_key))
    ghost.fire_on("form", "submit", expect_loading=True)
    ghost.open('https://dashboard.heroku.com/invoices/%s/%s' % (year, month))

    ghost.capture_to(file_path)

    return file_path