Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import requests | |
| from datetime import date, timedelta | |
| # Replace these with your Amadeus credentials | |
| API_KEY = "8WbVdJv5wCGALf2FDQbWYjbhIDQzw8OU" | |
| API_SECRET = "nqDbIdh1vuaB1TBj" | |
| # Get access token from Amadeus | |
| def get_amadeus_token(): | |
| url = "https://test.api.amadeus.com/v1/security/oauth2/token" | |
| data = { | |
| "grant_type": "client_credentials", | |
| "client_id": API_KEY, | |
| "client_secret": API_SECRET, | |
| } | |
| response = requests.post(url, data=data) | |
| return response.json().get("access_token") | |
| # Search for cheapest flights | |
| def search_flights(origin, destination, depart_date, return_date=None): | |
| token = get_amadeus_token() | |
| url = "https://test.api.amadeus.com/v2/shopping/flight-offers" | |
| headers = {"Authorization": f"Bearer {token}"} | |
| params = { | |
| "originLocationCode": origin, | |
| "destinationLocationCode": destination, | |
| "departureDate": depart_date.isoformat(), | |
| "adults": 1, | |
| "currencyCode": "USD", | |
| "max": 5 | |
| } | |
| if return_date: | |
| params["returnDate"] = return_date.isoformat() | |
| response = requests.get(url, headers=headers, params=params) | |
| return response.json() | |
| # === Streamlit UI === | |
| st.title("✈️ Cheapest Airfare Finder (via Amadeus)") | |
| st.markdown("Find the cheapest flights using Amadeus API.") | |
| origin = st.text_input("Origin Airport Code (e.g. LAX, JFK):").upper() | |
| destination = st.text_input("Destination Airport Code (e.g. LON, CDG):").upper() | |
| depart_date = st.date_input("Departure Date", date.today() + timedelta(days=1)) | |
| return_date = st.date_input("Return Date (optional)", date.today() + timedelta(days=7)) | |
| search_btn = st.button("Search Flights") | |
| if search_btn: | |
| if origin and destination: | |
| with st.spinner("Searching for cheapest flights..."): | |
| results = search_flights(origin, destination, depart_date, return_date) | |
| if "data" in results: | |
| for offer in results["data"]: | |
| price = offer["price"]["total"] | |
| itineraries = offer["itineraries"] | |
| segments = itineraries[0]["segments"] | |
| dep = segments[0]["departure"]["at"] | |
| arr = segments[-1]["arrival"]["at"] | |
| carrier = segments[0]["carrierCode"] | |
| st.markdown(f""" | |
| **✈️ {origin} → {destination}** | |
| - Price: **${price}** | |
| - Airline: {carrier} | |
| - Departure: {dep} | |
| - Arrival: {arr} | |
| """) | |
| else: | |
| st.error("No flights found. Try different inputs.") | |
| else: | |
| st.warning("Please enter both origin and destination airport codes.") | |