Permalink
Browse files

Add simple parse test. Add self post template

  • Loading branch information...
1 parent bf58966 commit 067e81c85c20fc20a3f53c8f1ace5adc730b3e1d @kyleconroy committed May 1, 2012
Showing with 76 additions and 29 deletions.
  1. +27 −0 .gitignore
  2. +32 −29 congressbot.py
  3. +4 −0 post_template.md
  4. +1 −0 requirements.txt
  5. +2 −0 tests/requirements.txt
  6. +10 −0 tests/test_bot.py
View
@@ -0,0 +1,27 @@
+*.py[co]
+
+# Packages
+*.egg
+*.egg-info
+dist
+build
+eggs
+parts
+bin
+var
+sdist
+develop-eggs
+.installed.cfg
+
+# Installer logs
+pip-log.txt
+
+# Unit test / coverage reports
+.coverage
+.tox
+
+#Translations
+*.mo
+
+#Mr Developer
+.mr.developer.cfg
View
@@ -1,47 +1,50 @@
#!/usr/bin/python
import logging
-import reddit
import feedparser
import time
+from jinja2 import Template
from pymongo import Connection
+from reddit import Reddit
mongo_db = Connection()
congress_db = mongo_db.wc
house_collection = congress_db.wc_house_today
-r = reddit.Reddit(user_agent='WatchingCongress/1.0')
-r.login('congressbot', '<BOTPASS>')
+template = Template(open('post_template.md').read())
-govfeed = feedparser.parse('http://www.govtrack.us/events/events.rss?'
- 'feeds=misc%3Aintroducedbills')
+def parse():
+ govfeed = feedparser.parse('http://www.govtrack.us/events/events.rss?'
+ 'feeds=misc%3Aintroducedbills')
-POST_TEMPLATE = """
-{description}
+ r = Reddit(user_agent='WatchingCongress/1.0')
+ r.login('congressbot', '<BOTPASS>')
-[Govtrack.us Summary]({link})
-"""
+ for entry in govfeed.entries:
+ if not entry['guid'].find('guid'):
+ logging.info("Couldn't find GUID")
+ continue
-for entry in govfeed.entries:
+ if house_collection.find_one({'guid': entry['guid']}):
+ logging.info("Already created story: {}".format(entry['title']))
+ continue
- if not entry['guid'].find('guid'):
- continue
+ record = {
+ 'title': entry['title'],
+ 'description': entry['description'],
+ 'link': entry['link'],
+ 'guid': entry['guid'],
+ }
- if house_collection.find_one({'guid': entry['guid']}):
- continue
+ try:
+ text = template.render(description=entry['description'],
+ link=entry['link'])
+ r.submit('watchingcongress', entry['title'], text=text)
+ house_collection.insert(record)
+ logging.info("Created story: {}".format(entry['title']))
+ except Exception as e:
+ logging.error("Exception occured: {}".format(unicode(e)))
+ time.sleep(2)
- record = {
- 'title': entry['title'],
- 'description': entry['description'],
- 'link': entry['link'],
- 'guid': entry['guid'],
- }
- try:
- text = SELF_POST.format(description=entry['description'],
- link=entry['link'])
- r.submit('watchingcongress', entry['title'], text=text)
- house_collection.insert(record)
- logging.info("Created story: {}".format(entry['title']))
- except Exception as e:
- logging.error("Exception occured: {}".format(unicode(e)))
- time.sleep(2)
+if __name__ == '__main__':
+ parse()
View
@@ -0,0 +1,4 @@
+{{description}}
+
+[Govtrack.us Summary]({{link}})
+
View
@@ -2,3 +2,4 @@ feedparser==5.1.1
pymongo==2.2
reddit==1.3.7
six==1.1.0
+Jinja2==2.6
View
@@ -0,0 +1,2 @@
+nose==1.1.2
+mock==0.8.0
View
@@ -0,0 +1,10 @@
+from mock import patch
+import logging
+import congressbot
+
+
+@patch('congressbot.house_collection')
+@patch('congressbot.Reddit')
+def test_feed_parse(reddit_mock, house_mock):
+ house_mock.find_one.return_value = False
+ congressbot.parse()

0 comments on commit 067e81c

Please sign in to comment.