예제 #1
0
파일: app.py 프로젝트: nprapps/idp-georgia
def _episode(filename):
    """
    Example view demonstrating rendering a simple HTML page.
    """
    key = filename.split('.')[0]
    if not os.path.exists('data/%s' % filename):
        abort(404)

    with open(app_config.EPISODE_DOCUMENTS[key]['path']) as f:
        html = f.read()

    context = make_context()
    doc = CopyDoc(html)
    parsed_document = parse_doc.parse(doc)
    context.update(parsed_document)
    context.update({
        'episode': key,
        'next': app_config.EPISODE_DOCUMENTS[key]['next']})
    return make_response(render_template('episode.html', **context))
예제 #2
0
def parse_document(html):
    doc = CopyDoc(html)
    parsed_document = parse_doc.parse(doc)

    return parsed_document
예제 #3
0
def lambda_handler(event, context):
    """
    Retrieves drive keys from the request payload
    - connects to google using authomatic and OAuth2 credentials
    - parses the factcheck document and publishes to staging
    """
    try:
        try:
            logger.info('Start preview generation')
            TRANSCRIPT_GDOC_KEY = event['doc_key']
            AUTHORS_GOOGLE_DOC_KEY = event['authors_key']
        except KeyError:
            msg = 'Did not find needed params in %s' % (event)
            raise app_config.UserException('[BadRequest]: %s' % msg)
        authors_url = app_config.SPREADSHEET_URL_TEMPLATE % (
            AUTHORS_GOOGLE_DOC_KEY)
        doc_url = app_config.DOC_URL_TEMPLATE % (TRANSCRIPT_GDOC_KEY)

        # Get the credentials and refresh if necesary
        credentials = app_config.authomatic.credentials(
            app_config.GOOGLE_CREDS)
        # Refresh credentials if needed
        if not credentials.valid:
            credentials.refresh()

        # Get authors
        response = app_config.authomatic.access(credentials, authors_url)
        if response.status != 200:
            msg = 'While accessing %s got HTTP: %s' % (authors_url,
                                                       response.status)
            raise app_config.UserException('[BadRequest]: %s' % msg)
        authors_data = response.content
        authors = transform_authors(authors_data)
        if not authors:
            msg = 'Could not parse authors spreadsheet %s' % (authors_url)
            raise app_config.UserException('[BadRequest]: %s' % msg)
        # Get doccument
        response = app_config.authomatic.access(credentials, doc_url)
        if response.status != 200:
            msg = 'While accessing %s got HTTP: %s' % (doc_url,
                                                       response.status)
            raise app_config.UserException('[BadRequest]: %s' % msg)
        html = response.content

        # Parse data
        doc = CopyDoc(html)
        logger.info('Parsed doc html with copydoc')
        context = parse_doc.parse(doc, authors)
        logger.info('Parsed factcheck')

        # Generate final files and upload to S3
        upload_template_contents(context, 'factcheck.html')
        upload_template_contents(context, 'share.html')
        context['preview'] = True
        upload_template_contents(context, 'factcheck.html',
                                 'factcheck_preview.html')
        logger.info('Generated factcheck templates. Execution successful')
        return {'message': 'Preview generated successfully'}
    except app_config.UserException, e:
        logger.error('Exit with controlled exception %s' % e)
        raise