Created
February 16, 2011 23:22
-
-
Save neilkod/830531 to your computer and use it in GitHub Desktop.
tweets the national debt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tweepy | |
from BeautifulSoup import BeautifulSoup | |
import urllib2 | |
CONSUMER_KEY = 'oh' | |
CONSUMER_SECRET = 'no' | |
ACCESS_KEY = 'you' | |
ACCESS_SECRET = 'dont' | |
def get_national_debt(): | |
page = urllib2.urlopen('http://www.treasurydirect.gov/NP/BPDLogin?application=np') | |
soup = BeautifulSoup(page) | |
debt = soup.find('table',{'class':'data1'}).findAll('td')[3].text | |
asof = soup.find('table',{'class':'data1'}).findAll('td')[0].text | |
population_url = 'http://www.census.gov/main/www/popclock.html' | |
population_page = urllib2.urlopen(population_url) | |
soup = BeautifulSoup(population_page) | |
population = soup.find('span',{'id':'usclocknum'}).text | |
debt_amount = float(debt.replace(',','')) | |
population_amount = int(population.replace(',','')) | |
per_person = debt_amount / population_amount | |
return "US National debt as of %s is $%s, or $%.2f for each person(%s) in the US" % (asof, debt,per_person,population) | |
tweet_string = get_national_debt() | |
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) | |
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET) | |
api = tweepy.API(auth) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment