-
Notifications
You must be signed in to change notification settings - Fork 0
/
apis.py
194 lines (181 loc) · 6.06 KB
/
apis.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import json
import boto3
import datetime
import base64
def lambda_handler(event, context):
print(type(event))
print(event)
print("Event json %s" % json.dumps(event))
print("Context %s" % context)
client = boto3.resource('dynamodb')
s3 = boto3.resource('s3')
table = client.Table('adm-data')
eventDateTime = (datetime.datetime.now()).strftime("%Y-%m-%d %H:%M:%S")
event = json.loads(event['body'])
# return {
# 'statusCode': 404,
# 'body': event['method'],
# "headers": {
# 'Content-Type': 'application/json',
# }}
if (event['method'] == "REGISTER_USER"):
response = table.put_item(
Item={
'pk': event['userpk'],
'sk': event['userpk'],
'interests': [],
'createdAt': eventDateTime,
})
return {
'statusCode': response['ResponseMetadata']['HTTPStatusCode'],
'body': "User Added",
"headers": {
'Content-Type': 'application/json',
}
}
elif (event['method'] == "LOGIN_USER"):
response = table.get_item(
Key={
'pk': event['userpk'],
'sk': event['userpk']
})
item = response.get('Item')
if item:
return {
'statusCode': 200,
'body': item,
"headers": {
'Content-Type': 'application/json',
}
}
else:
return {
'statusCode': 404,
'body': 'User not found',
"headers": {
'Content-Type': 'application/json',
}
}
elif (event['method'] == "LIST_POSTS"):
response = table.scan(
FilterExpression='#pk = :pk_val',
ExpressionAttributeNames={
'#pk': 'pk'
},
ExpressionAttributeValues={
':pk_val': "post"
})
return {
'statusCode': response['ResponseMetadata']['HTTPStatusCode'],
'body': response['Items'],
"headers": {
'Content-Type': 'text/html',
}
}
elif (event['method'] == "GET_USER"):
response = table.get_item(
Key={
'pk': event["userpk"],
'sk': event["userpk"]
})
item = response['Item']
return {
'statusCode': response['ResponseMetadata']['HTTPStatusCode'],
'body': item,
"headers": {
'Content-Type': 'text/html',
}
}
elif (event['method'] == "ADD_INTERESTS"):
response = table.get_item(
Key={
'pk': event["userpk"],
'sk': event["userpk"]
})
item = response['Item']
return {
'statusCode': response['ResponseMetadata']['HTTPStatusCode'],
'body': item,
"headers": {
'Content-Type': 'text/html',
}
}
elif (event['method'] == "UPLOAD_POST"):
image_data = event['image']
image_binary_data = base64.b64decode(image_data)
# Specify the S3 bucket name and image file name
bucket_name = 'admmedia'
image_file_name = "user_uploads/" + \
event['image_filename'] # You can specify any file name
# Upload the image to S3
s3_client = boto3.client('s3')
s3_client.put_object(
Body=image_binary_data,
Bucket=bucket_name,
Key=image_file_name
)
item = {
'pk': "post",
'sk': event["postsk"],
'image_link': "https://admmedia.s3.ap-south-1.amazonaws.com/user_uploads/"+event['image_filename'],
'recommends': [],
'caption': "",
'ad_links': [],
'username': event['username']
}
response = table.put_item(
Item=item)
return {
'statusCode': response['ResponseMetadata']['HTTPStatusCode'],
'body': item,
"headers": {
'Content-Type': 'text/html',
}
}
elif (event['method'] == "POST_RECOGNITION"):
key = {
'pk': "post",
'sk': event["postsk"]
}
# specify the attributes to update
update_expression = 'SET #attr1 = :rec, #attr2 = :cap, #attr3 = :links '
expression_attribute_names = {
'#attr1': 'recommends', '#attr2': 'caption', '#attr3': 'ad_links'}
expression_attribute_values = {':rec': {'S': event["recommends"]}, ':cap': {
'N': event["caption"]}, ':links': {'N': event["ad_links"]}}
# update the item in the table
response = table.update_item(
Key=key,
UpdateExpression=update_expression,
ExpressionAttributeNames=expression_attribute_names,
ExpressionAttributeValues=expression_attribute_values,
)
return {
'statusCode': response['ResponseMetadata']['HTTPStatusCode'],
'body': "post_updated",
"headers": {
'Content-Type': 'text/html',
}
}
elif (event['method'] == "LIKE_POST"):
response = table.get_item(
Key={
'pk': event["userpk"],
'sk': event["postsk"],
})
item = response['Item']
return {
'statusCode': response['ResponseMetadata']['HTTPStatusCode'],
'body': item,
"headers": {
'Content-Type': 'text/html',
}
}
else:
return {
"statusCode": 404,
"body": "Method not found",
"headers": {
'Content-Type': 'text/html',
}
}