Skip to content

Instantly share code, notes, and snippets.

@neilkod
Created February 16, 2011 23:22
Show Gist options
  • Save neilkod/830531 to your computer and use it in GitHub Desktop.
Save neilkod/830531 to your computer and use it in GitHub Desktop.
tweets the national debt
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