Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import datetime

def db_insert(bill, author, sub, post_t):
"""Stores information about bots activity in MySQL database.
Including, username of Reddit user who called the bot, subreddit where the user posted his reequest, date of request and general information about requested legislation.
"""
chamber = bill.bill[:1]
billid = int(filter(str.isdigit, str(bill.bill)))
billtype = ""
Expand All @@ -25,6 +28,7 @@ def db_insert(bill, author, sub, post_t):
cursor = db.cursor()

try:
"""Inserts data into table bills. If INSERT fails, rolls back database and prints exception message."""
cursor.execute("INSERT INTO bills (chamber, billid, billtype, congress, link_author, subreddit, date_linked, post_type) VALUES('%s', %d, '%s', %d, '%s', '%s', '%s', '%s')" % (chamber, billid, billtype, congress, link_author, subreddit, date_linked, post_type))
db.commit()
except Exception as e:
Expand Down
3 changes: 3 additions & 0 deletions mention_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
pp = ProPublica(const.PROPUB_KEY)

def bot():
"""Searches Reddit for mentions of the bot.
If found replies with information about legislation user requested."""
for mention in reddit.inbox.stream():
if "+/u/congress_bill_bot" in mention.body.lower():
print "************SUMMONED*************"
Expand All @@ -31,6 +33,7 @@ def bot():
comment.reply("Sorry, I couldn't seem to find that bill.")

while True:
"""Main program loop. Runs until interrupted by user or until exception occurs."""
try:
bot()
except KeyboardInterrupt:
Expand Down
13 changes: 13 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
from datetime import datetime

def parse_url(url):
"""Returns type of legislation and its ID.
arguments:
url - URL of legislation from Congress.gov
"""
url_parts = url.split('/')
congress, chamber, billid = url_parts[4], url_parts[5], url_parts[6].split('?')[0]

Expand Down Expand Up @@ -35,9 +39,18 @@ def parse_url(url):
return congress_number, full_billid

def find_urls(comment):
"""Returns URL found in Reddit comment.
arguments:
comment - Reddit comment to be searched for potential URLs
"""

return re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', comment)

def format_comment_from_bill(bill):
"""Retuns string describing the legislation, formatted as a Reddit comment.
arguments:
bill - JSON object describing legislation obtained from Congress
"""
newline = " \n"

comment = u"#\U0001F3DB **Here is some more information about [" + bill.bill + "](" + bill.congressdotgov_url + ")**" + (" - [PDF](" + bill.pdf + ")" + newline if len(bill.pdf) > 0 else "" + newline)
Expand Down