IvanCastl

RADAR

Apr 14th, 2025 (edited)
600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | Cybersecurity | 0 0
  1. import requests
  2. import csv
  3.  
  4. # Configuración
  5. api_key = 'DKJNJbahjvhxavscjjyu7388' # ¡Reemplaza con tu API key real!
  6. aeropuertos_mexico = ['MEX', 'MTY', 'GDL', 'CUN', 'TIJ', 'HMO'] # Principales aeropuertos
  7.  
  8. # Archivo CSV
  9. with open('vuelos_mexico.csv', 'w', newline='', encoding='utf-8') as file:
  10. writer = csv.writer(file)
  11. writer.writerow(['Vuelo', 'Aerolínea', 'Aeropuerto_Salida', 'Destino', 'Estado', 'Hora_Salida'])
  12.  
  13. # Consultar cada aeropuerto
  14. for aeropuerto in aeropuertos_mexico:
  15. url = f"http://api.aviationstack.com/v1/flights?access_key={api_key}&dep_iata={aeropuerto}"
  16. response = requests.get(url)
  17. data = response.json()
  18.  
  19. print(f"\n✈️ Vuelos desde {aeropuerto}:\n")
  20.  
  21. if 'data' in data and data['data']:
  22. for flight in data['data']:
  23. # Datos para CSV
  24. vuelo = flight['flight']['iata']
  25. aerolinea = flight['airline']['name']
  26. destino = flight['arrival']['airport']
  27. estado = flight['flight_status']
  28. hora_salida = flight['departure']['scheduled'].split('T')[1][:5] if flight['departure']['scheduled'] else "N/A"
  29.  
  30. # Escribir en CSV
  31. writer.writerow([vuelo, aerolinea, aeropuerto, destino, estado, hora_salida])
  32.  
  33. # Imprimir en consola
  34. print(f"🔹 {vuelo} | {aerolinea}")
  35. print(f" Destino: {destino}")
  36. print(f" Hora: {hora_salida} | Estado: {estado.upper()}")
  37. print("-" * 50)
  38. else:
  39. print(f"No hay vuelos registrados en {aeropuerto}.")
  40.  
  41. print("\n✅ Datos guardados en 'vuelos_mexico.csv'")
Tags: python
Advertisement
Add Comment
Please, Sign In to add comment