Skip to content

Instantly share code, notes, and snippets.

@hensing
Created November 21, 2021 12:38
Show Gist options
  • Save hensing/6fb472b3446cb31e47bf9e1845a5f998 to your computer and use it in GitHub Desktop.
Save hensing/6fb472b3446cb31e47bf9e1845a5f998 to your computer and use it in GitHub Desktop.
Refactor BonnOrange Waste Management (Abfuhrpläne) iCalendar Events
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "2312e463",
"metadata": {},
"source": [
"# Notebook zum Filtern und Umformartieren der BonnOrange Abfuhrpläne\n",
"\n",
"**Anleitung** \n",
"\n",
"1.) Plan unter https://www.bonnorange.de/service/privatpersonen/abfuhrtermine/termine runterladen und als .ics exportieren \n",
"2.) Hier laden und laufen lassen \n",
"3.) Bei Google Calendar o.ä. importieren \n",
"\n",
"*Viel Spaß!*"
]
},
{
"cell_type": "markdown",
"id": "ef04145d",
"metadata": {},
"source": [
"### Settings"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "3e52715c",
"metadata": {},
"outputs": [],
"source": [
"DROP = 'Gross' # Großbehälter rausfiltern\n",
"ALARM = 7 * 60 # minutes before event"
]
},
{
"cell_type": "markdown",
"id": "e2413065",
"metadata": {},
"source": [
"### Let's go!"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f9c74fef",
"metadata": {},
"outputs": [],
"source": [
"from icalendar import Calendar, Event, Alarm, vText\n",
"from datetime import timedelta"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "4e3733bd",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"odict_values([vText('b'https://www5.bonn.de/WasteManagementBonnOrange''), vText('b'2.0''), vText('b'Europe/Berlin''), vText('b'Europe/Berlin''), vText('b'Abfuhrtermine''), vText('b'P1D''), vText('b'P1D'')])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Load Calendar\n",
"with open('Leerungstermine_2021.ics', encoding='utf-8') as f_in:\n",
" cal = Calendar.from_ical(f_in.read())\n",
"cal.values()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "b03492de",
"metadata": {},
"outputs": [],
"source": [
"# Transform\n",
"new = cal.copy()\n",
"\n",
"for event in cal.walk():\n",
" # get summary and skip empty events\n",
" summary = event.get('SUMMARY')\n",
" if summary is None:\n",
" continue\n",
" \n",
" # drop events matching string\n",
" if DROP and DROP in summary:\n",
" continue\n",
" \n",
" # change summary & description\n",
" if summary.startswith('Rest'): \n",
" event['SUMMARY'] = vText(\"🗑 Restmüll\")\n",
" color_name = 'grau'\n",
" color_event = 'gray'\n",
" \n",
" elif summary.startswith('Gelbe'):\n",
" event['SUMMARY'] = vText(\"♻ Gelbe Tonne\")\n",
" color_name = 'gelb'\n",
" color_event = 'gold'\n",
" \n",
" elif summary.startswith('Papier'):\n",
" event['SUMMARY'] = vText(\"📰 Blaue Tonne\")\n",
" color_name = 'blau'\n",
" color_event = 'navy'\n",
"\n",
" elif summary.startswith('Bio'):\n",
" event['SUMMARY'] = vText(\"🌱 Bio Tonne\")\n",
" color_name = 'grün'\n",
" color_event = 'green'\n",
" \n",
" elif summary.startswith('Sperr'):\n",
" event['SUMMARY'] = vText(\"🛋 Sperrmüll\")\n",
" color_name = None\n",
" color_event = 'saddlebrown'\n",
" event['DESCRIPTION'] = vText(\"Sperrmüll rausstellen!\")\n",
" \n",
" elif summary.startswith('Weihna'):\n",
" event['SUMMARY'] = vText(\"🎄 Weihnachtsbäume\")\n",
" color_name = None\n",
" color_event = 'saddlebrown'\n",
" event['DESCRIPTION'] = vText(\"Weihnachtsbaum rausstellen!\")\n",
" \n",
" else:\n",
" raise(Exception('Unbekannter Typ', summary))\n",
" \n",
" # change description\n",
" if color_name:\n",
" event['DESCRIPTION'] = vText(f\"{color_name.capitalize()}e Tonne rausstellen!\")\n",
" \n",
" # set color\n",
" event.add('COLOR', vText(color_event))\n",
"\n",
" # add alarm\n",
" if ALARM and event['SUMMARY'] != vText(\"Restmüll\"):\n",
" alarm = Alarm()\n",
" alarm.add('action', 'DISPLAY')\n",
" alert_time = timedelta(minutes=-int(ALARM))\n",
" alarm.add('trigger', alert_time)\n",
" event.add_component(alarm)\n",
" \n",
" # write into calendar\n",
" new.add_component(event)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c7814a00",
"metadata": {},
"outputs": [],
"source": [
"# Export\n",
"with open('Abfuhr_bereinigt.ics', 'wb') as f_out:\n",
" f_out.write(new.to_ical())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7801e39b",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment