-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function_get_details.py
More file actions
48 lines (42 loc) · 1.66 KB
/
Copy pathlambda_function_get_details.py
File metadata and controls
48 lines (42 loc) · 1.66 KB
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
import json
import requests
import os
def lambda_handler(event, context):
# Get the destination from the event
if "locationid" in event:
locationid = int(event['locationid'])
elif "pathParameters" in event:
if "locationid" in event['pathParameters']:
locationid = int(event['pathParameters']["locationid"])
if not locationid:
return {
"statusCode": 400,
"body": json.dumps({"Error": "Invalid Input!"})
}
# TripAdvisor API endpoint and key
tripadvisor_base_url = f"https://api.content.tripadvisor.com/api/v1/location/{locationid}/details?key=TRIP_ADVISOR_API_KEY&language=en"
api_key = os.environ.get("TRIP_ADVISOR_API_KEY")
try:
# Make a request to the TripAdvisor API
response = requests.get(tripadvisor_base_url)
response.raise_for_status()
data = response.json()
# Extract details
details = {
"description": data.get("description"),
"web_url": data.get("web_url"),
"rating": data.get("rating"),
"hours": data.get("hours"),
"cuisine": data.get("cuisine"),
"price_level": data.get("price_level"),
"email": data.get("email"),
"phone": data.get("phone")}
return {
"statusCode": 200,
"body": json.dumps({"locationid": locationid, "details": details})
}
except requests.RequestException as e:
return {
"statusCode": 500,
"body": json.dumps({"error": "Failed to fetch data from TripAdvisor", "details": str(e)})
}