File size: 2,626 Bytes
ca8930f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c206733
ca8930f
 
 
c206733
ca8930f
 
c206733
ca8930f
 
 
c206733
 
 
ca8930f
 
c206733
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca8930f
 
 
c206733
ca8930f
 
 
 
 
 
 
 
 
c206733
ca8930f
c206733
ca8930f
 
 
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
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}")