-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
464 lines (392 loc) · 13.8 KB
/
main.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import boto3
import re
import time
import requests
from twilio.rest import Client
from collections import defaultdict
from decimal import Decimal
from itertools import groupby
from operator import itemgetter
from helpers.authorizer import authorize
from helpers.config import Config
from aws_lambda_powertools import Logger
from aws_lambda_powertools.utilities.typing import LambdaContext
from aws_lambda_powertools.event_handler import APIGatewayHttpResolver
from aws_lambda_powertools.event_handler.api_gateway import Response
from bs4 import BeautifulSoup
from urllib.parse import parse_qsl
config = Config()
logger = Logger()
app = APIGatewayHttpResolver()
client = Client(config.twilio_account_sid, config.twilio_auth_token)
ddb = boto3.resource("dynamodb")
scores_table = ddb.Table(config.scores_table)
wordles_table = ddb.Table(config.wordles_table)
users_table = ddb.Table(config.users_table)
ip_utc_offset_table = ddb.Table(config.ip_utc_offset_table)
responses = {
1: "Hole in one!",
2: "Albatross!",
3: "Birdie!",
4: "Par",
5: "Bogey",
6: "Double bogey",
}
default_opt_in_out_msgs = [
"stop",
"stopall",
"unsubscribe",
"cancel",
"end",
"quit",
"start",
"yes",
"unstop",
]
err_msg_to_user = "Something went wrong! Please try again."
reminder_msg = "Don't forget to do today's Wordle!\n\nhttps://nytimes.com/games/wordle\n\nText ENOUGH to opt out of these reminders."
unsubscribed_msg = (
"You will no longer receive daily Wordle reminders.\n\nText REMIND to opt back in."
)
subscribed_msg = (
"Successfully subscribed to Wordle reminders!\n\nText ENOUGH to opt out."
)
def sms_response(msg: str) -> Response:
return Response(
status_code=200,
body=msg,
content_type="text/plain",
)
def get_todays_wordle_number(ip: str = None, utc_offset: int = None) -> int:
offset = utc_offset if utc_offset else int(get_user_utc_offset(ip=ip))
hours_since_epoch = time.time() / 60 / 60 + offset
return (
int(hours_since_epoch / 24)
- config.reference["days_since_epoch"]
+ config.reference["puzzle_number"]
)
def get_user_utc_offset(ip: str) -> Decimal:
try:
return ip_utc_offset_table.query(
ProjectionExpression="UtcOffest",
KeyConditionExpression="#IpAddress = :val",
ExpressionAttributeNames={"#IpAddress": "IpAddress"},
ExpressionAttributeValues={":val": ip},
Limit=1,
)["Items"][0]["UtcOffest"]
except (KeyError, IndexError):
logger.debug(f"Cache miss: {ip}")
response = requests.get(f"{config.tz_api}{ip}")
response.raise_for_status()
raw_offset = int(response.json()["utc_offset"].replace(":", ""))
sign = -1 if raw_offset < 0 else 1
raw_offset = abs(raw_offset)
utc_offset = Decimal((int(raw_offset / 100) + (raw_offset % 100) / 60.0) * sign)
ip_utc_offset_table.put_item(
Item={
"IpAddress": ip,
"UtcOffest": utc_offset,
},
)
return utc_offset
def get_todays_wordle_answer() -> None:
content = BeautifulSoup(
requests.get(config.wordle_archive_api).text,
features="html.parser",
).select("section.content")[0]
elements = content.find_all("table")[0].find("tbody").find("tr").find_all("td")
a_id = elements[1].get_text().strip()
try:
answer = elements[2].find("span").get_text().strip()
except:
answer = elements[2].get_text().strip()
try:
definitions = []
response = requests.get(f"{config.dictionary_api}{answer}")
response.raise_for_status()
for i in response.json():
for m in i["meanings"]:
definitions.append(
{
"part_of_speech": m["partOfSpeech"],
"definitions": [d["definition"] for d in m["definitions"]],
}
)
except Exception as e:
logger.warn(f"Definition(s) not found for {answer}")
definitions = [{"part_of_speech": "", "definitions": []}]
wordles_table.put_item(
Item={
"Id": int(a_id),
"Answer": answer,
"Definitions": definitions,
},
)
def send_reminders() -> None:
for u in users_table.scan(
ProjectionExpression="PhoneNumber",
FilterExpression="attribute_not_exists(Unsubscribed)",
)["Items"]:
if (
len(
scores_table.query(
KeyConditionExpression="#PuzzleNumber = :puzzle AND #PhoneNumber = :who",
ExpressionAttributeNames={
"#PuzzleNumber": "PuzzleNumber",
"#PhoneNumber": "PhoneNumber",
},
ExpressionAttributeValues={
":puzzle": get_todays_wordle_number(utc_offset=-5),
":who": u["PhoneNumber"],
},
Limit=1,
)["Items"]
)
== 0
):
logger.debug(f"Sending reminder to {u['PhoneNumber']}")
message = client.messages.create(
from_=config.twilio_messaging_service_sid,
body=reminder_msg,
to=f"+1{u['PhoneNumber']}",
)
logger.info(f"Sent {message.sid}")
def unsubscribe_user(user: int) -> Response:
try:
users_table.update_item(
Key={"PhoneNumber": user},
UpdateExpression="SET #Unsubscribed = :val",
ExpressionAttributeNames={"#Unsubscribed": "Unsubscribed"},
ExpressionAttributeValues={":val": True},
)
return sms_response(msg=unsubscribed_msg)
except Exception as e:
logger.error(f"Error unsubscribing user {user}: {e}")
return sms_response(msg=err_msg_to_user)
def subscribe_user(user: int) -> Response:
try:
users_table.update_item(
Key={"PhoneNumber": user},
UpdateExpression="REMOVE Unsubscribed",
)
return sms_response(msg=subscribed_msg)
except Exception as e:
logger.error(f"Error subscribing user {user}: {e}")
return sms_response(msg=err_msg_to_user)
@app.post("/post")
@authorize(app)
def post_score() -> Response:
try:
decoded_body = dict(parse_qsl(app.current_event.decoded_body))
phone_number = int(decoded_body["From"][2:])
first_line = list(filter(None, decoded_body["Body"].split("\n")))[0]
chunks = first_line.split(" ")
# Handle some other message types
if chunks[0].lower() in default_opt_in_out_msgs:
return None
if chunks[0].lower() == "enough":
return unsubscribe_user(user=phone_number)
if chunks[0].lower() == "remind":
return subscribe_user(user=phone_number)
puzzle_number = int(chunks[1].replace(",", ""))
try:
guesses = int(chunks[2].split("/")[0])
victory = True
except ValueError:
guesses = 6
victory = False
assert guesses >= 1 and guesses <= 6
except Exception as e:
logger.error(e)
return sms_response(
msg="Sorry, didn't understand that. Make sure to paste your score directly from the Wordle site/app!"
)
try:
scores_table.put_item(
Item={
"PhoneNumber": phone_number,
"PuzzleNumber": puzzle_number,
"Guesses": guesses,
"Victory": victory,
"CreateTime": int(time.time()),
},
)
try:
users_table.query(
ProjectionExpression="CallerName",
KeyConditionExpression="#PhoneNumber = :val",
ExpressionAttributeNames={"#PhoneNumber": "PhoneNumber"},
ExpressionAttributeValues={":val": phone_number},
Limit=1,
)["Items"][0]["CallerName"]
except (KeyError, IndexError):
logger.debug(f"Cache miss: {phone_number}")
users_table.put_item(
Item={
"PhoneNumber": phone_number,
"CallerName": dict(
client.lookups.v2.phone_numbers(phone_number)
.fetch(fields="caller_name")
.caller_name
)["caller_name"],
},
)
return sms_response(
msg=(responses[guesses] if victory else "Better luck tomorrow!")
+ "\n\nwordle.kellenanker.com"
)
except Exception as e:
logger.error(f"Error putting item: {e}")
return sms_response(msg=err_msg_to_user)
@app.get("/leaderboard")
def leaderboard() -> list:
return sorted(
[
user(user=u)
for u in set(
[
i["PhoneNumber"]
for i in scores_table.scan(ProjectionExpression="PhoneNumber")[
"Items"
]
]
)
],
key=lambda x: x["Average"],
)
@app.get("/user/<user>")
def user(user: str) -> dict:
items = scores_table.scan(
FilterExpression="#PhoneNumber = :who",
ExpressionAttributeValues={":who": int(user)},
ExpressionAttributeNames={"#PhoneNumber": "PhoneNumber"},
ProjectionExpression="Guesses,Victory,PuzzleNumber",
)["Items"]
wins = [int(i["PuzzleNumber"]) for i in items if i["Victory"]]
# https://stackoverflow.com/questions/2361945/detecting-consecutive-integers-in-a-list
streaks = []
for _, g in groupby(
enumerate(wins),
lambda ix: ix[0] - ix[1],
):
streaks.append(list(map(itemgetter(1), g)))
# Shouldn't be needed once everyone texts in again
try:
caller_name = users_table.query(
ProjectionExpression="CallerName",
KeyConditionExpression="#PhoneNumber = :val",
ExpressionAttributeNames={"#PhoneNumber": "PhoneNumber"},
ExpressionAttributeValues={":val": int(user)},
Limit=1,
)["Items"][0]["CallerName"]
except:
caller_name = None
return {
"CallerName": caller_name,
"PhoneNumber": int(user),
"Puzzles": sorted(items, key=lambda x: x["PuzzleNumber"], reverse=True),
"Wins": sorted(wins, key=int, reverse=True),
"WinPercentage": (
0 if len(items) < 1 else round((len(wins) / len(items)) * 100, 2)
),
"Average": (
0
if len(items) < 1
else round(sum([int(i["Guesses"]) for i in items]) / len(items), 2)
),
"LongestStreak": (
0 if len(streaks) < 1 else sorted([len(s) for s in streaks])[-1]
),
"CurrentStreak": (
0
if len(items) < 1
or len(wins) < 1
or wins[-1]
< get_todays_wordle_number(app.current_event.request_context.http.source_ip)
- 1
else (len(streaks[-1]) if wins[-1] == items[-1]["PuzzleNumber"] else 0)
),
}
@app.get("/today")
def today() -> dict:
return {
"PuzzleNumber": get_todays_wordle_number(
ip=app.current_event.request_context.http.source_ip
)
}
@app.get("/wordles")
def wordles() -> list:
todays_wordle = get_todays_wordle_number(
ip=app.current_event.request_context.http.source_ip
)
return sorted(
[i for i in wordles_table.scan()["Items"] if i["Id"] < todays_wordle],
key=lambda x: x["Id"],
reverse=True,
)
@app.get("/wordle/<wordle>")
def wordle(wordle: str) -> dict:
items = scores_table.scan(
FilterExpression="#PuzzleNumber = :puzzle",
ExpressionAttributeValues={
":puzzle": int(wordle),
},
ExpressionAttributeNames={"#PuzzleNumber": "PuzzleNumber"},
ProjectionExpression="PhoneNumber,Guesses,Victory",
)["Items"]
answer = None
definitions = []
try:
if int(wordle) < get_todays_wordle_number(
ip=app.current_event.request_context.http.source_ip
):
item = wordles_table.query(
KeyConditionExpression="#Id = :val",
ExpressionAttributeNames={"#Id": "Id"},
ExpressionAttributeValues={":val": int(wordle)},
Limit=1,
)["Items"][0]
answer = item["Answer"]
# Until all wordle items have been updated
try:
definitions = item["Definitions"]
except:
pass
except (KeyError, IndexError):
pass
participants = []
for i in items:
try:
caller_name = users_table.query(
ProjectionExpression="CallerName",
KeyConditionExpression="#PhoneNumber = :val",
ExpressionAttributeNames={"#PhoneNumber": "PhoneNumber"},
ExpressionAttributeValues={":val": i["PhoneNumber"]},
Limit=1,
)["Items"][0]["CallerName"]
except:
caller_name = None
participants.append(
{
"CallerName": caller_name,
"PhoneNumber": i["PhoneNumber"],
"Guesses": int(i["Guesses"]),
"Victory": i["Victory"],
}
)
return {
"PuzzleNumber": int(wordle),
"Answer": answer,
"Definitions": definitions,
"Users": sorted(participants, key=lambda x: x["Guesses"]),
}
@app.get("/health")
def health() -> str:
return {"status": "alive"}
def api_handler(event: dict, context: LambdaContext) -> str:
logger.debug(event)
if event.get("action", None) == "update":
return get_todays_wordle_answer()
if event.get("action", None) == "remind":
return send_reminders()
return app.resolve(event, context)