automatic calendar try 0

This commit is contained in:
Clara Dautermann 2025-04-10 20:29:40 +02:00
parent 833dfd852c
commit 0ee099368c
Signed by: clara
GPG key ID: 223391B52FAD4463
305 changed files with 2665 additions and 126 deletions

View file

@ -2,11 +2,15 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import json"
"import json\n",
"from ics import Calendar, Event\n",
"from ics.grammar.parse import ContentLine\n",
"import datetime\n",
"import os"
]
},
{
@ -49,6 +53,115 @@
"\n",
"print(\", \".join(allmails))"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"def mapday(day):\n",
" return day.replace(' ', '').replace(':', '').lower()"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"def create_event(weekday, timeframe):\n",
" appointment = Event()\n",
"\n",
" times = timeframe.split(' - ')\n",
" begin = times[0].split(':')\n",
" end = times[1].split(':')\n",
" \n",
"\n",
" # All days have been validated to be one of [\"Mo :\", \"Di :\", \"Mi :\", \"Do :\", \"Fr :\"]\n",
" # We just use the week this was developed at. Events recur weekly anyways…\n",
" if weekday == 'Mo :':\n",
" day = 7\n",
" elif weekday == 'Di :':\n",
" day = 8\n",
" elif weekday == 'Mi :':\n",
" day = 9\n",
" elif weekday == 'Do :':\n",
" day = 10\n",
" elif weekday == 'Fr :':\n",
" day = 11\n",
"\n",
" appointment.begin = datetime.datetime(2025, 4, day, int(begin[0]), int(begin[1]), 0)\n",
" appointment.end = datetime.datetime(2025, 4, day, int(end[0]), int(end[1]), 0)\n",
"\n",
" appointment.extra.append(ContentLine(name=\"RRULE\", value=f\"FREQ=WEEKLY;INTERVAL=1;WKST=MO\"))\n",
" return appointment\n"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
"def create_ics_files(therapists, path):\n",
" # create a calendar for each therapist\n",
" for therapist in therapists:\n",
" # one .ics file per therapist\n",
" therapist_calendar = Calendar()\n",
" # extract all days\n",
" for day in therapist['phone_appointments']:\n",
" # create an event for each phone timeframe\n",
" for i, phone_time in enumerate(therapist['phone_appointments'][day]):\n",
" # non parsable times\n",
" if len(phone_time.split(\" - \")) != 2:\n",
" print(therapist['name'])\n",
"\n",
" appointment = create_event(day, phone_time)\n",
" appointment.name = therapist['name']\n",
"\n",
" # save appointment\n",
" outpath = os.path.join(path, f'{therapist['name'].replace(' ', '_').replace('/', '&')}_{mapday(day)}_{str(i)}.ics')\n",
" with open(outpath, \"w+\", newline='') as outfile:\n",
" outfile.write(appointment.serialize()) "
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"215 of 275 therapists callable.\n",
"113 of 215 therapists offer VT.\n"
]
}
],
"source": [
"with open('./data/therapists.json', 'r') as infile:\n",
" loaded = json.load(infile)\n",
"\n",
"# only want the ones we can actually call\n",
"therapists = list(filter(lambda t: 'phone_appointments' in t and 'phone' in t, loaded))\n",
"\n",
"print(f'{len(therapists)} of {len(loaded)} therapists callable.')\n",
"\n",
"# filter for VT\n",
"with_vt = list(filter(lambda entry: 'Verhaltenstherapeutische Einzeltherapie Erwachsene' in entry['therapy_types'], therapists))\n",
"print(f'{len(with_vt)} of {len(therapists)} therapists offer VT.')\n",
"\n",
"create_ics_files(with_vt, os.path.join('data', 'icsfiles', 'with_vt'))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {