From 5dd2413e310841099d122f59a48aac6bea31cbe5 Mon Sep 17 00:00:00 2001 From: mampfes Date: Sun, 26 Apr 2020 14:18:39 +0200 Subject: [PATCH] add support for generic ics files --- README.md | 1 + .../package/source/ics.py | 62 +++++++++++++++++++ doc/source/ics.md | 30 +++++++++ info.md | 1 + 4 files changed, 94 insertions(+) create mode 100644 custom_components/waste_collection_schedule/package/source/ics.py create mode 100644 doc/source/ics.md diff --git a/README.md b/README.md index 8568d7e4..415a9ff1 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ Currently the following service providers are supported: - [AbfallNavi / RegioIT.de](./doc/source/regioit_de.md) - [AbfallPlus.de / Abfall.IO](./doc/source/abfall_io.md) - [AWBKoeln.de](./doc/source/awbkoeln_de.md) +- [Generic ICS File](./doc/source/ics.md) - [Jumomind.de](./doc/source/jumomind_de.md) - [Stadtreinigung.Hamburg](./doc/source/stadtreinigung_hamburg.md) diff --git a/custom_components/waste_collection_schedule/package/source/ics.py b/custom_components/waste_collection_schedule/package/source/ics.py new file mode 100644 index 00000000..a3954370 --- /dev/null +++ b/custom_components/waste_collection_schedule/package/source/ics.py @@ -0,0 +1,62 @@ +import requests +import datetime +import icalendar +from collections import OrderedDict + +from ..helpers import CollectionAppointment + + +DESCRIPTION = "Source for ICS based services" +URL = "" +TEST_CASES = OrderedDict( + [ + ( + "Ludwigsburg", + { + "url": "https://www.avl-ludwigsburg.de/fileadmin/Files/Abfallkalender/ICS/Privat/Privat_{%Y}_Ossweil.ics" + }, + ) + ] +) + + +class Source: + def __init__(self, url): + self.url = url + + def fetch(self): + if "{%Y}" in self.url: + # url contains wildcard + now = datetime.datetime.now() + url = self.url.replace("{%Y}", str(now.year)) + entries = self.fetch_year(url) + if now.month == 12: + # also get data for next year if we are already in december + url = self.url.replace("{%Y}", str(now.year + 1)) + entries.extend(self.fetch_year(url)) + return entries + else: + return self.fetch_year(self.url) + + def fetch_year(self, url): + # get ics file + r = requests.get(url) + r.encoding = "utf-8" # requests doesn't guess the encoding correctly + + entries = [] + + # parse ics file + calender = icalendar.Calendar.from_ical(r.text) + + entries = [] + for e in calender.walk(): + if e.name == "VEVENT": + dtstart = None + if type(e.get("dtstart").dt) == datetime.date: + dtstart = e.get("dtstart").dt + elif type(e.get("dtstart").dt) == datetime.datetime: + dtstart = e.get("dtstart").dt.date() + summary = str(e.get("summary")) + entries.append(CollectionAppointment(dtstart, summary)) + + return entries diff --git a/doc/source/ics.md b/doc/source/ics.md new file mode 100644 index 00000000..ae9574a7 --- /dev/null +++ b/doc/source/ics.md @@ -0,0 +1,30 @@ +# ICS + +Add support for generic ICS file which are downloaded from a fix location. The waste type will be taken from the `summary` attribute. + +## Configuration via configuration.yaml + +```yaml +waste_collection_schedule: + sources: + - name: ics + args: + url: URL +``` + +### Configuration Variables + +**url**
+*(string) (required)* +If the original url contains the current year (4 digits including century), this can be replaced by the wildcard `{%Y}` (see example below). + +## Example + +```yaml +waste_collection_schedule: + sources: + - name: ics + args: + url: "https://www.avl-ludwigsburg.de/fileadmin/Files/Abfallkalender/ICS/Privat/Privat_{%Y}_Ossweil.ics" +``` + diff --git a/info.md b/info.md index 03c9478d..f60fa5f9 100644 --- a/info.md +++ b/info.md @@ -32,5 +32,6 @@ Currently the following service providers are supported: - AbfallNavi / RegioIT.de - AbfallPlus.de / Abfall.IO - AWBKoeln.de +- Generic ICS File - Jumomind.de - Stadtreinigung.Hamburg