Skip to content

Commit

Permalink
bs4 bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
joergschultzelutter committed Oct 20, 2023
1 parent 8394e3e commit fcfa0f5
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 205 deletions.
43 changes: 22 additions & 21 deletions src/airport_data_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,27 +173,28 @@ def get_metar_data(icao_code: str):
if resp:
if resp.status_code == 200:
soup = BeautifulSoup(resp.text, features="html.parser")
meintext = re.sub(" {2,}", " ", soup.get_text())

# Start by searching the web site for whether our request has failed
matches = re.search(r"\b(sorry)\b", meintext, re.IGNORECASE)
if not matches:
# Request seems to be successful, search for the airport position in the text
pos = meintext.find(icao_code)
if pos != -1:
remainder = meintext[pos:]
# Split the resulting text up into multiple lines
metar_result_array = remainder.splitlines()
# start with an empty response
response = ""
# Iterate through the list. We stop at that line which starts with TAF (TAF report)
# Everything else before that line is our METAR report which may consist of 1..n lines
for metar_result_item in metar_result_array:
if metar_result_item.startswith("TAF"):
response = response + " ### "
response = response + metar_result_item
success = True
response = response.strip()
if soup:
meintext = re.sub(" {2,}", " ", soup.get_text())

# Start by searching the web site for whether our request has failed
matches = re.search(r"\b(sorry)\b", meintext, re.IGNORECASE)
if not matches:
# Request seems to be successful, search for the airport position in the text
pos = meintext.find(icao_code)
if pos != -1:
remainder = meintext[pos:]
# Split the resulting text up into multiple lines
metar_result_array = remainder.splitlines()
# start with an empty response
response = ""
# Iterate through the list. We stop at that line which starts with TAF (TAF report)
# Everything else before that line is our METAR report which may consist of 1..n lines
for metar_result_item in metar_result_array:
if metar_result_item.startswith("TAF"):
response = response + " ### "
response = response + metar_result_item
success = True
response = response.strip()
return success, response


Expand Down
115 changes: 59 additions & 56 deletions src/cwop_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,47 +106,48 @@ def get_cwop_findu(cwop_id: str, units: str = "metric"):
if resp:
if resp.status_code == 200:
soup = BeautifulSoup(resp.text, features="html.parser")
matches = re.search(
r"\b(Sorry, no weather reports found)\b", soup.get_text(), re.IGNORECASE
)
if not matches:
# Tabelle parsen; Regex funktioniert nicht immer sauber
table = soup.find("table")
output_rows = []
if table:
for table_row in table.findAll("tr"):
columns = table_row.findAll("td")
output_row = []
for column in columns:
output_row.append(column.text.strip())
output_rows.append(output_row)
if len(output_rows) > 0:
if len(output_rows[0]) >= 10:
time = output_rows[1][0]
time_year = int(time[0:4])
time_month = int(time[4:6])
time_day = int(time[6:8])
time_hh = int(time[8:10])
time_mm = int(time[10:12])
time_ss = int(time[12:14])
my_timestamp = datetime.datetime(
year=time_year,
month=time_month,
day=time_day,
hour=time_hh,
minute=time_mm,
second=time_ss,
)
temp = output_rows[1][1]
wind_direction = output_rows[1][2]
wind_speed = output_rows[1][3]
wind_gust = output_rows[1][4]
rain_1h = output_rows[1][5]
rain_24h = output_rows[1][6]
rain_mn = output_rows[1][7]
humidity = output_rows[1][8]
air_pressure = output_rows[1][9]
success = True
if soup:
matches = re.search(
r"\b(Sorry, no weather reports found)\b", soup.get_text(), re.IGNORECASE
)
if not matches:
# Tabelle parsen; Regex funktioniert nicht immer sauber
table = soup.find("table")
output_rows = []
if table:
for table_row in table.findAll("tr"):
columns = table_row.findAll("td")
output_row = []
for column in columns:
output_row.append(column.text.strip())
output_rows.append(output_row)
if len(output_rows) > 0:
if len(output_rows[0]) >= 10:
time = output_rows[1][0]
time_year = int(time[0:4])
time_month = int(time[4:6])
time_day = int(time[6:8])
time_hh = int(time[8:10])
time_mm = int(time[10:12])
time_ss = int(time[12:14])
my_timestamp = datetime.datetime(
year=time_year,
month=time_month,
day=time_day,
hour=time_hh,
minute=time_mm,
second=time_ss,
)
temp = output_rows[1][1]
wind_direction = output_rows[1][2]
wind_speed = output_rows[1][3]
wind_gust = output_rows[1][4]
rain_1h = output_rows[1][5]
rain_24h = output_rows[1][6]
rain_mn = output_rows[1][7]
humidity = output_rows[1][8]
air_pressure = output_rows[1][9]
success = True
cwop_response = {
"cwop_id": cwop_id,
"time": my_timestamp,
Expand Down Expand Up @@ -239,21 +240,23 @@ def get_nearest_cwop_findu(latitude: float, longitude: float, units: str = "metr
if resp:
if resp.status_code == 200:
soup = BeautifulSoup(resp.text, features="html.parser")
matches = re.search(r"\b(sorry)\b", soup.get_text(), re.IGNORECASE)
if not matches:
# Parse table
table = soup.find("table")
output_rows = []
for table_row in table.findAll("tr"):
columns = table_row.findAll("td")
output_row = []
for column in columns:
output_row.append(column.text.strip())
output_rows.append(output_row)
if len(output_rows) > 0:
if len(output_rows[0]) >= 13:
# call findu again as the previous URL does not support units :-(
return get_cwop_findu(output_rows[1][0], units)
if soup:
matches = re.search(r"\b(sorry)\b", soup.get_text(), re.IGNORECASE)
if not matches:
# Parse table
table = soup.find("table")
if table:
output_rows = []
for table_row in table.findAll("tr"):
columns = table_row.findAll("td")
output_row = []
for column in columns:
output_row.append(column.text.strip())
output_rows.append(output_row)
if len(output_rows) > 0:
if len(output_rows[0]) >= 13:
# call findu again as the previous URL does not support units :-(
return get_cwop_findu(output_rows[1][0], units)
# This code will only be triggered in the event of a failure
cwop_response = {
"cwop_id": cwop_id,
Expand Down
2 changes: 1 addition & 1 deletion src/mpad_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#
# Program version
#
mpad_version: str = "0.38"
mpad_version: str = "0.39"
#
###########################
# Constants, do not change#
Expand Down
Loading

0 comments on commit fcfa0f5

Please sign in to comment.