-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsections-cache.py
More file actions
65 lines (55 loc) · 2.07 KB
/
sections-cache.py
File metadata and controls
65 lines (55 loc) · 2.07 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
import json
import boto3
import time
from boto3.dynamodb.conditions import Key, Attr
client = boto3.client('lambda')
dynamo = boto3.resource('dynamodb')
expdate = int( time.time() ) + 86400
def lambda_handler(event, context):
print(event['pathParameters'])
print(context)
cached_sections = dynamo.Table('sections')
response = cached_sections.query(
KeyConditionExpression=
Key('term').eq(event['pathParameters']['term']) &
Key('subject').eq(event['pathParameters']['subject']),
FilterExpression=Attr('catalogNumber').eq(event['pathParameters']['catalogNumber'])
)
if response['Items']:
return {
"statusCode": 200,
"body": json.dumps(response['Items'])['classes'],
"isBase64Encoded": "false",
"headers": {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": '*'
}
}
else:
inputParams = {
"term" : event['pathParameters']['term'],
"subject" : event['pathParameters']['subject'],
"number" : event['pathParameters']['catalogNumber']
}
response = client.invoke(
FunctionName = 'arn:aws:lambda:us-west-1:606504888305:function:cpp-schedule-scraper',
InvocationType = 'RequestResponse',
Payload = json.dumps(inputParams)
)
body = json.dumps(json.load(response['Payload']))
cached_data = {}
cached_data['term'] = event['pathParameters']['term']
cached_data['subject'] = event['pathParameters']['subject']
cached_data['number'] = event['pathParameters']['catalogNumber']
cached_data['classes'] = json.loads(body)
cached_data['expdate'] = expdate
cached_sections.put_item(Item=cached_data)
return {
"statusCode": 200,
"body": body,
"isBase64Encoded": "false",
"headers": {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": '*'
}
}