-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda.py
More file actions
56 lines (49 loc) · 2.12 KB
/
lambda.py
File metadata and controls
56 lines (49 loc) · 2.12 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
import json
from urllib import request
def lambda_handler(event, context):
# Extract the 'location' parameter from the query string
location = event['queryStringParameters'].get('location', '')
# Make an HTTP GET request to retrieve weather data
with request.urlopen(
"https://qnag8pg924.execute-api.us-east-2.amazonaws.com/v1/IdealConditions?location=" + location) as weather_response:
forecast_content = weather_response.read().decode('utf-8')
forecast_json = json.loads(forecast_content)
# Extract necessary information from the received JSON response
current_weather = forecast_json["forecast"]["0"]["weather"]
city = forecast_json["information"]["city"]
state = forecast_json["information"]["state"]
date = forecast_json["forecast"]["0"]["date"]
time = forecast_json["forecast"]["0"]["time"]
activities = []
if "Thunderstorms" in current_weather:
activities = ["Stay indoors", "Watch a movie", "Read a book"]
elif "Rainy" in current_weather or "Showers" in current_weather:
activities = ["Watching movies", "Cooking", "Reading"]
elif "Snow" in current_weather:
activities = ["Skiing", "Building a snowman", "Drinking hot chocolate"]
elif "Cloudy" in current_weather:
activities = ["Walking in the park", "Photography", "Board games"]
elif "Sunny" in current_weather:
activities = ["Hiking", "Picnics", "Outdoor sports"]
elif "Clear" in current_weather:
activities = ["Stargazing", "Outdoor activities", "Barbecue"]
elif "Fog" in current_weather:
activities = ["Take a walk", "Enjoy a hot drink", "Relax"]
else:
activities = ["No activities found for this weather condition."]
# Construct the response object
response = {
'statusCode': 200,
'body': json.dumps({
"city": city,
"state": state,
"date": date,
"time": time,
"short_weather": current_weather,
"activities": activities,
}),
'headers': {
'Content-Type': 'application/json'
}
}
return response