We'd like to welcome python-sunlight into the most excellent family of open-source projects maintained by Labs. This particular project aims to unify and normalize the Sunlight APIs into a single Python library that's easy to understand, use, and fun to play with.
This library currently supports our Congress API, Open States API, and Capitol Words API. As such we're deprecating the old python-sunlightapi and python-openstates libraries. They'll still work but will no longer be receiving updates, so switching is highly recommended.
This library has some neat features that should make migration painless - as well as some new features, such as a standardized location to place your Sunlight API Key, which makes testing (as well as distributing) your app even easier.
We've just released version 1.0.1 over on PyPI, which makes installation a snap on any system with
pip. The documentation is fairly complete, but feedback is super welcome -- we're eager to learn where folks get stuck.
Most of the bugs seemed to be worked out after the Boston Python Project Night, where we had a few folks test out the library. A special thanks to all our beta-testers!
Alright, so how do I get started?
Hacking on python-sunlight is super easy. Here's how to get setup.
You'll need an API key. If you've not done so, get an API key (it's alright, we'll wait, go ahead).
Back already? Great.
Now, you'll have gotten the email that has a long-ish string of letters and numbers - let's save this to
~/.sunlight.key (where python-sunlight will look for a key). If you already had a key, it'd be worth it to go and dig it up.
If you're on a UNIX-type (MacOS, GNU/Linux, *BSD, AIX or Solaris (or any of the other POSIX-ey systems)) machine, you should be able to run a command that looks like the following:
echo "your-api-key-here" > ~/.sunlight.key
It's worth mentioning that
your-api-key-here should actually be your API key that was emailed to you up above.
Next, you should install python-sunlight via
pip is not installed on your system, please download and install pip.
pip install sunlight
And you're good to go!
Without further ado, an example!
#!/usr/bin/env python # Copyright (c) 2012, BSD-3 clause, Sunlight Labs from sunlight import capitolwords from sunlight import congress phrase = "death metal" # Today, we'll be printing out the Twitter IDs of all legislators that use # this phrase most in the congressional record. for cw_record in capitolwords.phrases_by_entity( "legislator", # We're getting all legislators sort="count", # sorted by how much they say phrase=phrase, # this word )[:6]: # We'll just try the top 5 legislators legislator = congress.legislators( bioguide_id=cw_record['legislator'], # Look up this biogude (unique ID) # for every fed. legislator all_legislators="true" # search retired legislators ) if len(legislator) >= 1: # If we were able to find the legislator legislator = legislator # (this is a search, so it's a list) if legislator['twitter_id'] != "": # and they have a Twitter ID print "%s. %s (@%s) said %s %s times" % ( legislator['title'], legislator['lastname'], legislator['twitter_id'], phrase, int(cw_record['count']) ) # Print it to output :)
The output looks like this:
Sen. Feingold (@russfeingold) said death metal 979 times Rep. Jackson Lee (@JacksonLeeTX18) said death metal 923 times Sen. Leahy (@SenatorLeahy) said death metal 800 times Sen. Kyl (@senjonkyl) said death metal 755 times Sen. Durbin (@SenatorDurbin) said death metal 593 times
And once more (this time, searching for "san francisco"):
Rep. Filner (@CongBobFilner) said san francisco 1346 times Sen. Feinstein (@senfeinstein) said san francisco 1288 times Sen. Boxer (@senatorboxer) said san francisco 1181 times Rep. Pelosi (@NancyPelosi) said san francisco 1135 times Rep. Eshoo (@RepAnnaEshoo) said san francisco 677 times
Questions, concerns, bugs, patches, examples and virtual hugs are all welcome on our GitHub page, so please do check it out!