From 1953a87a08c5d4d70aac2282ab758a949c0168f1 Mon Sep 17 00:00:00 2001 From: Jack Sweeney Date: Tue, 10 Nov 2020 01:19:33 +0000 Subject: [PATCH] Auto download airports.dat --- NotifyBotMulti.py | 5 ++-- defAirport.py | 71 ++++++++++++++++++++++++++++++----------------- planeClass.py | 6 ++-- 3 files changed, 52 insertions(+), 30 deletions(-) diff --git a/NotifyBotMulti.py b/NotifyBotMulti.py index 620b651..3c29a3a 100644 --- a/NotifyBotMulti.py +++ b/NotifyBotMulti.py @@ -9,7 +9,9 @@ if platform.system() == "Windows": init(convert=True) from planeClass import Plane from datetime import datetime +from defAirport import DownloadAirports import pytz +DownloadAirports() main_config = configparser.ConfigParser() main_config.read('./configs/mainconf.ini') import os @@ -82,5 +84,4 @@ while True: sys.stdout.flush() time.sleep(1) sys.stdout.write(Back.RED + ('\x1b[1K\r' +"Slept for " +str(sleep_sec)) + Style.RESET_ALL) - print() - + print() \ No newline at end of file diff --git a/defAirport.py b/defAirport.py index 5970e8c..97d4a0f 100644 --- a/defAirport.py +++ b/defAirport.py @@ -1,27 +1,48 @@ #https://www.geeksforgeeks.org/python-calculate-distance-between-two-places-using-geopy/ #https://openflights.org/data.html -def getAirport(latitude, longitude): - import json - import csv - from geopy.distance import geodesic - plane = (latitude, longitude) - header = ["id", "name", "city", "country", "iata", "icao", "lat", "lng", "alt", "tz", "dst", "tz_db", "type", "source"] - airports = [] - first_run = True - with open('airports.dat', encoding='utf-8') as csvf: - reader = csv.DictReader( csvf, header) - #for row in reader: - # airports.append(row) - for row in reader: - airport = row - airport_coord = float(airport['lat']), float(airport['lng']) - airport_dist = float((geodesic(plane, airport_coord).mi)) - if first_run: - closest_airport_dict = airport - closest_airport_dist = airport_dist - first_run = False - elif airport_dist < closest_airport_dist: - closest_airport_dict = airport - closest_airport_dist = airport_dist - print("Closest Airport:", closest_airport_dict['icao'], closest_airport_dict['name'], closest_airport_dist, "Miles Away") - return closest_airport_dict \ No newline at end of file +def DownloadAirports(): + import os + if not os.path.isfile('airports.dat'): + print("No airports.dat file, downloading now") + try: + import requests + url = 'https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat' + airports = requests.get(url) + + open('airports.dat', 'wb').write(airports.content) + except: + raise("Error getting airports.dat or storing") + else: + #Writes current date to airports.dat to show when it was aqquired + import datetime + date = datetime.datetime.now() + with open('airports.dat', 'a') as airports: + airports.write("#" + str(date)) + print("Successfully got airports.dat") + elif os.path.isfile('airports.dat'): + print("Already Have airports.dat, continuing") +def getClosestAirport(latitude, longitude): + import json + import csv + from geopy.distance import geodesic + plane = (latitude, longitude) + header = ["id", "name", "city", "country", "iata", "icao", "lat", "lng", "alt", "tz", "dst", "tz_db", "type", "source"] + airports = [] + first_run = True + with open('airports.dat', encoding='utf-8') as csvf: + reader = csv.DictReader(filter(lambda row: row[0]!='#', csvf), header) + #for row in reader: + # airports.append(row) + for row in reader: + airport = row + airport_coord = float(airport['lat']), float(airport['lng']) + airport_dist = float((geodesic(plane, airport_coord).mi)) + if first_run: + closest_airport_dict = airport + closest_airport_dist = airport_dist + first_run = False + elif airport_dist < closest_airport_dist: + closest_airport_dict = airport + closest_airport_dist = airport_dist + print("Closest Airport:", closest_airport_dict['icao'], closest_airport_dict['name'], closest_airport_dist, "Miles Away") + return closest_airport_dict \ No newline at end of file diff --git a/planeClass.py b/planeClass.py index eb58e5d..7db254c 100644 --- a/planeClass.py +++ b/planeClass.py @@ -47,7 +47,7 @@ class Plane: import platform from datetime import datetime from tabulate import tabulate - from defAirport import getAirport + from defAirport import getClosestAirport if self.config.get('MAP', 'OPTION') == "GOOGLESTATICMAP": from defMap import getMap elif self.config.get('MAP', 'OPTION') == "ADSBX": @@ -304,7 +304,7 @@ class Plane: raise Exception("Map option not set correctly in this planes conf") #Discord if self.config.getboolean('DISCORD', 'ENABLE'): - nearest = getAirport(self.latitude, self.longitude) + nearest = getClosestAirport(self.latitude, self.longitude) self.dis_message = (self.dis_title + " " + self.tookoff_message + nearest['icao'] + ", " + nearest["name"]).strip() sendDis(self.dis_message, self.map_file_name, self.config) #PushBullet @@ -346,7 +346,7 @@ class Plane: raise Exception("Map option not set correctly in this planes conf") #Discord if self.config.getboolean('DISCORD', 'ENABLE'): - nearest = getAirport(self.last_latitude, self.last_longitude) + nearest = getClosestAirport(self.last_latitude, self.last_longitude) self.dis_message = (self.dis_title + " " +self.landed_message + nearest['icao'] + ", " + nearest["name"]).strip() sendDis(self.dis_message, self.map_file_name, self.config) #PushBullet