-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws_dynamodb.py
80 lines (71 loc) · 2.3 KB
/
aws_dynamodb.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
76
77
78
79
80
#The code reads the input from output.txt and inserts it in the dynamoDb
from __future__ import print_function # Python 2/3 compatibility
import boto3
import json
import decimal
import fcntl
import struct
import time
import ssl
import datetime
import calendar
userId = "amzn1.ask.account.AHFIXJPV6S446WKJYWB7QLF76DF2YLLPNX3EP2DQQW664LET5TN2XM3ZB24L5B2PTTV6MMRTYY4IPBYMGL6V7YJNLI7OAF6FUSCZWJOCL5423IOIYSFRJCMJ5GSVXFLJRDVKDUIOAQ73HHKF3K4GW4YCWA5XYXYSFDNIIQH6VQPNHLREM5WT5X4KLEDCVLA6IU4GPZMZBI4K5ZI"
# Helper class to convert a DynamoDB item to JSON.
class DecimalEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, decimal.Decimal):
if o % 1 > 0:
return float(o)
else:
return int(o)
return super(DecimalEncoder, self).default(o)
#Update the item on AWS
#For that we need guid, timestamp and message
def call_aws(message):
now = datetime.datetime.utcnow()
timestamp = int(round((now - datetime.datetime(2018, 1 , 1)).total_seconds()))
#days = (now - datetime.datetime(2016, 1 , 1)).days + id
dynamodb = boto3.resource('dynamodb', region_name='us-east-1', endpoint_url="https://dynamodb.us-east-1.amazonaws.com")
table = dynamodb.Table('SmartCap')
# your logic here...
try:
print("Trying to print " + message)
response = table.update_item(
Key={
'guid' : userId
#'command' : message,
},
UpdateExpression="set tstamp= :t, command =:r",
ExpressionAttributeValues={
':r': message,
':t': str(timestamp)
},
ReturnValues="UPDATED_NEW"
)
print("PutItem succeeded:")
return 1
except Exception, e:
print (e)
fol = open("awserror.txt", "wb")
fol.write(str(e))
fol.close()
return e
#print(json.dumps(response, indent=4, cls=DecimalEncoder))
def lambda_handler(event, context):
""" Route the incoming request based on type (LaunchRequest, IntentRequest,
etc.) The JSON body of the request is provided in the event parameter.
"""
#Main function
def main():
ssl._create_default_https_context = ssl._create_unverified_context
#Open the output.txt file in the read mode"
fo = open("output.txt", "rt")
try:
e = call_aws(str(fo.read()))
except Exception, e:
print (e)
time.sleep(.5)
# Close opend file
fo.close()
if __name__ == "__main__":
main()