nprapps/nyt-ap-elections

Name: nyt-ap-elections

Owner: NPR visuals team

Description: A wrapper for the AP v2 Elections API.

Created: 2015-10-29 18:16:24.0

Updated: 2015-10-29 18:16:25.0

Pushed: 2015-11-09 19:50:49.0

Homepage:

Size: 183

Language: Python

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Requirements
Optional requirements:
Installation

Install the Python library:

install nyt-ap-elections

Set your AP API key:

rt AP_API_KEY=<MY_AP_API_KEY>
Additional configuration for demo

Make sure you are running PostgreSQL. Then create your db and user:

tedb elex
teuser elex
Usage
Demo app
on -m elex.demo
Modules

Use the election loader manually from within your project.

 elex.parser import api
 elex import loader
 elex.loader import postgres

api.Election(electiondate='2015-11-03', testresults=False, liveresults=True, is_test=False)
races = e.get_races(omitResults=False, level="ru", test=False)

s, reporting_units, candidate_reporting_units = e.get_units(raw_races)
idates, ballot_positions = e.get_uniques(candidate_reporting_units)

APPING = [
(postgres.Candidate, candidates),
(postgres.BallotPosition, ballot_positions),
(postgres.CandidateReportingUnit, candidate_reporting_units),
(postgres.ReportingUnit, reporting_units),
(postgres.Race, races)


er.ELEX_PG_CONNEX.connect()
er.ELEX_PG_CONNEX.drop_tables([mapping[0] for mapping in DB_MAPPING], safe=True)
er.ELEX_PG_CONNEX.create_tables([mapping[0] for mapping in DB_MAPPING], safe=True)

obj, obj_list in DB_MAPPING:
with loader.ELEX_PG_CONNEX.atomic():
    for idx in range(0, len(obj_list), 2000):
        obj.insert_many([o.__dict__ for o in obj_list[idx:idx+2000]]).execute()

er.ELEX_PG_CONNEX.close()
Options
Recording
Flat files

Will record timestamped and namespaced files to the ELEX_RECORDING_DIR before parsing.

rt ELEX_RECORDING=flat
rt ELEX_RECORDING_DIR=/tmp
MongoDB

Will record a timestamped record to MongoDB, connecting via ELEX_RECORDING_MONGO_URL and writing to the ELEX_RECORDING_MONGO_DB database.

rt ELEX_RECORDING=mongodb
rt ELEX_RECORDING_MONGO_URL=mongodb://localhost:27017/  # Or your own connection string.
rt ELEX_RECORDING_MONGO_DB=ap_elections_loader
Development
Run tests
tests

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.