diff --git a/analytics.py b/analytics.py index 7e04fbd..e723457 100644 --- a/analytics.py +++ b/analytics.py @@ -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 = "" @@ -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: diff --git a/mention_bot.py b/mention_bot.py index e7cf97c..0f8b370 100644 --- a/mention_bot.py +++ b/mention_bot.py @@ -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*************" @@ -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: diff --git a/utils.py b/utils.py index 8350a74..9aa7160 100644 --- a/utils.py +++ b/utils.py @@ -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] @@ -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)