forked from kylejfrost/Congress_Bill_Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomment_bot.py
More file actions
84 lines (63 loc) · 2.63 KB
/
comment_bot.py
File metadata and controls
84 lines (63 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# -*- coding: utf-8 -*-
import const
import utils
import praw
import re
import sys
import analytics
from propub import ProPublica
reddit = praw.Reddit(client_id=const.CLIENT_ID,
client_secret=const.CLIENT_SECRET,
user_agent=const.USER_AGENT,
username=const.USERNAME,
password=const.PASSWORD)
pp = ProPublica(const.PROPUB_KEY)
def bot():
for comment in reddit.subreddit(sys.argv[1]).stream.comments():
if comment.author.name == "Congress_Bill_Bot":
continue
urls = utils.find_urls(comment.body)
if len(urls) > 0:
bills = []
for url in urls:
url = url.replace(")", "")
if "congress.gov/bill" not in url:
continue
print "\n***************URL*****************"
print "Working on comment: " + comment.permalink(fast=True)
print "Working on: " + url
congress, bill_id = utils.parse_url(url)
print "Found Congress: " + congress + ", and Bill: " + bill_id
print "Adding bill to list."
bill = pp.get_bill(congress, bill_id)
print "Got Bill titled: " + bill.title
bills.append(bill)
if len(bills) > 0:
print "---------- Working on Bills! ----------"
reply = ""
for bill in bills:
print "Adding bill to reply: " + bill.title
reply = reply + " \n***** \n" + utils.format_comment_from_bill(bill)
analytics.db_insert(bill, comment.author.name, comment.subreddit, "C")
comment.reply(reply)
print "I replied to: https://reddit.com" + comment.permalink()
elif "+/u/Congress_Bill_Bot [[" in comment.body:
print "************SUMMONED*************"
print "Comment: " + comment.permalink(fast=True)
try:
congress, bill_id = re.search(r'\[\[(.*?)\]\]', comment.body).group(1).lower().replace(" ", "").replace(".", "").split(",")
bill = pp.get_bill(congress, bill_id)
reply = utils.format_comment_from_bill(bill)
analytics.db_insert(bill, comment.author.name, comment.subreddit, "C")
comment.reply(reply)
print "I replied to: " + comment.permalink()
except:
comment.reply("Sorry, I couldn't seem to find that bill.")
while True:
try:
bot()
except KeyboardInterrupt:
exit()
except Exception as e:
print e
pass