Skip to content
This repository was archived by the owner on Nov 19, 2021. It is now read-only.

Commit dede994

Browse files
committed
Sanitize county name
The ministry sadly does some horrid stuff to their HTML and has implemented hyphenation manually, leading to some county names now being split in weird ways after extraction. The following replacements takes place: * "- <upper case letter>" -> "-<upper case letter>" * "- <lower case letter>" -> "<lower case letter>"
1 parent d57f688 commit dede994

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

custom_components/coronavirus_hessen/__init__.py

+39
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from datetime import timedelta
44
import logging
5+
import re
56

67
import async_timeout
78
import asyncio
@@ -19,6 +20,8 @@
1920

2021
PLATFORMS = ["sensor"]
2122

23+
HYPHEN_PATTERN = re.compile(r"- (.)")
24+
2225
async def async_setup(hass: HomeAssistant, config: dict):
2326
"""Set up the Coronavirus Hessen component."""
2427
# Make sure coordinator is initialized.
@@ -87,8 +90,10 @@ async def async_get_data():
8790
_LOGGER.exception("Error processing line {}, skipping".format(line))
8891
continue
8992

93+
county = sanitize_county(county)
9094
if county == "Gesamtergebnis":
9195
county = OPTION_TOTAL
96+
9297
result[county] = dict(cases=cases, hospitalized=hospitalized, deaths=deaths)
9398

9499
_LOGGER.debug("Corona Hessen: {!r}".format(result))
@@ -109,3 +114,37 @@ def parse_num(s, t=int):
109114
if len(s) and s != "-":
110115
return t(s.replace(".", "").replace(",", "."))
111116
return 0
117+
118+
def sanitize_county(county):
119+
"""
120+
Sanitizes the county.
121+
122+
The ministry sadly does some horrid stuff to their HTML
123+
and has implemented hyphenation manually, leading to
124+
some county names now being split in weird ways after
125+
extraction.
126+
127+
The following replacements takes place:
128+
129+
* "- <upper case letter>" -> "-<upper case letter>"
130+
* "- <lower case letter>" -> "<lower case letter>"
131+
132+
Examples:
133+
134+
>>> sanitize_county("LK Main-Kinzig- Kreis")
135+
<<< "LK Main-Kinzig-Kreis"
136+
>>> sanitize_county("LK Wetterau- kreis")
137+
<<< "LK Wetteraukreis"
138+
>>> sanitize_county("SK Frankfurt am Main")
139+
<<< "SK Frankfurt am Main"
140+
"""
141+
142+
def replace(m):
143+
letter = m.group(1)
144+
if letter.islower():
145+
return letter
146+
else:
147+
return "-{}".format(letter)
148+
149+
return HYPHEN_PATTERN.sub(replace, county)
150+

0 commit comments

Comments
 (0)