|
| 1 | +import ConfigParser |
| 2 | +import datetime |
| 3 | +import json |
| 4 | +import OAuth2Util |
| 5 | +from pprint import pprint |
| 6 | +import praw |
| 7 | +import requests |
| 8 | +import sys, traceback |
| 9 | +from TheBlueAlliance import * |
| 10 | + |
| 11 | +# Read the config file |
| 12 | +config = ConfigParser.ConfigParser() |
| 13 | +config.read("settings.cfg") |
| 14 | +SUBREDDIT = config.get("Reddit", "subreddit") |
| 15 | +# Eventually this will be based off of TBA exposing the week |
| 16 | +# As of now, it's only useful for the post title anyway |
| 17 | +CURRENT_WEEK = '5' |
| 18 | +HEADERS = {'X-TBA-App-Id': config.get("TBA", "appid")} |
| 19 | +URL = 'http://www.thebluealliance.com/api/v2/' |
| 20 | + |
| 21 | +# Datetime stuff for current week dates |
| 22 | +today = datetime.date.today() |
| 23 | +week_start_datetime = today - datetime.timedelta(days=today.weekday()) |
| 24 | +week_start = week_start_datetime.strftime('%Y-%m-%d') |
| 25 | +week_end_datetime = week_start_datetime + datetime.timedelta(days=6) |
| 26 | +week_end = week_end_datetime.strftime('%Y-%m-%d') |
| 27 | + |
| 28 | +def get_events(): |
| 29 | + pprint('grabbing events...') |
| 30 | + r = requests.get(URL + 'events/2016', headers=HEADERS) |
| 31 | + events = r.json() |
| 32 | + |
| 33 | + return events |
| 34 | + |
| 35 | +def main(): |
| 36 | + try: |
| 37 | + # Setup PRAW instance |
| 38 | + pprint('initializing reddit connection...') |
| 39 | + reddit = praw.Reddit(user_agent=config.get("Reddit", "useragent")) |
| 40 | + o = OAuth2Util.OAuth2Util(reddit) |
| 41 | + o.refresh(force=True) |
| 42 | + toc = ''; |
| 43 | + |
| 44 | + # If the post exists, exit. Otherwise, continue. |
| 45 | + pprint('checking if post exists...') |
| 46 | + sub = reddit.get_subreddit(SUBREDDIT) |
| 47 | + search = reddit.search('[Discussion] Week '+CURRENT_WEEK, subreddit=SUBREDDIT, sort='new', period='month') |
| 48 | + search_list = list(search) |
| 49 | + for post in search_list: |
| 50 | + if post.title == '[Discussion] Week '+CURRENT_WEEK: |
| 51 | + pprint('post already exists! exiting.') |
| 52 | + return |
| 53 | + |
| 54 | + pprint('posting the thread...') |
| 55 | + post = sub.submit('[Discussion] Week '+CURRENT_WEEK, text="", send_replies=False) |
| 56 | + post.sticky(bottom=True) |
| 57 | + post.distinguish() |
| 58 | + post.set_suggested_sort('old') |
| 59 | + |
| 60 | + # Grab ALL events from TBA and create a comment for this week's events |
| 61 | + # Once a week endpoint is released, this will only grab that instead |
| 62 | + # https://github.com/the-blue-alliance/the-blue-alliance/pull/1107 |
| 63 | + events = get_events() |
| 64 | + pprint('creating comments for each event...') |
| 65 | + for event in events: |
| 66 | + if event['start_date'] >= week_start and event['end_date'] <= week_end: |
| 67 | + pprint('# '+event['key']) |
| 68 | + comment = post.add_comment('##' + event['name'] + |
| 69 | + '\n\n'+ event['location'] + ' || ' + |
| 70 | + datetime.datetime.strptime(event['start_date'], '%Y-%m-%d').strftime('%B %d') + |
| 71 | + ' - ' + datetime.datetime.strptime(event['end_date'], '%Y-%m-%d').strftime('%B %d') + |
| 72 | + '\n\n---\n\nMore Information: https://www.thebluealliance.com/event/'+event['key']) |
| 73 | + comment.distinguish() |
| 74 | + # Add the comment to the table-of-contents var for the post edit |
| 75 | + toc += '- [' + event['name'] + '](' + comment.permalink + ')\n\n' |
| 76 | + |
| 77 | + pprint('adding a comment ToC to the OP...') |
| 78 | + post.edit('Here are some handy links to each event\'s top-level comment:\n\n' + toc + |
| 79 | + '---\n*This post was automatically generated by a script. If you have any suggestions or features, please file an issue at the [GitHub repo](https://github.com/synth3tk/frcbot/issues)!*') |
| 80 | + |
| 81 | + pprint('done!') |
| 82 | + |
| 83 | + except Exception as err: |
| 84 | + print traceback.print_exc() |
| 85 | + sys.exit(0) |
| 86 | + |
| 87 | +if __name__ == '__main__': |
| 88 | + main() |
0 commit comments