Spaces:
Sleeping
Sleeping
import requests | |
import os | |
from dotenv import load_dotenv | |
load_dotenv() | |
def get_trip_planner(days, destination): | |
try: | |
# Fetch the API token from environment variables | |
api_token = os.environ.get("ZYLA_API_TOKEN") | |
if not api_token: | |
raise ValueError("API token is required. Set ZYLA_API_TOKEN in environment variables.") | |
# Validate inputs | |
if not isinstance(days, int) or days <= 0: | |
raise ValueError("Days must be a positive integer.") | |
if not destination.strip(): | |
raise ValueError("Destination cannot be empty.") | |
# Construct API URL | |
api_url = f"https://zylalabs.com/api/2242/trip+planner+api/2103/get+planning?days={days}&destination={destination}" | |
# Make the API request | |
response = requests.get(api_url, headers={"Authorization": f"Bearer {api_token}"}) | |
response.raise_for_status() | |
# Parse the response | |
data = response.json() | |
if 'error' in data: | |
raise ValueError(f"API Error: {data['error']}") | |
trip_plan = data.get("plan", []) | |
if not trip_plan: | |
print("No trip plan found.") | |
return [] | |
formatted_plan = { | |
"_id": data.get("_id", "Unknown ID"), | |
"plan": [ | |
{ | |
"day": day_plan.get("day"), | |
"activities": [ | |
{ | |
"time": activity.get("time"), | |
"description": activity.get("description") | |
} | |
for activity in day_plan.get("activities", []) | |
] | |
} | |
for day_plan in trip_plan | |
], | |
"key": data.get("key", "Unknown Key") | |
} | |
# Print the formatted output | |
print("\nFormatted Trip Plan:") | |
print(formatted_plan) | |
return formatted_plan | |
except Exception as e: | |
print(f"Error occurred: {e}") | |
return None | |
if __name__ == "__main__": | |
try: | |
# Gather user inputs | |
days = int(input("Enter the number of days for the trip: ").strip()) | |
destination = input("Enter the destination: ").strip() | |
# Fetch trip plans | |
print("\nFetching trip plans...") | |
trip_plan = get_trip_planner(days, destination) | |
if not trip_plan: | |
print("No trip plans found or an error occurred.") | |
except Exception as e: | |
print(f"Error occurred: {e}") | |