-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamoDB_Script.py
55 lines (42 loc) · 1.54 KB
/
DynamoDB_Script.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
import json
# http://api.weatherapi.com/v1/current.json?key=&q=India&aqi=no
from datetime import datetime
import requests
import boto3
from decimal import Decimal
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table("weather")
def get_weather_data(city):
api_url = "http://api.weatherapi.com/v1/current.json"
params = {
"q": city,
"key": "<your_key>"
}
response = requests.get(api_url, params=params)
data = response.json()
return data
def lambda_handler(event, context):
cities = ["Bangalore","Delhi","Mumbai","Chennai","Kashmir","Dehradun","Kochi","Kerela","Hyderabad","Sikkim"]
for city in cities:
data = get_weather_data(city)
temp = data['current']['temp_c']
wind_speed = data['current']['wind_mph']
wind_dir = data['current']['wind_dir']
pressure_mb = data['current']['pressure_mb']
humidity = data['current']['humidity']
print(city,temp,wind_speed,wind_dir,pressure_mb,humidity)
current_timestamp = datetime.utcnow().isoformat()
item = {
'city': city,
'time': str(current_timestamp),
'temp': temp,
'wind_speed': wind_speed,
'wind_dir': wind_dir,
'pressure_mb': pressure_mb,
'humidity': humidity
}
item = json.loads(json.dumps(item), parse_float=Decimal)
# Insert data into DynamoDB
table.put_item(
Item=item
)