refactor offenbach_de

This commit is contained in:
mampfes
2023-03-14 07:40:52 +01:00
parent 94e3e70e52
commit 4a60fc5cfa

View File

@@ -1,21 +1,18 @@
import re
import requests
from datetime import datetime
import requests
from waste_collection_schedule import Collection # type: ignore[attr-defined]
from waste_collection_schedule.service.ICS import ICS
TITLE = "Abfallkalender Offenbach am Main"
DESCRIPTION = "Source für Abfallkalender Offenbach"
URL = "https://www.offenbach.de"
##ABFALLPATH="/stadtwerke/stadtservice/Entsorgung/abfallkalender.php"
TEST_CASES = {
"offenbach": {"f_id_location": 7036 }, # KaiserStraße 1
"offenbach": {"f_id_location": 7036}, # KaiserStraße 1
}
API_URL = "https://www.insert-it.de/BmsAbfallkalenderOffenbach/Main/Calender"
RE_FILTER_NAME = re.compile(r': (.*?)\s+\(')
ICON_MAP = {
"Restmüll": "mdi:trash-can",
@@ -24,27 +21,32 @@ ICON_MAP = {
"Altpapier": "mdi:package-variant",
}
class Source:
def __init__(self, f_id_location):
self._location = f_id_location
self._ics = ICS()
self._ics = ICS(regex=r"Leerung:\s+(.*)\s+\(.*\)")
def fetch(self):
now = datetime.now()
entries = self.fetch_year(now.year)
if now.month == 12:
entries.append(self.fetch_year(now.year + 1))
return entries
def fetch_year(self, year):
s = requests.Session()
params = {
"bmsLocationId": self._location,
"year": datetime.now().year
}
params = {"bmsLocationId": self._location, "year": year}
r = s.get(API_URL, params=params)
r.raise_for_status()
r.encoding = "utf-8"
entries = []
dates = self._ics.convert(r.content.decode('utf-8'))
dates = self._ics.convert(r.text)
for d in dates:
m = RE_FILTER_NAME.search(d[1])
mull_type = m.group(1) if m else d[1].replace("Leerung: ", "")
entries.append(Collection(d[0], mull_type, icon=ICON_MAP.get(mull_type, "mdi:trash-can")))
entries.append(Collection(d[0], d[1], icon=ICON_MAP.get(d[1])))
return entries
return entries