Wrote parse_to_dynamodb.py as lambda_handler to get all relevant data and also to store the complete json in a DynamoDB-table
This commit is contained in:
21
.idea/workspace.xml
generated
21
.idea/workspace.xml
generated
@@ -4,7 +4,10 @@
|
||||
<option name="autoReloadType" value="SELECTIVE" />
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="9a6512a2-4a65-46dc-ac79-ea908fa84d1f" name="Changes" comment="" />
|
||||
<list default="true" id="9a6512a2-4a65-46dc-ac79-ea908fa84d1f" name="Changes" comment="">
|
||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/lambda_function/parse_to_dynamodb.py" beforeDir="false" afterPath="$PROJECT_DIR$/src/lambda_function/parse_to_dynamodb.py" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||
@@ -14,6 +17,7 @@
|
||||
<option name="RECENT_TEMPLATES">
|
||||
<list>
|
||||
<option value="Python Script" />
|
||||
<option value="AWS CloudFormation Template (YAML)" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
@@ -72,7 +76,7 @@
|
||||
<workItem from="1709141002289" duration="33000" />
|
||||
<workItem from="1709141085982" duration="184000" />
|
||||
<workItem from="1709141371555" duration="1607000" />
|
||||
<workItem from="1709143935553" duration="351000" />
|
||||
<workItem from="1709143935553" duration="3109000" />
|
||||
</task>
|
||||
<task id="LOCAL-00001" summary="Initial code to get the weather data from the API">
|
||||
<option name="closed" value="true" />
|
||||
@@ -106,7 +110,15 @@
|
||||
<option name="project" value="LOCAL" />
|
||||
<updated>1709144200442</updated>
|
||||
</task>
|
||||
<option name="localTasksCounter" value="5" />
|
||||
<task id="LOCAL-00005" summary="Add CloudFormation template to deploy the environment">
|
||||
<option name="closed" value="true" />
|
||||
<created>1709144394361</created>
|
||||
<option name="number" value="00005" />
|
||||
<option name="presentableId" value="LOCAL-00005" />
|
||||
<option name="project" value="LOCAL" />
|
||||
<updated>1709144394361</updated>
|
||||
</task>
|
||||
<option name="localTasksCounter" value="6" />
|
||||
<servers />
|
||||
</component>
|
||||
<component name="TypeScriptGeneratedFilesManager">
|
||||
@@ -127,6 +139,7 @@
|
||||
<MESSAGE value="Initial code to get the weather data from the API" />
|
||||
<MESSAGE value="Add requirements.txt to control python package version" />
|
||||
<MESSAGE value="Add lambda function to write json to dynamo db" />
|
||||
<option name="LAST_COMMIT_MESSAGE" value="Add lambda function to write json to dynamo db" />
|
||||
<MESSAGE value="Add CloudFormation template to deploy the environment" />
|
||||
<option name="LAST_COMMIT_MESSAGE" value="Add CloudFormation template to deploy the environment" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -0,0 +1,61 @@
|
||||
import json
|
||||
|
||||
import boto3
|
||||
from datetime import datetime
|
||||
|
||||
class ForecastToDynamoDB()
|
||||
def __init__(self):
|
||||
self.dynamodb = boto3.resource('dynamodb')
|
||||
self.table = self.dynamodb.Table('Forecast')
|
||||
|
||||
def parse_current(self,json_data):
|
||||
|
||||
city = json_data['location']['name']
|
||||
region = json_data['location']['region']
|
||||
localtime = datetime.strptime(json_data['location']['localtime'], '%Y/%m-%d %H:%M')
|
||||
localtime_lastupdate = datetime.strptime(json_data['current']['last_updated'], '%Y/%m-%d %H:%M')
|
||||
current_temp_c = json_data['current']['temp_c']
|
||||
current_condition = json_root['current']['condition']['text']
|
||||
current_windspeed = json_data['current']['wind_kph']
|
||||
current_winddegree = json_data['current']['wind_degree']
|
||||
current_winddir = json_data['current']['wind_dir']
|
||||
current_feelslike_c = json_data['current']['feelslike_c']
|
||||
|
||||
response = self.table.put_item(
|
||||
Item = {
|
||||
'city': city,
|
||||
'region': region,
|
||||
'localtime': localtime,
|
||||
'localtime_lastupdated': localtime_lastupdate,
|
||||
'current_tmep_c': current_temp_c,
|
||||
'current_condition': current_condition,
|
||||
'current_windspeed': current_windspeed,
|
||||
'current_winddegree': current_winddegree,
|
||||
'current_winddir': current_winddir,
|
||||
'current_feelslike_c': current_feelslike_c,
|
||||
'current_json': json_data['current']
|
||||
}
|
||||
)
|
||||
|
||||
return response
|
||||
|
||||
def lambda_handler(event, context):
|
||||
|
||||
try:
|
||||
handler = ForecastToDynamoDB()
|
||||
|
||||
json_data = json.loads(event['body'])
|
||||
|
||||
response = handler.parse_current(json_data)
|
||||
|
||||
return {
|
||||
'statusCode': 200,
|
||||
'body': json.dump({'message':'Item is successfully written in DynamoDB'}),
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
return {
|
||||
'statusCode': 500,
|
||||
'body': json.dump({'error':str(e)}),
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user