Giter Club home page Giter Club logo

ap-election-loader's Introduction

AP Elections Data Loader

Many news organizations use data from The Associated Press to power their election results reporting and real-time interactive maps. The code in this repository has been used by The Huffington Post since the 2012 Iowa caucuses to build results maps for elections including the Republican primaries, the general election and the Wisconsin recall in 2012 as well as the special elections in South Carolina and Massachusetts in 2013.

This repository is not affiliated with The Associated Press. You must have a contract with the AP and an account on its FTP server to use this code.

This repository has a single purpose: to get results off the AP's FTP server and into MySQL as fast as possible. It does not contain methods to query those results, and does not make assumptions about the front-end used to display the loaded data.

Getting started

  1. Install the necessary gems:

     bundle install
    
  2. Create local copies of the example config files:

     cp config/ap.yml.example config/ap.yml
     cp config/database.yml.example config/database.yml
    
  3. Enter your AP credentials into config/ap.yml, your database credentials into config/database.yml, and ensure the database referenced in database.yml exists locally.

  4. Import the AP's current Massachusetts data:

     ruby crawl.rb --initialize --states=MA
    

The results data from the AP FTP server is now loaded into the ap_races, ap_results and ap_candidates tables in MySQL. On subsequent imports for the current election in Massachusetts, you do not need to include the initialize option. The full list of options is described below.

Replays

The AP conducts tests of its live results reporting in the weeks leading up to an election. With the record and replay parameters, you can record these tests and replay them at a later time, which is useful for development. Recordings can be easily stored on s3, which means you can make them accessible to other developers.

To record an AP test, start recording before the test begins, and stop it after the test is over:

ruby crawl.rb --record

You can now replay that test at any time:

ruby crawl.rb --replay

To store the recording on s3, create an s3.yml config file from the example file provided, fill in your account information, and upload it:

ruby upload_replay.rb

Once uploaded, you can run that replay from any machine that has a corresponding s3.yml:

ruby crawl.rb --replay

By default, the newest replay will always be run, but you can change that with the replaydate option.

Posthooks

Posthooks allow you to create code that is run every time new results are imported. For example, at the Huffington Post, we often bake out static pages each time results are updated.

To add a posthook, copy the example file:

cp posthook/posthook.rb.example posthook/posthook.rb

Each time results have been updated, the run method in your posthook will be called. You can add any code you need to that file, and add libraries or other external dependencies to the posthook directory.

All Options

The following options are available to crawl.rb. Any option listed without examples is boolean and defaults to false.

  • states: Comma-separated states to download
    • examples: MA, MA,CA, all
  • initialize: Create initial set of results records
  • once: Only download and import data once
  • clean: Clean the data directories for specified states before downloading
  • interval: Interval in seconds at which AP data will be downloaded
    • examples: 300, 600
  • posthook: Run posthook after first iteration, even if results didn't change
  • record: Record this run
  • replay: Replay the most recent run
  • replaydate: Specify date of replay to run
    • examples: 20130521, 20130523
  • replaytime: Set the results to their state at the specified time.
  • replaytimefrom: Run the replay from the specified time onward.
  • replaytimeto: Run the replay up to the specified time.
  • help: Show help dialog

Authors

Copyright

Copyright © 2013 The Huffington Post. See LICENSE for details.

ap-election-loader's People

Contributors

bycoffe avatar jayb avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.