Skip to content

Commit

Permalink
CWOP module bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
joergschultzelutter committed Dec 30, 2023
1 parent fcfa0f5 commit 57a5512
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
67 changes: 40 additions & 27 deletions src/cwop_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ def get_cwop_findu(cwop_id: str, units: str = "metric"):
soup = BeautifulSoup(resp.text, features="html.parser")
if soup:
matches = re.search(
r"\b(Sorry, no weather reports found)\b", soup.get_text(), re.IGNORECASE
r"\b(Sorry, no weather reports found)\b",
soup.get_text(),
re.IGNORECASE,
)
if not matches:
# Tabelle parsen; Regex funktioniert nicht immer sauber
Expand All @@ -123,31 +125,37 @@ def get_cwop_findu(cwop_id: str, units: str = "metric"):
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
try:
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
except IndexError:
logger.error(
msg=f"Index error while parsing regular CWOP data, output_row={output_rows}"
)

cwop_response = {
"cwop_id": cwop_id,
"time": my_timestamp,
Expand Down Expand Up @@ -256,7 +264,12 @@ def get_nearest_cwop_findu(latitude: float, longitude: float, units: str = "metr
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)
try:
return get_cwop_findu(output_rows[1][0], units)
except IndexError:
logger.error(
msg=f"Index error while parsing CWOP 'near' data, output_row={output_rows}"
)
# 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.39"
mpad_version: str = "0.40"
#
###########################
# Constants, do not change#
Expand Down

0 comments on commit 57a5512

Please sign in to comment.