From edfb97f9a72c918c19effbedaa20bd543ce7e214 Mon Sep 17 00:00:00 2001 From: mampfes Date: Sat, 17 Dec 2022 13:40:46 +0100 Subject: [PATCH] add XML support for umweltprofis_at fix #414 --- .../source/data_umweltprofis_at.py | 35 ++++++++++++++++++- doc/source/data_umweltprofis_at.md | 11 +++--- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/data_umweltprofis_at.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/data_umweltprofis_at.py index aeb1cf32..8ccd1004 100644 --- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/data_umweltprofis_at.py +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/data_umweltprofis_at.py @@ -1,5 +1,7 @@ import logging import requests +from datetime import datetime +from xml.dom.minidom import parseString from waste_collection_schedule import Collection # type: ignore[attr-defined] from waste_collection_schedule.service.ICS import ICS @@ -8,17 +10,33 @@ DESCRIPTION = "Source for Umweltprofis" URL = "https://www.umweltprofis.at" TEST_CASES = { "Ebensee": {"url": "https://data.umweltprofis.at/OpenData/AppointmentService/AppointmentService.asmx/GetIcalWastePickupCalendar?key=KXX_K0bIXDdk0NrTkk3xWqLM9-bsNgIVBE6FMXDObTqxmp9S39nIqwhf9LTIAX9shrlpfCYU7TG_8pS9NjkAJnM_ruQ1SYm3V9YXVRfLRws1"}, + "Rohrbach": {"xmlurl": "https://data.umweltprofis.at/opendata/AppointmentService/AppointmentService.asmx/GetTermineForLocationSecured?Key=TEMPKeyabvvMKVCic0cMcmsTEMPKey&StreetNr=118213&HouseNr=Alle&intervall=Alle"}, } _LOGGER = logging.getLogger(__name__) +def getText(element): + s = "" + for e in element.childNodes: + if e.nodeType == e.TEXT_NODE: + s += e.nodeValue + return s class Source: - def __init__(self, url): + def __init__(self, url=None, xmlurl=None): self._url = url + self._xmlurl = xmlurl self._ics = ICS() + if url is None and xmlurl is None: + raise Exception("either url or xmlurl needs to be specified") def fetch(self): + if self._url is not None: + return self.fetch_ics() + elif self._xmlurl is not None: + return self.fetch_xml() + + def fetch_ics(self): r = requests.get(self._url) if r.status_code != 200: _LOGGER.error("Error querying calendar data") @@ -32,3 +50,18 @@ class Source: for d in dates: entries.append(Collection(d[0], d[1])) return entries + + def fetch_xml(self): + r = requests.get(self._xmlurl) + r.raise_for_status() + + doc = parseString(r.text) + appointments = doc.getElementsByTagName("AppointmentEntry") + + entries = [] + for a in appointments: + date_string = getText(a.getElementsByTagName("Datum")[0]) + date = datetime.fromisoformat(date_string).date() + waste_type = getText(a.getElementsByTagName("WasteType")[0]) + entries.append(Collection(date, waste_type)) + return entries diff --git a/doc/source/data_umweltprofis_at.md b/doc/source/data_umweltprofis_at.md index ca45afb1..15bd4143 100644 --- a/doc/source/data_umweltprofis_at.md +++ b/doc/source/data_umweltprofis_at.md @@ -4,20 +4,21 @@ Support for schedules provided by [Umweltprofis.at](https://www.umweltprofis.at) ## Configuration via configuration.yaml -You need to generate your personal iCal Link before you can start using this source. Go to [https://data.umweltprofis.at/opendata/AppointmentService/index.aspx](https://data.umweltprofis.at/opendata/AppointmentService/index.aspx) and fill out the form. At the end, you can generate an iCal link. Copy this link and paste it to configuration.yaml as seen below. +You need to generate your personal XML link before you can start using this source. Go to [https://data.umweltprofis.at/opendata/AppointmentService/index.aspx](https://data.umweltprofis.at/opendata/AppointmentService/index.aspx) and fill out the form. At the end +at step 6 you get a link to a XML file. Copy this link and paste it to configuration.yaml as seen below. ```yaml waste_collection_schedule: sources: - name: data_umweltprofis_at args: - url: URL + xmlurl: URL ``` ### Configuration Variables -**URL**
-*(url) (required)* +**xmlurl**
+*(URL) (required)* ## Example @@ -26,5 +27,5 @@ waste_collection_schedule: sources: - name: data_umweltprofis_at args: - url: https://data.umweltprofis.at/OpenData/AppointmentService/AppointmentService.asmx/GetIcalWastePickupCalendar?key=xxx + xmlurl: https://data.umweltprofis.at/opendata/AppointmentService/AppointmentService.asmx/GetTermineForLocationSecured?Key=TEMPKeyabvvMKVCic0cMcmsTEMPKey&StreetNr=124972&HouseNr=Alle&intervall=Alle ``` \ No newline at end of file