Last active
February 11, 2024 17:03
-
-
Save under0tech/3a90f56855e954fecfb57d1ed106f0e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import airportsdata | |
from geopy.distance import geodesic | |
from datetime import datetime, timedelta | |
from FlightRadar24 import FlightRadar24API | |
TELEGRAM_URL = 'https://api.telegram.org' | |
TELEGRAM_BOT_ID = 'bot5603010001:III_xNHA14A9xznXjjcUD98GXT-A5zAAxzX' # Your bot | |
TELEGRAM_CHAT_ID = '-1008101011052' # Your airport reminder channel | |
def send_message(message): | |
response = requests.post( | |
f'{TELEGRAM_URL}/{TELEGRAM_BOT_ID}/sendMessage?chat_id={TELEGRAM_CHAT_ID}&parse_mode=Markdown&text={message}') | |
return response | |
def get_airport_coordinates(iata_code): | |
airports = airportsdata.load('IATA') | |
if iata_code in airports: | |
airport = airports[iata_code] | |
lat = round(airport['lat'], 3) | |
lon = round(airport['lon'], 3) | |
return (lat, lon) | |
else: | |
return None | |
def get_aircraft_trail_details(reg): | |
fr_api = FlightRadar24API() | |
flights = fr_api.get_flights(registration=reg) | |
if flights: | |
flight_details = fr_api.get_flight_details(flights[0]) | |
if flight_details: | |
aircraft_lat = round(flight_details['trail'][0]['lat'], 3) | |
aircraft_lon = round(flight_details['trail'][0]['lng'], 3) | |
aircraft_kts = int(flight_details['trail'][0]['spd']) | |
flight_num = flight_details['identification']['number']['default'] | |
return (aircraft_lat, aircraft_lon, aircraft_kts, flight_num) | |
return None | |
def calculate_time_to_reach_airport(airport_coords, aircraft_coords, groundspeed_kts): | |
groundspeed_kmh = groundspeed_kts * 1.852 | |
distance_km = geodesic(airport_coords, aircraft_coords).kilometers | |
time_minutes = int(distance_km / groundspeed_kmh * 60) | |
return time_minutes | |
def get_flight_eta(reg): | |
eta = 0 | |
fr_api = FlightRadar24API() | |
flights = fr_api.get_flights(registration=reg) | |
if flights: | |
flight_details = fr_api.get_flight_details(flights[0]) | |
if flight_details: | |
arrival_time = datetime.fromtimestamp(flight_details['time']['estimated']['arrival']) | |
current_time = datetime.now() | |
time_difference = (arrival_time - current_time).total_seconds() / 60 | |
eta = time_difference | |
return int(eta) | |
def aircraft_approaches_airport_go(request): | |
request_json = request.get_json() | |
airport_iata_code = request_json.get('airport_iata_code') | |
aircraft_reg = request_json.get('aircraft_reg') | |
remind_me = int(request_json.get('remind_me')) | |
airport_coords = get_airport_coordinates(airport_iata_code) | |
if airport_coords: | |
print(f'{airport_iata_code} airport coordinates: {airport_coords}') | |
acrd = get_aircraft_trail_details(aircraft_reg) | |
if acrd: | |
print(f'Trail details for {aircraft_reg}: {acrd}') | |
minutes = calculate_time_to_reach_airport(airport_coords, (acrd[0], acrd[1]), acrd[2]) | |
print(f'Calculated time of arrival to {airport_iata_code} for flight {acrd[3]}: {minutes} min.') | |
minutes_eta = get_flight_eta(aircraft_reg) | |
if minutes_eta > 0: | |
print(f'Estimated time of arrival to {airport_iata_code} for flight {acrd[3]}: {minutes_eta} min.') | |
average_estimation = int((minutes + minutes_eta) / 2) | |
print(f'Average ETA for flight {acrd[3]}: {average_estimation} min.') | |
if remind_me >= average_estimation: | |
print('You have to go....') | |
message = f'*AIRPORT REMINDER*: Estimated time of arrival' + \ | |
f' to *{airport_iata_code}* for flight *{acrd[3]}* is less' + \ | |
f' than {average_estimation} min. ' + \ | |
f'\n\t_You have to go to the airport to pick up your girlfriend in time!_' | |
send_message(message) | |
return f'Airport reminder: DONE!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment