Skip to content

Instantly share code, notes, and snippets.

@saurbhc
Created August 25, 2024 21:45
Show Gist options
  • Save saurbhc/8e0ac7b7d5abd594c740b16a07be4e2d to your computer and use it in GitHub Desktop.
Save saurbhc/8e0ac7b7d5abd594c740b16a07be4e2d to your computer and use it in GitHub Desktop.
Flight Tracker; example cities: Jaipur -> Delhi; flight number: `I5 1316`
from __future__ import annotations
import requests
import datetime
def main() -> int:
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:129.0) Gecko/20100101 Firefox/129.0',
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json',
'Accept-Language': 'en-GB,en;q=0.5',
'Connection': 'keep-alive',
'Priority': 'u=0',
}
response = requests.get(
"https://www.flightstats.com/v2/api-next/flight-tracker/other-days/I5*/1316",
headers=headers,
)
response.raise_for_status()
request_data = response.json()
request_data = request_data['data']
for data in request_data:
flights = data['flights']
for flight in flights:
arrival_airport = flight['arrivalAirport']
departure_airport = flight['departureAirport']
if arrival_airport['city'] == 'Delhi' and \
arrival_airport['name'] == 'Delhi Indira Gandhi International Airport' and \
departure_airport['city'] == 'Jaipur' and \
departure_airport['name'] == 'Jaipur International Airport':
# get year month date from url 'url': '/flight-tracker/I5*/1316?year=2024&month=08&date=22&flightId=1269951751'
url = flight['url']
url = url.split('?')[-1]
url = url.split('&')
_flight = {}
for u in url:
key, value = u.split('=')
_flight[key] = value
url = f"https://www.flightstats.com/v2/api-next/flight-tracker/I5*/1316/{_flight['year']}/{_flight['month']}/{_flight['date']}/{_flight['flightId']}"
response = requests.get(url, headers=headers)
response.raise_for_status()
flight_data = response.json()
flight_data = flight_data['data']
arrival_delay = flight_data['status']['delay']['arrival']['minutes']
departure_delay = flight_data['status']['delay']['departure']['minutes']
scheduledArrival = flight_data['schedule']['scheduledArrival']
scheduledArrival = datetime.datetime.strptime(scheduledArrival, '%Y-%m-%dT%H:%M:%S.%f')
scheduledArrival = scheduledArrival.strftime('%Y-%m-%d %H:%M:%S')
msg = f"{scheduledArrival} "
if flight_data['status']['color'] == 'green':
msg += u"\U0001F7E2 "
else:
msg += u"\U0001F7E5 "
msg += f"{departure_airport['city']} -> {arrival_airport['city']} "
msg += f"I5 1316 " # flight number
msg += f"({flight_data['flightNote']['message']}) "
msg += f"({flight_data['status']['status']}) "
msg += f"({flight_data['status']['statusDescription']}) "
msg += f"\n - arrival delay: {arrival_delay} minutes"
msg += f"\n - departure delay: {departure_delay} minutes"
print(msg)
return 0
if __name__ == "__main__":
raise SystemExit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment