fix: Use async fetching

This commit is contained in:
Adrian Rumpold
2021-06-18 15:31:51 +02:00
parent a03a4c79c1
commit 5ba354c86d
2 changed files with 8 additions and 10 deletions

View File

@@ -66,8 +66,8 @@ async def get_coordinator(hass):
return hass.data[DOMAIN] return hass.data[DOMAIN]
async def async_get_data() -> IncidenceData: async def async_get_data() -> IncidenceData:
crawler = CovidCrawler() crawler = CovidCrawler(hass)
return crawler.crawl() return await crawler.crawl()
hass.data[DOMAIN] = DataUpdateCoordinator( hass.data[DOMAIN] = DataUpdateCoordinator(
hass, hass,

View File

@@ -5,8 +5,8 @@ import re
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from dataclasses import dataclass from dataclasses import dataclass
import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from homeassistant import aiohttp_client
_log = logging.getLogger(__name__) _log = logging.getLogger(__name__)
@@ -29,12 +29,13 @@ class CovidCrawlerBase(ABC):
class CovidCrawler(CovidCrawlerBase): class CovidCrawler(CovidCrawlerBase):
def __init__(self) -> None: def __init__(self, hass) -> None:
self.url = ( self.url = (
"https://www.augsburg.de/umwelt-soziales/gesundheit/coronavirus/fallzahlen" "https://www.augsburg.de/umwelt-soziales/gesundheit/coronavirus/fallzahlen"
) )
self.hass = hass
def crawl(self) -> IncidenceData: async def crawl(self) -> IncidenceData:
""" """
Fetch COVID-19 infection data from the target website. Fetch COVID-19 infection data from the target website.
""" """
@@ -43,11 +44,8 @@ class CovidCrawler(CovidCrawlerBase):
locale.setlocale(locale.LC_ALL, "de_DE.utf8") locale.setlocale(locale.LC_ALL, "de_DE.utf8")
result = requests.get(self.url) result = await aiohttp_client.async_get_clientsession(self.hass).get(self.url)
if not result.ok: soup = BeautifulSoup(await result.text(), features="html.parser")
result.raise_for_status()
soup = BeautifulSoup(result.text, features="html.parser")
match = soup.find(class_="frame--type-textpic") match = soup.find(class_="frame--type-textpic")
text = match.p.text text = match.p.text