generated from hjess/PythonTemplateProject
[main] sync
All checks were successful
Build, Push, and Deploy to Nomad / docker-nomad (push) Successful in 1m1s
All checks were successful
Build, Push, and Deploy to Nomad / docker-nomad (push) Successful in 1m1s
This commit is contained in:
@@ -2,12 +2,12 @@ import calendar
|
||||
from datetime import date, timedelta
|
||||
|
||||
def skriv_datoer_med_formattering(år):
|
||||
start_dato = date(år, 1, 1)
|
||||
start_dato = date(år, 1, 1+10)
|
||||
slut_dato = date(år, 12, 29)
|
||||
dags_differens = timedelta(days=1)
|
||||
|
||||
print(f"Datoer for året {år}:\n")
|
||||
|
||||
resultat = []
|
||||
nuværende_dato = start_dato
|
||||
while nuværende_dato <= slut_dato:
|
||||
ugenummer = nuværende_dato.isocalendar()[1]
|
||||
@@ -17,14 +17,16 @@ def skriv_datoer_med_formattering(år):
|
||||
# Tabuler outputtet længere ind
|
||||
if ugedag == "Friday":
|
||||
print(f"{nuværende_dato.strftime('%Y-%m-%d')} Til Porto fra København")
|
||||
resultat.append({'date': nuværende_dato.strftime('%Y-%m-%d'), 'orgin': 'CPH', 'dest': 'LIS','uge':ugenummer, 'month':nuværende_dato.month})
|
||||
|
||||
if ugedag == "Wednesday":
|
||||
print(f"{nuværende_dato.strftime('%Y-%m-%d')} Til København fra Porto")
|
||||
resultat.append( { 'date': nuværende_dato.strftime( '%Y-%m-%d' ), 'orgin': 'LIS', 'dest': 'CPH','uge':ugenummer,'month':nuværende_dato.month} )
|
||||
|
||||
|
||||
# else:
|
||||
# print(f"{nuværende_dato.strftime('%Y-%m-%d')} ({ugedag})")
|
||||
|
||||
nuværende_dato += dags_differens
|
||||
|
||||
print(resultat)
|
||||
# Kald funktionen med det ønskede år
|
||||
skriv_datoer_med_formattering(2025)
|
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -1,10 +1,8 @@
|
||||
import json
|
||||
import time
|
||||
from datetime import datetime
|
||||
|
||||
from amadeus import Client, ResponseError
|
||||
|
||||
from pprint import pprint
|
||||
|
||||
|
||||
# OopCompanion:suppressRename
|
||||
|
||||
@@ -14,10 +12,7 @@ class AmadeusClient:
|
||||
"""
|
||||
Initialize the Amadeus API client.
|
||||
"""
|
||||
self.client = Client(
|
||||
client_id = client_id,
|
||||
client_secret = client_secret
|
||||
)
|
||||
self.client = Client(client_id=client_id, client_secret=client_secret,hostname="production")
|
||||
|
||||
def find_cheapest_flights(self, origin: str, destination: str, departure_date: str, adults: int = 1):
|
||||
"""
|
||||
@@ -31,65 +26,101 @@ class AmadeusClient:
|
||||
"""
|
||||
try:
|
||||
response = self.client.shopping.flight_offers_search.get(
|
||||
originLocationCode = origin,
|
||||
destinationLocationCode = destination,
|
||||
departureDate = departure_date,
|
||||
adults = adults,
|
||||
originLocationCode=origin,
|
||||
destinationLocationCode=destination,
|
||||
departureDate=departure_date,
|
||||
adults=adults,
|
||||
max=250,
|
||||
nonStop="true"
|
||||
nonStop="true",
|
||||
)
|
||||
return response.data
|
||||
except ResponseError as error:
|
||||
raise 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 ) )
|
||||
|
||||
|
||||
def parse_flight_data(self, flights):
|
||||
"""
|
||||
Parse the flight data to extract useful information about flights.
|
||||
|
||||
:param flights: Raw flight offers data.
|
||||
:return: Parsed flight details.
|
||||
"""
|
||||
result = []
|
||||
for flight_offer in flights:
|
||||
itineraries = flight_offer.get("itineraries", [])
|
||||
price = float(flight_offer.get("price", {}).get("total", 0))
|
||||
|
||||
for itinerary in itineraries:
|
||||
segments = itinerary.get("segments", [])
|
||||
for segment in segments:
|
||||
if segment.get("numberOfStops", -1) == 0: # Only non-stop flights
|
||||
departure = segment.get("departure", {})
|
||||
arrival = segment.get("arrival", {})
|
||||
travel_time = (
|
||||
datetime.fromisoformat(arrival["at"]) - datetime.fromisoformat(departure["at"])
|
||||
).total_seconds() / 3600
|
||||
|
||||
result.append({
|
||||
"departure": {
|
||||
"iataCode": departure.get("iataCode"),
|
||||
"at": departure.get("at"),
|
||||
},
|
||||
"arrival": {
|
||||
"iataCode": arrival.get("iataCode"),
|
||||
"at": arrival.get("at"),
|
||||
},
|
||||
"travel_time": travel_time,
|
||||
"price": price,
|
||||
})
|
||||
return result
|
||||
|
||||
def get_flight_summary(self, departure_date: str, origin: str, destination: str):
|
||||
"""
|
||||
Get a summary of the flight details for a specific date, origin, and destination.
|
||||
|
||||
:param departure_date: The date of travel in YYYY-MM-DD format.
|
||||
:param origin: The IATA code for the origin location.
|
||||
:param destination: The IATA code for the destination location.
|
||||
:return: Formatted flight summary string.
|
||||
"""
|
||||
time.sleep(5) # Avoid rate limiting
|
||||
flights = self.find_cheapest_flights(origin, destination, departure_date)
|
||||
parsed_flights = self.parse_flight_data(flights)
|
||||
with open("data_chunks.txt", "a") as fp:
|
||||
json.dump(parsed_flights, fp,indent = 4)
|
||||
fp.write(",")
|
||||
|
||||
|
||||
if not parsed_flights:
|
||||
return f"No flights found for {departure_date}: {origin} -> {destination}"
|
||||
|
||||
average_price = sum(f["price"] for f in parsed_flights) / len(parsed_flights)
|
||||
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}"
|
||||
|
||||
|
||||
# Example usage
|
||||
if __name__ == "__main__":
|
||||
client_id = 'uxDqIh36xPAUvpXnXynwAnH86pGBdIch'
|
||||
client_secret = 'xTSLooNZpJWantb5'
|
||||
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}]
|
||||
|
||||
amadeus_client = AmadeusClient( client_id, client_secret )
|
||||
with open( "summery.txt", 'w' ) as fp:
|
||||
for days in travel_days:
|
||||
origin = days['orgin']
|
||||
destination = days['dest']
|
||||
departure_date = days['date']
|
||||
|
||||
try:
|
||||
#flights = amadeus_client.find_cheapest_flights(
|
||||
# origin = 'LIS',
|
||||
# destination = 'CPH',
|
||||
# departure_date = '2025-01-15',
|
||||
# adults = 1,
|
||||
#)
|
||||
#pprint( flights )
|
||||
#with open( "flights.json", "w" ) as file:
|
||||
# json.dump( flights, file, indent = 4 )
|
||||
with open( "flights.json", "r" ) as file:
|
||||
flights = json.load( file )
|
||||
try:
|
||||
summary = amadeus_client.get_flight_summary( departure_date, origin, destination )
|
||||
print( summary )
|
||||
fp.writelines( summary + '\n' )
|
||||
except ResponseError as error:
|
||||
print( f"An error occurred: {error}" )
|
||||
|
||||
for flight_offer in flights:
|
||||
itineraries = flight_offer.get( 'itineraries', [] )
|
||||
result = []
|
||||
price = flight_offer.get( 'price', { } ).get( 'total', 0 )
|
||||
for itinerary in itineraries:
|
||||
segments = itinerary.get( 'segments', [] )
|
||||
for segment in segments:
|
||||
numberOfStops = segment.get( 'numberOfStops', -1 )
|
||||
if numberOfStops == 0:
|
||||
departure = segment.get( 'departure', { } )
|
||||
arrival = segment.get( 'arrival', { } )
|
||||
travel_time = datetime.fromisoformat( str(arrival['at']) ) - datetime.fromisoformat( str(departure['at']) )
|
||||
|
||||
result.append( {
|
||||
"departure": {
|
||||
"iataCode": departure.get( "iataCode" ),
|
||||
"at": departure.get( "at" ),
|
||||
},
|
||||
"arrival": {
|
||||
"iataCode": arrival.get( "iataCode" ),
|
||||
"at": arrival.get( "at" ),
|
||||
}
|
||||
,
|
||||
"travel_time": {
|
||||
"time":travel_time,
|
||||
}
|
||||
} )
|
||||
pprint( result )
|
||||
except ResponseError as error:
|
||||
print( f"An error occurred: {error}" )
|
||||
1754
inspiration/TingSomSkalTilføjes/data_chunks.txt
Normal file
1754
inspiration/TingSomSkalTilføjes/data_chunks.txt
Normal file
File diff suppressed because it is too large
Load Diff
50
inspiration/TingSomSkalTilføjes/summery.txt
Normal file
50
inspiration/TingSomSkalTilføjes/summery.txt
Normal file
@@ -0,0 +1,50 @@
|
||||
2025-01-15: LIS -> CPH - 128.87
|
||||
2025-01-17: CPH -> LIS - 374.72
|
||||
2025-01-29: LIS -> CPH - 128.87
|
||||
2025-01-31: CPH -> LIS - 313.24
|
||||
2025-02-12: LIS -> CPH - 152.62
|
||||
2025-02-14: CPH -> LIS - 518.62
|
||||
2025-02-26: LIS -> CPH - 162.87
|
||||
2025-02-28: CPH -> LIS - 167.78
|
||||
2025-03-12: LIS -> CPH - 171.87
|
||||
2025-03-14: CPH -> LIS - 254.00
|
||||
2025-03-26: LIS -> CPH - 211.62
|
||||
2025-03-28: CPH -> LIS - 193.06
|
||||
2025-04-09: LIS -> CPH - 158.87
|
||||
2025-04-11: CPH -> LIS - 998.95
|
||||
2025-04-23: LIS -> CPH - 226.87
|
||||
2025-04-25: CPH -> LIS - 261.38
|
||||
2025-05-07: LIS -> CPH - 196.12
|
||||
2025-05-09: CPH -> LIS - 304.88
|
||||
2025-05-21: LIS -> CPH - 160.29
|
||||
2025-05-23: CPH -> LIS - 199.16
|
||||
2025-06-04: LIS -> CPH - 161.62
|
||||
2025-06-06: CPH -> LIS - 300.38
|
||||
2025-06-18: LIS -> CPH - 155.12
|
||||
2025-06-20: CPH -> LIS - 257.88
|
||||
2025-07-02: LIS -> CPH - 149.62
|
||||
2025-07-04: CPH -> LIS - 288.57
|
||||
2025-07-16: LIS -> CPH - 158.87
|
||||
2025-07-18: CPH -> LIS - 261.76
|
||||
2025-07-30: LIS -> CPH - 277.62
|
||||
2025-08-01: CPH -> LIS - 169.11
|
||||
2025-08-13: LIS -> CPH - 194.62
|
||||
2025-08-15: CPH -> LIS - 140.19
|
||||
2025-08-27: LIS -> CPH - 145.95
|
||||
2025-08-29: CPH -> LIS - 240.52
|
||||
2025-09-10: LIS -> CPH - 158.87
|
||||
2025-09-12: CPH -> LIS - 289.38
|
||||
2025-09-24: LIS -> CPH - 170.12
|
||||
2025-09-26: CPH -> LIS - 261.38
|
||||
2025-10-08: LIS -> CPH - 162.87
|
||||
2025-10-10: CPH -> LIS - 261.38
|
||||
2025-10-22: LIS -> CPH - 145.87
|
||||
2025-10-24: CPH -> LIS - 227.52
|
||||
2025-11-05: LIS -> CPH - 151.12
|
||||
2025-11-07: CPH -> LIS - 142.80
|
||||
2025-11-19: LIS -> CPH - 116.62
|
||||
2025-11-21: CPH -> LIS - 135.80
|
||||
2025-12-03: LIS -> CPH - 116.62
|
||||
No flights found for 2025-12-05: CPH -> LIS
|
||||
2025-12-17: LIS -> CPH - 116.62
|
||||
2025-12-19: CPH -> LIS - 143.30
|
||||
Reference in New Issue
Block a user