r/watchingcongress • • Apr 29 '12

Bot code

I started on some real basic code to get the house feed pulled in. I have to roll out for a bit so if anyone wants to help improve this here is what I have so far:

import reddit
import feedparser
from pymongo import Connection

mdb = Connection()
wcdb = mdb.wc
hclc = wcdb.wc_house_today

r = reddit.Reddit(user_agent='WatchingCongress/1.0')
r.login('congressbot', '*********')

hf = feedparser.parse('http://thomas.loc.gov/home/rss/housefloortoday.xml')

for entry in hf.entries:
  if hclc.find_one({'guid': entry.guid}) == None:
    record = {'title': entry.title,
      'description': entry.description,
      'link': entry.link,
      'guid': entry.guid}
    hclc.insert(record)
    r.submit('watchingcongress', entry.title + ': ' + entry.description,
      url=entry.link)

I haven't used the reddit API before and I seem to be running into some errors regarding captcha. If anyone has any insight or wants to add to this post here, and if not I will work on it when I get back this evening!

103 Upvotes

18 comments sorted by

24

u/All-American-Bot Apr 29 '12

Hi,

I've written some bots - happy to help if you have questions. I think the two things you are missing:

  • sleep for (at least) 2 secs after each submit

  • when your bot is young it will have low karma and therefore can't post often to make it work robustly do something like:

          try:
              c.reply(resp)
          except reddit.errors.RateLimitExceeded as detail:
              print detail
              print "We are talking too fast, waiting %f and trying again" % (detail.sleep_time)
              sleep(detail.sleep_time + 5)
              c.reply(resp)

This properly handles the 'captcha' case

9

u/handsoffme Apr 30 '12

Much thanks, I will throw this in.

12

u/sumzup Apr 30 '12

Add your bot as an approved submitter to the subreddit.

16

u/sumzup Apr 30 '12

handsoffme can just add this bot as an approved submitter to this subreddit.

8

u/All-American-Bot Apr 30 '12

ah - the best possible fix!

10

u/piqueoil Apr 30 '12

Just throwing this out there:

http://www.opencongress.org/bill/pending

It may be more useful to scrape that periodically instead of the Thomas XML feed. Although it's nice to get it straight from the source, OpenCongress provides really short, jargon-free summaries for each bill (ex: "To extend the temporary suspension of duty on electromechanical ice shavers, with self-contained electric motor"). They could be incorporated into the post title.

However, there doesn't appear to be an RSS feed for recent actions.

4

u/handsoffme Apr 30 '12

Getting this data seems to be one of the most common requests from the thread, they have an API I will look into whether it is data we can legally pull into here.

7

u/dacjames Apr 30 '12

Here's the api. Bills introduced since <date> seems like exactly what you need. Checking that feed every hour would probably do the trick and you wouldn't have to worry about filtering duplicates yourself.

Throw the code in a github repository and I'll gladly help out.

2

u/[deleted] Apr 30 '12

Use govtrack. It's what opencongress is based off of and encourages reuse.

1

u/clonedredditor Apr 30 '12

There's also these API's at Sunlight Foundation.

http://services.sunlightlabs.com/

The Real Time Congress API looks interesting.

5

u/xNaVx Apr 30 '12

I really love the idea of this subreddit. Although I don't know as much about coding as I wish I did, I have a couple of suggestions:

  • Instead of having the bot post a link to the bill (probably the OpenCongress version), how about instead creating a self post, with information such as a brief summary (like the intention), a link to the OpenCongerss version, a link to the CRS Summary (Layman's explanation) and if possible, a link to the CBO analysis?

  • Also it may be useful to have bills that are going through committee action now, so we can see what's going on before they're already voted on.

1

u/handsoffme Apr 30 '12

I like the self post idea, I'll see what I can figure out from the additional APIs suggested above.

3

u/CivicsMod Apr 30 '12

Why does it say what day it is? The link is automatically kept up to date. completely unnecessary to put that in the title.

3

u/p-static Apr 30 '12

You should really put this up on Github or something like that!

2

u/chrisguitarguy Apr 30 '12

As someone suggested, using govtrack seems like a good idea.

I wrote some stuff that will parse govtrack's xml and stick it into MongoDB.

https://gist.github.com/2555847

There is a lot of data on govtrack, something like 9000 bills for the 112th congress.

1

u/Derferman Apr 30 '12

Can you create a github repository for the code? It should make collaborating much easier. We can also host in on Heroku for free so we don't have to worry about paying for servers