Skip to content

Instantly share code, notes, and snippets.

@mojowen
Last active December 25, 2015 07:29
Show Gist options
  • Save mojowen/6939232 to your computer and use it in GitHub Desktop.
Save mojowen/6939232 to your computer and use it in GitHub Desktop.
Target only select Congressional Members using Sunlight API Foundation's API.

Target Congressional Members

Use these handy functions to find congessional targets by zip code. AND optionally filter those members by setting:

  • A target chamber - e.g. House or Senate
  • A target party - e.g. D or R
  • A list of target senators - either an array of names ["Ted Cruz","Mitch McConnell"], twitter handles ["SenTedCruz","McConnellPress"], or Bioguide IDs ["B001282","C001098"]

Will return an empty Sunglight API Call { results: [ ], count: 0 } if there are no results after the filter.

Here are some examples:

  • Target the entire Congressional Delegation in the 94103 zip code TweetCongress('94103',api,function(result) { ... })

  • Target all Republican Legislators in the 97227 zip code TweetCongress('97227',api,'R',function(result) { ... })

  • Target all House members in the 60616 zip code TweetCongress('60616',api,'House',function(result) { ... })

  • Target only Ted Cruz in the 73301 zip code - cause seriusly fuck that guy TweetCongress('73301',api,['Ted Cruz'],function(result) { ... })

Extending

Things I'd like to add:

  • Allow an 'exact' flag to be passed to the function - this will trigger some sort of dialog that gets lat/lng and allows better drilling down to an exact Congressional District for Zip Codes with multiple reps
  • Allow a 'fallback' list to be passed - will tweet at these folks if there aren't any results
  • Allow an 'exclude' list - WON'T tweet at these people.
  • Should integrate with committees api to allow taretting by committee
  • Could also allow for targetting based on bill votes using votes api
/**
This gist makes it really easy to target only some members of Congress using the Sunlight Congressional API:
http://sunlightlabs.github.io/congress/
What it does:
> Get your Senators or Reps from a zip code using Sunlight API Foundation's API
OPTIONALLY
> Set a target chamber (i.e. a string - "house", "senate" )
> Set a target party
> Set a target list ( i.e an array of either names ["Ted Cruz","Mitch McConnell"], twitter handles ["SenTedCruz","McConnellPress"], or Bioguide IDs ["B001282","C001098"]
> Will only return congressional reps for those zip codes - will return [] if there's no match
What you'll need:
> A Sunlight API Key - get one here: http://sunlightlabs.github.io/congress/
What I should add:
> Some optional geotargetting if it returns two house members
> Ability to add a fallback list or something if there are no results
> Ability to add an exclude list
**/
function TweetCongress(zip,apiKEY,restrictions,callback) {
var restrictions = restrictions || null;
// Allow for no restrictions
if( typeof restrictions == 'function' && callback == null ) {
var callback = restrictions;
restrictions = null
}
var improved_callback = function(result) {
if( restrictions != null ) {
var filtered = [];
// Lowercase everything
restrictions = typeof restrictions == 'string' ? restrictions.toLowerCase() : restrictions.map( function(el) { return el.toLowerCase() });
for (var i = 0; i < result.results.length; i++) {
if( typeof restrictions == 'string' ) {
// If the restriction is just a string - just specifying a chamber
if( restrictions === result.results[i].chamber.toLowerCase() ) filtered.push( result.results[i] );
if( restrictions === result.results[i].party.toLowerCase() ) filtered.push( result.results[i] )
} else {
// If the restriction is an array - check and see if the result match first + last, twitter name, or bioguide
if( restrictions.indexOf( result.results[i].first_name.toLowerCase() + ' '+result.results[i].last_name.toLowerCase() ) !== -1 ) filtered.push( result.results[i] )
if( restrictions.indexOf( result.results[i].twitter_id.toLowerCase() ) !== -1 ) filtered.push( result.results[i] )
if( restrictions.indexOf( result.results[i].bioguide_id.toLowerCase() ) !== -1 ) filtered.push( result.results[i] )
}
}
result.results = filtered;
result.count = result.results.length
result.page.count = result.results.length
}
// Do the normal callback thing
callback(result);
};
// Will use HTTPS if the page is https
JSONP(document.location.protocol+'//congress.api.sunlightfoundation.com/legislators/locate', { zip: zip, apikey: apiKEY }, function(r) { improved_callback(r) });
function JSONP(url,data,callback,request_callback) {
var d = document,
s = d.createElement('script'),
callback_name = '___'+( new Date ).getTime(),
callback_function_name = callback_name+'_callback';
var request_callback = request_callback || 'callback';
data[request_callback] = callback_function_name;
window[ callback_function_name ] = function(result) {
window[ callback_name ].parentElement.removeChild( window[ callback_name ] );
window[ callback_function_name ] = null;
callback(result);
};
s.type = "text/javascript";
s.id = callback_name;
s.src = url+"?"+serialize(data);
d.head.appendChild( s );
function serialize(data) {
if( typeof data == 'string' ) return data;
var serialized = '';
for( var i in data ) serialized += escape(i) + '=' + escape( data[i])+'&';
return serialized;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment