forked from lzha97/spelling_bee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_function.py
101 lines (80 loc) · 3.38 KB
/
lambda_function.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
import json
import random
import boto3
import os
import logging
from botocore.exceptions import ClientError
def lambda_handler(event, context):
# TODO implement
print(event)
##for the daily generation of letters and their possible words
def is_pangram(string):
if len(set(list(string))) == 7: return True
return False
if 'detail-type' in event:
if event['detail-type'] == 'Scheduled Event':
worddict = dict()
with open('validwords.json', 'r') as dictfile:
worddict = json.load(dictfile)
pangram_set = set()
for k in worddict:
if is_pangram(k):
letterset = frozenset(k)
pangram_set.add(letterset)
res = []
while(len(res) < 15):
res = []
todays_letters = list(random.choice(list(pangram_set)))
score = 0
center_letter = todays_letters[3]
pangram = ''
for w in worddict:
flag = True
for c in w:
if c not in todays_letters:
flag = False
if center_letter not in w:
flag = False
if flag == True:
res.append(w)
if len(w) == 4: score +=1
elif is_pangram(w):
score +=17
pangram = w
elif len(w) > 4: score += len(w)
s3 = boto3.client('s3')
bucket_name = os.environ['BUCKET_NAME']
data = {'letters':todays_letters, 'pangram': pangram, 'possible_words':res, 'center_letter': center_letter, 'maxscore': score}
key = os.environ['FILE_KEY']
response = s3.put_object(Bucket=bucket_name,Key=key,Body=json.dumps(data),ACL='public-read')
return response
## for the return of letters and details from the api request
if 'queryStringParameters' in event:
bucket_name = os.environ['BUCKET_NAME']
key = os.environ['FILE_KEY']
s3 = boto3.resource('s3')
try:
s3.Bucket(bucket_name).download_file(key, '/tmp/todays_letters.json')
except ClientError as e:
if e.response['Error']['Code'] == "404":
return {
'statusCode': 404,
'body': 'Oops! Could not find today\'s word file.'
}
else:
return {
'statusCode': 500,
'body': "Uh oh, an error occurred"
}
with open('/tmp/todays_letters.json', 'r') as rdfile:
data = rdfile.read()
print(data)
data = json.loads(data)
return {
'headers':{
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
'statusCode': 200,
'body': json.dumps(data, indent=4)
}