-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflight_data.py
More file actions
196 lines (167 loc) · 4.96 KB
/
flight_data.py
File metadata and controls
196 lines (167 loc) · 4.96 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import requests
import urllib.request
from bs4 import BeautifulSoup
def metar(url):
data = []
# Pulling web XML file
raw_xml = urllib.request.urlopen(url)
raw_data = raw_xml.read()
# Parsing XML file
bs_data = BeautifulSoup(raw_data, "xml")
# Pulling required data
fc = bs_data.find('flight_category')
wd = bs_data.find('wind_dir_degrees')
ws = bs_data.find('wind_speed_kt')
vis = bs_data.find('visibility_statute_mi')
# Pulling required text and checking for missing data
if fc is None:
data.append("Unknown")
else:
data.append(fc.text)
if wd is None:
data.append("Unknown")
else:
data.append(wd.text)
if ws is None:
data.append("Unknown")
else:
data.append(ws.text)
if vis is None:
data.append("Unknown")
else:
data.append(vis.text)
# Closing XML file
raw_xml.close()
return data
def metar_raw(url):
# Replace other metar bot on home assistant
data = "unavalible"
# Pulling web XML file
raw_xml = urllib.request.urlopen(url)
raw_data = raw_xml.read()
# Parsing XML file
bs_data = BeautifulSoup(raw_data, "xml")
# Pulling required data
data = bs_data.find('raw_text')
if data is None:
data = "unavalible"
raw_xml.close()
return data.text
def flight_restrictions(url):
output = []
# Getting data
data = requests.get(url)
html = BeautifulSoup(data.text, 'html.parser')
data = html.select('.auto-style1b')[0].get_text()
output.append(data)
data = html.select('.auto-style2b')[0].get_text()
output.append(data)
data = html.select('.auto-style3b')[0].get_text()
output.append(data)
return output
def fr_notes(url, line):
line = int(line)
line_c = line + 3
selection = ".auto-style{}".format(line_c)
# Getting data
data = requests.get(url)
html = BeautifulSoup(data.text, 'html.parser')
data = html.select(selection)[0].get_text()
return data
def fr_notes_b(url, line):
line = int(line)
line_c = line + 3
selection = ".auto-style{}b".format(line_c)
# Getting data
data = requests.get(url)
html = BeautifulSoup(data.text, 'html.parser')
data = html.select(selection)[0].get_text()
return data
def fr_notes_all(url):
data = requests.get(url)
html = BeautifulSoup(data.text, 'html.parser')
output = [0]
i =4
while i < 10:
selection = f".auto-style{i}"
try:
data = html.select(selection)[0].get_text()
except IndexError:
break
output.append(data)
i += 1
output[0] = i - 4 #Kept changing this between 2 & 4 something not right
return output
def fr_notes_count(url):
data = requests.get(url)
html = BeautifulSoup(data.text, 'html.parser')
i = 4
while i < 10:
selection = f".auto-style{i}"
try:
data = html.select(selection)[0].get_text()
except IndexError:
break
i += 1
i = i - 4 #Kept changing this between 2 & 4 something not right
return i
def fr_autowx(url, count):
i = 4
count = int(count) + 4
result = ["False", 9999]
# Strings to test against
autowx = "at or before"
eod = "rest of the"
test = "removed"
# Getting data
data = requests.get(url)
html = BeautifulSoup(data.text, 'html.parser')
# Testing each line
while i < count:
selection = ".auto-style{}".format(i)
try:
data = html.select(selection)[0].get_text()
except IndexError:
data = "Empty"
# AutoWX Time
if autowx in data:
result[0] = "True"
data = data.split(" ")
data = data[9]
data = data.split(",")
result[1] = int(data[0])
return result
# AutoWX end of day
elif eod and test in data:
result[0] = "True"
result[1] = "end of day"
return result
i += 1
return "False"
def pressure_altitude(airport):
# Replace other metar bot on home assistant
url = f"https://aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=3&mostRecent=true&stationString={airport}"
data = "unavalible"
# Pulling web XML file
raw_xml = urllib.request.urlopen(url)
raw_data = raw_xml.read()
# Parsing XML file
bs_data = BeautifulSoup(raw_data, "xml")
# Pulling required data
altim = bs_data.find('altim_in_hg')
elev_m = bs_data.find('elevation_m')
if altim is None:
altim = "unavalible"
return altim
if elev_m is None:
elev_m = "unavalible"
return elev_m
elev_f = float(elev_m.text) * 3.28084
raw_xml.close()
f_press = float(altim.text)
s_press = 29.92
f_elev = 845
press_c = f_press - s_press
elev_c = press_c * 1000
pa = float(elev_c + elev_f)
return f"{airport} Pressure Altitude: {pa:.0f}ft"