From 2f73be90106a51fbca02b12ce701512359e45f8e Mon Sep 17 00:00:00 2001 From: Adrian Rumpold Date: Wed, 11 Aug 2021 08:12:48 +0200 Subject: [PATCH] chore: Refactor duplicated HTTP fetching code --- .../crawler.py | 39 ++++++++----------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/custom_components/home_assistant_covid19_augsburg/crawler.py b/custom_components/home_assistant_covid19_augsburg/crawler.py index e77d4dc..a977d8b 100644 --- a/custom_components/home_assistant_covid19_augsburg/crawler.py +++ b/custom_components/home_assistant_covid19_augsburg/crawler.py @@ -78,16 +78,9 @@ class CovidCrawler(CovidCrawlerBase): def __init__(self, hass=None) -> None: self.hass = hass - async def crawl_incidence(self) -> IncidenceData: - """ - Fetch COVID-19 infection data from the target website. - """ + async def _fetch(self, url: str) -> str: + """Fetch a URL, using either the current Home Assistant instance or requests""" - _log.info("Fetching COVID-19 data update") - - url = ( - "https://www.augsburg.de/umwelt-soziales/gesundheit/coronavirus/fallzahlen" - ) if self.hass: from homeassistant.helpers import aiohttp_client @@ -99,6 +92,19 @@ class CovidCrawler(CovidCrawlerBase): result = requests.get(url) result.raise_for_status() soup = BeautifulSoup(result.text, "html.parser") + return soup + + async def crawl_incidence(self) -> IncidenceData: + """ + Fetch COVID-19 infection data from the target website. + """ + + _log.info("Fetching COVID-19 data update") + + url = ( + "https://www.augsburg.de/umwelt-soziales/gesundheit/coronavirus/fallzahlen" + ) + soup = await self._fetch(url) match = soup.find(class_="frame--type-textpic") text = match.p.text @@ -155,20 +161,9 @@ class CovidCrawler(CovidCrawlerBase): url = ( "https://www.augsburg.de/umwelt-soziales/gesundheit/coronavirus/impfzentrum" ) + soup = await self._fetch(url) + container_id = "c1088140" - - if self.hass: - from homeassistant.helpers import aiohttp_client - - result = await aiohttp_client.async_get_clientsession(self.hass).get(url) - soup = BeautifulSoup(await result.text(), "html.parser") - else: - import requests - - result = requests.get(url) - result.raise_for_status() - soup = BeautifulSoup(result.text, "html.parser") - result = soup.find(id=container_id) text = re.sub(r"\s+", " ", result.text) regexes = [