This repository was archived by the owner on Sep 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwinter_db.py
More file actions
313 lines (291 loc) · 11.4 KB
/
winter_db.py
File metadata and controls
313 lines (291 loc) · 11.4 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import os
import urllib.parse
import urllib.request
import psycopg2
from psycopg2 import sql
from slack_api import *
from flask import Flask, request, jsonify, make_response
app = Flask(__name__)
#CREATE TABLE winter_data(name text, num_posts SMALLINT, num_workouts SMALLINT, num_throws SMALLINT, num_cardio SMALLINT, num_gym SMALLINT, workout_score numeric(4, 1), last_post DATE, slack_id CHAR(9), last_time BIGINT)
def add_num_posts(mention_id, event_time, name, channel_id):
try:
urllib.parse.uses_netloc.append("postgres")
url = urllib.parse.urlparse(os.environ["DATABASE_URL"])
conn = psycopg2.connect(
database=url.path[1:],
user=url.username,
password=url.password,
host=url.hostname,
port=url.port
)
cursor = conn.cursor()
cursor.execute(sql.SQL(
"UPDATE winter_data SET num_posts=num_posts+1 WHERE slack_id = %s"),
[mention_id[0]])
if cursor.rowcount == 0 and channel_id == "GUF7AMF1Q":
cursor.execute(sql.SQL("INSERT INTO winter_data VALUES (%s, 0, 0, 0, 0, 0, 0, now(), %s, %s)"),
[name, mention_id[0], event_time])
send_debug_message("%s is new to Wreck" % name)
conn.commit()
cursor.close()
conn.close()
return True
except (Exception, psycopg2.DatabaseError) as error:
send_debug_message(error)
return True
def collect_stats(datafield, rev):
try:
urllib.parse.uses_netloc.append("postgres")
url = urllib.parse.urlparse(os.environ["DATABASE_URL"])
conn = psycopg2.connect(
database=url.path[1:],
user=url.username,
password=url.password,
host=url.hostname,
port=url.port
)
cursor = conn.cursor()
# get all of the people who's workout scores are greater than -1 (any non players have a workout score of -1)
cursor.execute(sql.SQL(
"SELECT * FROM winter_data WHERE workout_score > -1.0"), )
leaderboard = cursor.fetchall()
leaderboard.sort(key=lambda s: s[6], reverse=rev) # sort the leaderboard by score descending
string1 = "Leaderboard:\n"
for x in range(0, len(leaderboard)):
string1 += '%d) %s with %.1f point(s); %.1d throw(s); %.1d sprint(s); %.1d lift(s). \n' % (x + 1, leaderboard[x][0],
leaderboard[x][6], leaderboard[x][3], leaderboard[x][4], leaderboard[x][5])
cursor.close()
conn.close()
return string1
except (Exception, psycopg2.DatabaseError) as error:
send_debug_message(error)
def get_group_info():
url = "https://slack.com/api/users.list?token=" + os.getenv('BOT_OAUTH_ACCESS_TOKEN')
json = requests.get(url).json()
return json
def get_emojis():
url = 'https://slack.com/api/emoji.list?token=' + os.getenv('OAUTH_ACCESS_TOKEN')
json = requests.get(url).json()
return json
def add_to_db(channel_id, names, addition, gym_num, throw_num, cardio_num, num_workouts, ids): # add "addition" to each of the "names" in the db
cursor = None
conn = None
num_committed = 0
try:
urllib.parse.uses_netloc.append("postgres")
url = urllib.parse.urlparse(os.environ["DATABASE_URL"])
conn = psycopg2.connect(
database=url.path[1:],
user=url.username,
password=url.password,
host=url.hostname,
port=url.port
)
cursor = conn.cursor()
for x in range(0, len(names)):
print("starting", names[x])
cursor.execute(sql.SQL(
"SELECT workout_score FROM winter_data WHERE slack_id = %s"), [str(ids[x])])
score = cursor.fetchall()[0][0]
score = int(score)
if score != -1 and channel_id == "GUF7AMF1Q":
cursor.execute(sql.SQL("""
UPDATE winter_data SET num_workouts=num_workouts+%s,
num_throws=num_throws+%s, num_cardio=num_cardio+%s, num_gym=num_gym+%s,
workout_score=workout_score+%s, last_post=now() WHERE slack_id = %s
"""),
[str(num_workouts), str(throw_num), str(cardio_num), str(gym_num), str(addition), ids[x]])
conn.commit()
send_debug_message("committed %s with %s points" % (names[x], str(addition)))
print("committed %s" % names[x])
num_committed += 1
else:
send_debug_message("invalid workout poster found " + names[x])
except (Exception, psycopg2.DatabaseError) as error:
send_debug_message(str(error))
finally:
if cursor is not None:
cursor.close()
conn.close()
return num_committed
def get_req(mention_id):
cursor = None
conn = None
req_string = ""
try:
urllib.parse.uses_netloc.append("postgres")
urllib.parse.uses_netloc.append("postgres")
url = urllib.parse.urlparse(os.environ["DATABASE_URL"])
conn = psycopg2.connect(
database=url.path[1:],
user=url.username,
password=url.password,
host=url.hostname,
port=url.port
)
cursor = conn.cursor()
cursor.execute(sql.SQL(
"SELECT * FROM winter_data WHERE slack_id = %s"), [mention_id[0]])
entry = cursor.fetchall()
req_string += '%s requirements fulfilled: %.1d throws; %.1d cardio; %.1d lifts.' % (entry[x][0], entry[x][3], entry[x][4], entry[x][5])
cursor.close()
conn.close()
return req_string
except (Exception, psycopg2.DatabaseError) as error:
send_debug_message(error)
def subtract_from_db(names, subtraction, ids): # subtract "subtraction" from each of the "names" in the db
cursor = None
conn = None
num_committed = 0
try:
urllib.parse.uses_netloc.append("postgres")
url = urllib.parse.urlparse(os.environ["DATABASE_URL"])
conn = psycopg2.connect(
database=url.path[1:],
user=url.username,
password=url.password,
host=url.hostname,
port=url.port
)
cursor = conn.cursor()
for x in range(0, len(names)):
cursor.execute(sql.SQL(
"UPDATE winter_data SET workout_score = workout_score - %s WHERE slack_id = %s"),
[subtraction, ids[x]])
conn.commit()
send_debug_message("subtracted %s" % names[x])
num_committed += 1
except (Exception, psycopg2.DatabaseError) as error:
send_debug_message(str(error))
finally:
if cursor is not None:
cursor.close()
conn.close()
return num_committed
def reset_scores(): # reset the scores of everyone
cursor = None
conn = None
try:
urllib.parse.uses_netloc.append("postgres")
url = urllib.parse.urlparse(os.environ["DATABASE_URL"])
conn = psycopg2.connect(
database=url.path[1:],
user=url.username,
password=url.password,
host=url.hostname,
port=url.port
)
cursor = conn.cursor()
cursor.execute(sql.SQL("""
UPDATE winter_data SET num_workouts = 0, num_throws = 0, num_cardio = 0,
num_gym = 0, workout_score = 0, last_post = now() WHERE workout_score != -1
"""))
# cursor.execute(sql.SQL(
# "DELETE FROM tribe_workouts"
# ))
conn.commit()
except (Exception, psycopg2.DatabaseError) as error:
send_debug_message(str(error))
finally:
if cursor is not None:
cursor.close()
conn.close()
def reset_talkative(): # reset the num_posts of everyone
cursor = None
conn = None
try:
urllib.parse.uses_netloc.append("postgres")
url = urllib.parse.urlparse(os.environ["DATABASE_URL"])
conn = psycopg2.connect(
database=url.path[1:],
user=url.username,
password=url.password,
host=url.hostname,
port=url.port
)
cursor = conn.cursor()
cursor.execute(sql.SQL(
"UPDATE winter_data SET num_posts = 0 WHERE workout_score != -1"))
conn.commit()
except (Exception, psycopg2.DatabaseError) as error:
send_debug_message(str(error))
finally:
if cursor is not None:
cursor.close()
conn.close()
def add_workout(name, slack_id, workout_type):
cursor = None
conn = None
try:
urllib.parse.uses_netloc.append("postgres")
url = urllib.parse.urlparse(os.environ["DATABASE_URL"])
conn = psycopg2.connect(
database=url.path[1:],
user=url.username,
password=url.password,
host=url.hostname,
port=url.port
)
# cursor = conn.cursor()
# cursor.execute(sql.SQL("INSERT INTO tribe_workouts VALUES (%s, %s, %s, now())"), [str(name), str(slack_id), str(workout_type)])
# conn.commit()
# send_debug_message("Committed " + name + " to the workout list")
except (Exception, psycopg2.DatabaseError) as error:
send_debug_message(str(error))
finally:
if cursor is not None:
cursor.close()
conn.close()
def get_workouts_after_date(date, type, slack_id):
cursor = None
conn = None
workouts = []
try:
urllib.parse.uses_netloc.append("postgres")
url = urllib.parse.urlparse(os.environ["DATABASE_URL"])
conn = psycopg2.connect(
database=url.path[1:],
user=url.username,
password=url.password,
host=url.hostname,
port=url.port
)
# cursor = conn.cursor()
# cursor.execute(sql.SQL("SELECT * from tribe_workouts WHERE slack_id=%s and workout_date BETWEEN %s and now() and workout_type=%s"),
# [slack_id, date, "!" + type])
# workouts = cursor.fetchall()
# conn.commit()
except (Exception, psycopg2.DatabaseError) as error:
send_debug_message(str(error))
finally:
if cursor is not None:
cursor.close()
conn.close()
return workouts
def get_group_workouts_after_date(date, type):
cursor = None
conn = None
workouts = []
print(date, type)
try:
urllib.parse.uses_netloc.append("postgres")
url = urllib.parse.urlparse(os.environ["DATABASE_URL"])
conn = psycopg2.connect(
database=url.path[1:],
user=url.username,
password=url.password,
host=url.hostname,
port=url.port
)
# cursor = conn.cursor()
# cursor.execute(sql.SQL("SELECT * from tribe_workouts WHERE workout_date BETWEEN %s and now() and workout_type=%s"),
# [date, "!" + type])
# workouts = cursor.fetchall()
# conn.commit()
except (Exception, psycopg2.DatabaseError) as error:
send_debug_message(str(error))
finally:
if cursor is not None:
cursor.close()
conn.close()
return workouts