forked from sogeti/HavingFunWithLambda_101
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buienradar-lambda.py
75 lines (65 loc) · 2.63 KB
/
buienradar-lambda.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
# Import Modules
from __future__ import print_function
from warnings import catch_warnings
import boto3
import json
import os
import urllib.request
from datetime import datetime
# set boto3 clients
sns_client = boto3.client('sns')
# Global Variable for ARN Topic
TopicArn = os.environ['TopicArn']
def lambda_handler(event, context):
buienradar_data_url = "https://data.buienradar.nl/2.0/feed/json"
try:
response = urllib.request.urlopen(buienradar_data_url)
except:
return('An error occurred while attempting to retrieve data from Buienradar.')
if response.getcode() == 200:
source = response.read()
data = json.loads(source)
else:
return('An error occurred while attempting to retrieve data from Buienradar.')
tomorrow = data['forecast']['fivedayforecast'][0]
day_short = tomorrow['day'].split("T")[0]
datetime_object = datetime.strptime(tomorrow['day'], '%Y-%m-%dT%H:%M:%S')
day_name = datetime_object.strftime('%A')
weatherdescript = tomorrow['weatherdescription']
mintemperature = tomorrow['mintemperature']
maxtemperature = tomorrow['maxtemperature']
rainChance = tomorrow['rainChance']
sunChance = tomorrow['sunChance']
windDirection = tomorrow['windDirection'].upper()
wind = tomorrow['wind']
subject = f"Weather report for - {day_name}"
msg = """Tomorrows Weather Report
------------------------------------------------------------------------------------
Tomorrow : {a}
------------------------------------------------------------------------------------
\tWeather description\t\t:{b}
\tmintemperature\t\t:{c}°C
\tmaxtemperature\t\t:{d}°C
\trainChance\t\t\t:{e}
\tsunChance\t\t\t:{f}
\twindDirection\t\t\t:{g}
------------------------------------------------------------------------------------
Regards,
Having Fun with Lambda - 101
-- data fetched from https://www.buienradar.nl --
""".format(a=" " + day_short,
b=" " + str(weatherdescript),
c=" " + str(mintemperature),
d=" " + str(maxtemperature),
e=" " + str(rainChance) + "%",
f=" " + str(sunChance) + "%",
g=" " + windDirection + " " + str(wind) + "Bft" ,)
topic = sns_client.publish(
TopicArn=TopicArn,
Message=msg,
Subject=subject
)
if topic['ResponseMetadata']['HTTPStatusCode'] == 200:
return(f"{weatherdescript} - TempMin: {mintemperature} C TempMax: {maxtemperature} C")
else:
return('An error occurred while attempting to send mail.')