[main] Flight Things
All checks were successful
Build, Push, and Deploy to Nomad / docker-nomad (push) Successful in 1m54s

This commit is contained in:
2025-01-21 21:04:40 +01:00
parent ae999e1fac
commit e4fd13a782
12 changed files with 9508 additions and 95 deletions

View File

@@ -1,18 +1,31 @@
import json
import time
from datetime import datetime
import pandas as pd
from amadeus import Client, ResponseError
import calender_maker
from inspiration.TingSomSkalTilføjes.calender_maker import CalenderMaker
# OopCompanion:suppressRename
class AmadeusClient:
def __init__(self, client_id: str, client_secret: str):
def __init__(self, prod=bool):
"""
Initialize the Amadeus API client.
"""
self.client = Client(client_id=client_id, client_secret=client_secret,hostname="production")
if prod:
client_id = "ABRGQv6U7IWAYxwwmjqAOPUDGvuFMSjw"
client_secret = "BcwpSKf3FICJIxaw"
self.client = Client(client_id=client_id, client_secret=client_secret,host="api.amadeus.com")
print("Getting production Amadeus API client")
else:
client_id = "uxDqIh36xPAUvpXnXynwAnH86pGBdIch"
client_secret = "xTSLooNZpJWantb5"
self.client = Client( client_id = client_id, client_secret = client_secret,host="test.api.amadeus.com" )
print( "Getting development Amadeus API client" )
def find_cheapest_flights(self, origin: str, destination: str, departure_date: str, adults: int = 1):
"""
@@ -34,11 +47,13 @@ class AmadeusClient:
nonStop="true",
)
return response.data
except ResponseError as error:
if error.response.status_code == 429:
retry_after = error.response.headers.get( 'Retry-After', '60' ) # standard fallback til 60 sekunder
print( f"Rate limit reached. Retry after {retry_after} seconds." )
time.sleep( int( retry_after ) )
print(error)
def parse_flight_data(self, flights):
@@ -101,26 +116,61 @@ class AmadeusClient:
first_departure = parsed_flights[0]["departure"]["iataCode"]
last_arrival = parsed_flights[-1]["arrival"]["iataCode"]
return f"{departure_date}: {first_departure} -> {last_arrival} - {average_price:.2f}"
#return f"{departure_date}: {first_departure} -> {last_arrival} - {average_price:.2f}"
return {"departure_date":departure_date, "departure": first_departure, "arrival": last_arrival,"price": average_price}
# Example usage
if __name__ == "__main__":
client_id = "ABRGQv6U7IWAYxwwmjqAOPUDGvuFMSjw"
client_secret = "BcwpSKf3FICJIxaw"
travel_days = [{'date': '2025-01-15', 'orgin': 'LIS', 'dest': 'CPH', 'uge': 3, 'month': 1}, {'date': '2025-01-17', 'orgin': 'CPH', 'dest': 'LIS', 'uge': 3, 'month': 1}, {'date': '2025-01-29', 'orgin': 'LIS', 'dest': 'CPH', 'uge': 5, 'month': 1}, {'date': '2025-01-31', 'orgin': 'CPH', 'dest': 'LIS', 'uge': 5, 'month': 1}, {'date': '2025-02-12', 'orgin': 'LIS', 'dest': 'CPH', 'uge': 7, 'month': 2}, {'date': '2025-02-14', 'orgin': 'CPH', 'dest': 'LIS', 'uge': 7, 'month': 2}, {'date': '2025-02-26', 'orgin': 'LIS', 'dest': 'CPH', 'uge': 9, 'month': 2}, {'date': '2025-02-28', 'orgin': 'CPH', 'dest': 'LIS', 'uge': 9, 'month': 2}, {'date': '2025-03-12', 'orgin': 'LIS', 'dest': 'CPH', 'uge': 11, 'month': 3}, {'date': '2025-03-14', 'orgin': 'CPH', 'dest': 'LIS', 'uge': 11, 'month': 3}, {'date': '2025-03-26', 'orgin': 'LIS', 'dest': 'CPH', 'uge': 13, 'month': 3}, {'date': '2025-03-28', 'orgin': 'CPH', 'dest': 'LIS', 'uge': 13, 'month': 3}, {'date': '2025-04-09', 'orgin': 'LIS', 'dest': 'CPH', 'uge': 15, 'month': 4}, {'date': '2025-04-11', 'orgin': 'CPH', 'dest': 'LIS', 'uge': 15, 'month': 4}, {'date': '2025-04-23', 'orgin': 'LIS', 'dest': 'CPH', 'uge': 17, 'month': 4}, {'date': '2025-04-25', 'orgin': 'CPH', 'dest': 'LIS', 'uge': 17, 'month': 4}, {'date': '2025-05-07', 'orgin': 'LIS', 'dest': 'CPH', 'uge': 19, 'month': 5}, {'date': '2025-05-09', 'orgin': 'CPH', 'dest': 'LIS', 'uge': 19, 'month': 5}, {'date': '2025-05-21', 'orgin': 'LIS', 'dest': 'CPH', 'uge': 21, 'month': 5}, {'date': '2025-05-23', 'orgin': 'CPH', 'dest': 'LIS', 'uge': 21, 'month': 5}, {'date': '2025-06-04', 'orgin': 'LIS', 'dest': 'CPH', 'uge': 23, 'month': 6}, {'date': '2025-06-06', 'orgin': 'CPH', 'dest': 'LIS', 'uge': 23, 'month': 6}, {'date': '2025-06-18', 'orgin': 'LIS', 'dest': 'CPH', 'uge': 25, 'month': 6}, {'date': '2025-06-20', 'orgin': 'CPH', 'dest': 'LIS', 'uge': 25, 'month': 6}, {'date': '2025-07-02', 'orgin': 'LIS', 'dest': 'CPH', 'uge': 27, 'month': 7}, {'date': '2025-07-04', 'orgin': 'CPH', 'dest': 'LIS', 'uge': 27, 'month': 7}, {'date': '2025-07-16', 'orgin': 'LIS', 'dest': 'CPH', 'uge': 29, 'month': 7}, {'date': '2025-07-18', 'orgin': 'CPH', 'dest': 'LIS', 'uge': 29, 'month': 7}, {'date': '2025-07-30', 'orgin': 'LIS', 'dest': 'CPH', 'uge': 31, 'month': 7}, {'date': '2025-08-01', 'orgin': 'CPH', 'dest': 'LIS', 'uge': 31, 'month': 8}, {'date': '2025-08-13', 'orgin': 'LIS', 'dest': 'CPH', 'uge': 33, 'month': 8}, {'date': '2025-08-15', 'orgin': 'CPH', 'dest': 'LIS', 'uge': 33, 'month': 8}, {'date': '2025-08-27', 'orgin': 'LIS', 'dest': 'CPH', 'uge': 35, 'month': 8}, {'date': '2025-08-29', 'orgin': 'CPH', 'dest': 'LIS', 'uge': 35, 'month': 8}, {'date': '2025-09-10', 'orgin': 'LIS', 'dest': 'CPH', 'uge': 37, 'month': 9}, {'date': '2025-09-12', 'orgin': 'CPH', 'dest': 'LIS', 'uge': 37, 'month': 9}, {'date': '2025-09-24', 'orgin': 'LIS', 'dest': 'CPH', 'uge': 39, 'month': 9}, {'date': '2025-09-26', 'orgin': 'CPH', 'dest': 'LIS', 'uge': 39, 'month': 9}, {'date': '2025-10-08', 'orgin': 'LIS', 'dest': 'CPH', 'uge': 41, 'month': 10}, {'date': '2025-10-10', 'orgin': 'CPH', 'dest': 'LIS', 'uge': 41, 'month': 10}, {'date': '2025-10-22', 'orgin': 'LIS', 'dest': 'CPH', 'uge': 43, 'month': 10}, {'date': '2025-10-24', 'orgin': 'CPH', 'dest': 'LIS', 'uge': 43, 'month': 10}, {'date': '2025-11-05', 'orgin': 'LIS', 'dest': 'CPH', 'uge': 45, 'month': 11}, {'date': '2025-11-07', 'orgin': 'CPH', 'dest': 'LIS', 'uge': 45, 'month': 11}, {'date': '2025-11-19', 'orgin': 'LIS', 'dest': 'CPH', 'uge': 47, 'month': 11}, {'date': '2025-11-21', 'orgin': 'CPH', 'dest': 'LIS', 'uge': 47, 'month': 11}, {'date': '2025-12-03', 'orgin': 'LIS', 'dest': 'CPH', 'uge': 49, 'month': 12}, {'date': '2025-12-05', 'orgin': 'CPH', 'dest': 'LIS', 'uge': 49, 'month': 12}, {'date': '2025-12-17', 'orgin': 'LIS', 'dest': 'CPH', 'uge': 51, 'month': 12}, {'date': '2025-12-19', 'orgin': 'CPH', 'dest': 'LIS', 'uge': 51, 'month': 12}]
cd = CalenderMaker()
travel_days = cd.get_dates(year = 2025, dest_day="Thursday", orgin_day="Tuesday")
amadeus_client = AmadeusClient( prod = True)
import csv
amadeus_client = AmadeusClient( client_id, client_secret )
with open( "summery.txt", 'w' ) as fp:
with open( "summery.txt", 'w' ) as fp, open( 'data_csv.csv', "w" ) as fp2:
writer = csv.DictWriter( fp2, fieldnames = ['orgin', 'dest', 'date', 'price','note'] )
writer.writeheader()
for days in travel_days:
origin = days['orgin']
destination = days['dest']
departure_date = days['date']
try:
summary = amadeus_client.get_flight_summary( departure_date, origin, destination )
print( summary )
fp.writelines( summary + '\n' )
try:
summary = amadeus_client.get_flight_summary( departure_date, origin, destination )
print(summary)
writer.writerow( {
"orgin": summary["departure_date"],
"dest": summary["departure"],
"date": summary["arrival"],
"price": summary['price'],
"note": ""
} )
fp.writelines( str(summary) + '\n' )
except Exception as e:
print( f"Error fetching flight summary for {origin} -> {destination} on {departure_date}" )
writer.writerow( {
"orgin": origin,
"dest": destination,
"date": departure_date,
"price": 214.0,
"note": "False - Flight not found"
} )
pass
except ResponseError as error:
print( f"An error occurred: {error}" )
df = pd.read_csv( 'data_csv.csv' )
# Write the raw data to the first sheet
with pd.ExcelWriter( 'output.xlsx' ) as writer:
df.to_excel( writer, sheet_name = 'Sheet1', index = False )
# Calculate the average monthly data
df['date'] = pd.to_datetime( df['date'] )
df['month'] = df['date'].dt.to_period( 'M' )
avg_monthly_data = df.groupby( 'month' )['price'].mean().reset_index()
avg_monthly_data.columns = ['Month', 'Average Price']
# Write the average monthly data to the second sheet
avg_monthly_data.to_excel( writer, sheet_name = 'Sheet2', index = False )