npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ballot-tally

v1.0.0

Published

Elections Results via AP Elections API 2.x

Downloads

1

Readme

ballot-tally 卌

A lightweight node.js wrapper for the U.S. AP Elections API 2.x.

Here's a minimal example, which fetches results for Presidential primaries held on April 26, 2016.

var ballotTally = require('ballot-tally');
var client = new ballotTally('v2', process.env.AP_API_KEY);
var query = new ballotTally.Query.Elections(client, '2016-04-26');

query.level('fipscode').officeID('P').raceType('D').raceType('R')
  .fetch(function(err, data) {
      if (!err) console.log(data);
    });

Use this lightweight library to quickly create a script to fetch raw results data from the AP.

For heavy lifting, check out the database-ready library Elex, developed by our friends at The New York Times & NPR.

Installation

$ npm install ballot-tally

Usage

  1. Get an API key from the AP, then set it as the AP_API_KEY environment variable
$ export AP_API_KEY='your_key_here'
  1. Create a client object
var ballotTally = require('ballot-tally');

var apiKey = process.env.AP_API_KEY;

var client = new ballotTally('v2', apiKey);
  1. Create a query builder
var query = new ballotTally.Query.Elections(client, '2012-11-06');
  1. Build query to filter results based on criteria
query.statePostal('FL')
  .test(false)
  .level('fipscode')
  .officeID('P')
  .raceType('G');
  1. Fetch results
query.fetch(function(err, data) {
  if (!err) {
    console.log("%j", data);
  }
  else {
    console.error(err);
  }
});

Raw queries

Instead of using the query builder to fetch results, you can connect to the API with raw queries.

// This makes a query which is identical to the previous example.

var ballotTally = require('ballot-tally');

var apiKey = process.env.AP_API_KEY;

var client = new ballotTally('v2', apiKey);

client.elections('2012-11-06',
  {
    statepostal: 'FL',
    test: false,
    level: 'fipscode',
    officeID: 'P',
    raceTypeId: 'G'
  },
  function(err, data) {
    if (!err) {
      console.log("%j", data);
    }
    else {
      console.error(err);
    }
  }
);

Query options

Query options match with the API filtering parameters. Please refer to API documentation for more information.

Common queries

Get top level (State) results for Primaries:

query.level('state').officeID('P').raceType('D').raceType('R')
  .fetch(function(err, data) {
      if (!err) console.log(data);
    });

Get county level results for Primaries (to render county maps):

query.level('fipscode').officeID('P').raceType('D').raceType('R')
  .fetch(function(err, data) {
      if (!err) console.log(data);
    });

Get town level results for New England states (to render town maps):

query.level('ru').officeID('P').raceType('D').raceType('R')
  .fetch(function(err, data) {
      if (!err) console.log(data);
    });

Get delegate count for Primaries:

Delegate count is reported only at district level for Presidential Primaries.

query.level('district').officeID('P').raceType('D').raceType('R')
  .fetch(function(err, data) {
      if (!err) console.log(data);
    });

Get county level results for Presidential General election (to render national maps):

query.level('fipscode').officeID('P').raceType('G').national(true)
  .fetch(function(err, data) {
      if (!err) console.log(data);
    });

States where a winner yet to be called for Presidential General election:

query.level('state').officeID('P').raceType('G').national(true).winner('U')
  .fetch(function(err, data) {
      if (!err) console.log(data);
    });

U.S. House General elections - results for all districts:

query.level('district').officeID('H').raceType('G').national(true)
  .fetch(function(err, data) {
      if (!err) console.log(data);
    });

More examples

See the examples directory for further sample scripts.

Notes

This library was developed independently by The Wall Street Journal. It is not endorsed by Associated Press.

Version history

v1.0.0 (2016-04-26)

  • Initial public release

License

ISC