-
Notifications
You must be signed in to change notification settings - Fork 1
/
lambda_function.py
98 lines (78 loc) · 3.1 KB
/
lambda_function.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
import json
import os
import boto3
from botocore.exceptions import ClientError
table_name = os.environ.get('TABLE_NAME')
dynamo = boto3.resource('dynamodb')
anime_table = dynamo.Table(table_name)
def lambda_handler(event, context):
REGION =os.environ.get('REGION')
CAMPAIN_ARN = os.environ.get('CAMPAIGN_ARN')
FILTERS_STR = os.environ.get('FILTERS')
FILTERS = []
if FILTERS_STR is not None:
FILTERS = json.loads(FILTERS_STR)
print (event)
# ** --------------------------------
# ** OBTENER RECOMENDACION
# ** --------------------------------
pathParameters = event['pathParameters']
if not 'userId' in pathParameters:
return build_response(500, 'Falta userId')
userId = pathParameters['userId']
filter_arn = None
qs_params = event['queryStringParameters']
if (qs_params is not None) and ('filter' in qs_params):
filter_arn = get_filter_arn(FILTERS, qs_params['filter'])
if filter_arn is None:
return build_response(400, 'Invalid Filter Name')
numResults = 25
if (qs_params is not None) and ('numResults' in qs_params):
numResults = int(qs_params['numResults'])
personalize_runtime = boto3.client('personalize-runtime', region_name=REGION)
try:
args = dict(
campaignArn = CAMPAIN_ARN,
numResults = numResults,
userId = str(userId)
)
if filter_arn:
args = dict(campaignArn = CAMPAIN_ARN, userId = str(userId), numResults = numResults, filterArn = filter_arn)
get_recommendations_response = personalize_runtime.get_recommendations(
**args)
print (get_recommendations_response)
get_recommendations_response["itemList"] = add_anime_data(anime_table,get_recommendations_response["itemList"])
return build_response(200, get_recommendations_response)
except ClientError as error:
print (error.response['Error']['Code'], error.response['Error'])
return build_response(error.response['Error']['Code'], error.response['Error'])
except BaseException as error:
print("Unknown error while executing: " + str(error))
build_response(500, error.response['Error'])
def get_filter_arn(filter_list, filter_name):
for f in filter_list:
if f['name'] == filter_name:
return f['filterArn']
return None
def build_response(status_code, json_content):
return {
'statusCode': status_code,
"headers": {
"Access-Control-Allow-Origin":"*",
"Content-Type": "application/json",
"Access-Control-Allow-Methods" : "GET, OPTIONS, POST, DELETE",
},
'body': json.dumps({'data':json_content})
}
def add_anime_data(table,itemlist):
list_with_data=[]
for animeid in itemlist:
anime=get_anime_data(table,animeid["itemId"])
if anime:
anime['personalizescore']= animeid['score']
list_with_data.append(anime)
return list_with_data
def get_anime_data(table,anime_id):
print (anime_id)
item = table.get_item(Key={'MAL_ID': anime_id})
return item.get('Item')