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

Commit

Permalink
Sanitize county name
Browse files Browse the repository at this point in the history
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>"
  • Loading branch information
foosel committed Apr 20, 2020
1 parent d57f688 commit dede994
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions custom_components/coronavirus_hessen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from datetime import timedelta
import logging
import re

import async_timeout
import asyncio
Expand All @@ -19,6 +20,8 @@

PLATFORMS = ["sensor"]

HYPHEN_PATTERN = re.compile(r"- (.)")

async def async_setup(hass: HomeAssistant, config: dict):
"""Set up the Coronavirus Hessen component."""
# Make sure coordinator is initialized.
Expand Down Expand Up @@ -87,8 +90,10 @@ async def async_get_data():
_LOGGER.exception("Error processing line {}, skipping".format(line))
continue

county = sanitize_county(county)
if county == "Gesamtergebnis":
county = OPTION_TOTAL

result[county] = dict(cases=cases, hospitalized=hospitalized, deaths=deaths)

_LOGGER.debug("Corona Hessen: {!r}".format(result))
Expand All @@ -109,3 +114,37 @@ def parse_num(s, t=int):
if len(s) and s != "-":
return t(s.replace(".", "").replace(",", "."))
return 0

def sanitize_county(county):
"""
Sanitizes the county.
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>"
Examples:
>>> sanitize_county("LK Main-Kinzig- Kreis")
<<< "LK Main-Kinzig-Kreis"
>>> sanitize_county("LK Wetterau- kreis")
<<< "LK Wetteraukreis"
>>> sanitize_county("SK Frankfurt am Main")
<<< "SK Frankfurt am Main"
"""

def replace(m):
letter = m.group(1)
if letter.islower():
return letter
else:
return "-{}".format(letter)

return HYPHEN_PATTERN.sub(replace, county)

0 comments on commit dede994

Please sign in to comment.