This repository was archived by the owner on Apr 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweekly_discussion.py
88 lines (76 loc) · 3.56 KB
/
weekly_discussion.py
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
85
86
87
88
import ConfigParser
import datetime
import json
import OAuth2Util
from pprint import pprint
import praw
import requests
import sys, traceback
from TheBlueAlliance import *
# Read the config file
config = ConfigParser.ConfigParser()
config.read("settings.cfg")
SUBREDDIT = config.get("Reddit", "subreddit")
# Eventually this will be based off of TBA exposing the week
# As of now, it's only useful for the post title anyway
CURRENT_WEEK = '5'
HEADERS = {'X-TBA-App-Id': config.get("TBA", "appid")}
URL = 'http://www.thebluealliance.com/api/v2/'
# Datetime stuff for current week dates
today = datetime.date.today()
week_start_datetime = today - datetime.timedelta(days=today.weekday())
week_start = week_start_datetime.strftime('%Y-%m-%d')
week_end_datetime = week_start_datetime + datetime.timedelta(days=6)
week_end = week_end_datetime.strftime('%Y-%m-%d')
def get_events():
pprint('grabbing events...')
r = requests.get(URL + 'events/2016', headers=HEADERS)
events = r.json()
return events
def main():
try:
# Setup PRAW instance
pprint('initializing reddit connection...')
reddit = praw.Reddit(user_agent=config.get("Reddit", "useragent"))
o = OAuth2Util.OAuth2Util(reddit)
o.refresh(force=True)
toc = '';
# If the post exists, exit. Otherwise, continue.
pprint('checking if post exists...')
sub = reddit.get_subreddit(SUBREDDIT)
search = reddit.search('[Discussion] Week '+CURRENT_WEEK, subreddit=SUBREDDIT, sort='new', period='month')
search_list = list(search)
for post in search_list:
if post.title == '[Discussion] Week '+CURRENT_WEEK:
pprint('post already exists! exiting.')
return
pprint('posting the thread...')
post = sub.submit('[Discussion] Week '+CURRENT_WEEK, text="", send_replies=False)
post.sticky(bottom=True)
post.distinguish()
post.set_suggested_sort('old')
# Grab ALL events from TBA and create a comment for this week's events
# Once a week endpoint is released, this will only grab that instead
# https://github.com/the-blue-alliance/the-blue-alliance/pull/1107
events = get_events()
pprint('creating comments for each event...')
for event in events:
if event['start_date'] >= week_start and event['end_date'] <= week_end:
pprint('# '+event['key'])
comment = post.add_comment('##' + event['name'] +
'\n\n'+ event['location'] + ' || ' +
datetime.datetime.strptime(event['start_date'], '%Y-%m-%d').strftime('%B %d') +
' - ' + datetime.datetime.strptime(event['end_date'], '%Y-%m-%d').strftime('%B %d') +
'\n\n---\n\nMore Information: https://www.thebluealliance.com/event/'+event['key'])
comment.distinguish()
# Add the comment to the table-of-contents var for the post edit
toc += '- [' + event['name'] + '](' + comment.permalink + ')\n\n'
pprint('adding a comment ToC to the OP...')
post.edit('Here are some handy links to each event\'s top-level comment:\n\n' + toc +
'---\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)!*')
pprint('done!')
except Exception as err:
print traceback.print_exc()
sys.exit(0)
if __name__ == '__main__':
main()