AP ELECTION LOADER
Relies on Elex, a command-line tool to get results from the AP Election API 2.0. Demonstrates a method putting those results into a Postgres database using the COPY method and the loader's CSV output.
The following things are assumed to be true in this documentation.
- You are running OSX.
- You are using Python 2.7. (Probably the version that came OSX.)
- You have pip, virtualenv and virtualenvwrapper installed and working.
See "Chapter 2: Install Virtualenv" of NPR's development environment blog post for details.
Having trouble on OS X El Capitan? See: Can't install virtualenvwrapper on OSX 10.11 El Capitan.
mkvirtualenv elex-loader git clone firstname.lastname@example.org:newsdev/elex-loader.git && cd elex-loader pip install -r requirements.txt ./scripts/$ENV/bootstrap.sh
bootstrap.sh script will create databases and the user necessary for local development. Note: This does not exist for non-development environments. Please use commands in elex-dotfiles instead.
The New York Times defines a handful of different environments; principally,
dev: Hits test URLs by default. Assumes a local Postgres database where the local user is a superuser.
stg: Hits test URLs by default. Requires a Postgres user / host / password to be defined in the environment. We use a
.pgpassfile and export the rest in
/etc/environment. Check out elex-dotfiles for more.
prd: Hits live URLs by default. Requires a Postgres user / host / password to be defined in the environment.
Load initial data
The AP will make "live zeros" available in the morning of an election day. You can run
reload.sh to get an entire new set of data, including races, reporting units, candidates and zeroed-out results.
Load results on election night
The daemon will run 100,000 times (seriously) unless it is stopped. We control ours with a custom Supervisord instance and a modified
/etc/supervisord.conf. This configuration file is available in elex-dotfiles along with other secrets.
Set a wait interval
You might want to control how long the daemon waits between cycles. This is hardcoded to a default -- 15s in production, 30s elsewhere. You can create the file
/tmp/elex_loader_timeout.sh and export an
ELEX_LOADER_TIMEOUT variable like this:
The daemon checks for this file and sources it if it exists in every loop, which means you can dynamically control the wait time. For example, we do this in our admin.
Load results once
Sometimes you just need to load a single update, e.g., to grab final results after we've turned off the loader. This command will get new results without baking or reloading any other parts of the database.
Load delegate data
Often, the AP will update delegate information after our daemon has stopped running. To update just delegates for a given racedate, run this command.