forked from aleiby/kml2g1000
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfas.py
81 lines (56 loc) · 2.83 KB
/
fas.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import sys
import requests
from bs4 import BeautifulSoup
URL = "https://flightaware.com/live/flight/"
class INCORRECTURL(Exception):
pass
def findPlaneData(tailnum):
"""Looks through global URL for history of aircraft based on the tailnumber through flightaware.com. There is a limitation of 14 days when
using this function because of the way flightaware.com stores data.
Args:
tailnum (str): The six character tail number of the aircraft.
Returns:
dataPoints (list): A list of lists containing the data points for each flight. Each list entry contains the following data points:
[0] = Date in YYYMMDD format
[1] = Time in HHMMZ UTC format
[2] = Departing airport
[3] = Destination airport
"""
page = requests.get(URL + tailnum + "/history")
soup = BeautifulSoup(page.content, "html.parser")
table = soup.findAll(class_="nowrap")
if not table:
print("No data found for this tail number. Please try downloading the files manually. Exiting...")
sys.exit(1)
dataPoints = []
for t in table:
rawData = t.find("a", href=True)
dataPoint = str(rawData["href"]).split("/")
dataPoints.append(dataPoint[-4:])
return dataPoints
def downloadKML(tailnum, dataset, flight):
"""Downloads the KML file for a specific flight from flightaware.com. The KML file is a Google Earth file that contains the flight path. This function
is used in conjunction with the fas.findPlaneData() function.
Args:
tailnum (str): The six character tail number of the aircraft.
dataset (list): A list of lists containing the data points for each flight. This is the output from the fas.findPlaneData() function.
flight (int): The index of the flight in the dataset list that you want to download the KML file for.
Returns:
_ (requests.models.Response): The KML file is downloaded to the return paramater
"""
dURL = URL + tailnum + "/history/" + dataset[flight][0] + "/" + dataset[flight][1] + "Z/" + dataset[flight][2] + "/" + dataset[flight][3] + "/google_earth"
return requests.get(dURL, allow_redirects=True)
def downloadFLink(flightLink):
"""Downloads the KML file for a specific flight from flightaware.com. The KML file is a Google Earth file that contains the flight path.
Args:
flightLink (str): The flightaware.com URL of the flight you would like to download.
Returns:
_ (requests.models.Response): The KML file is downloaded to the return paramater
"""
parts = flightLink.split("/")
if parts[-1] != "tracklog":
raise INCORRECTURL
parts[-1] = "google_earth"
parts[-4] = parts[-4] + "Z"
_url = "/".join(parts)
return requests.get(_url, allow_redirects=True)