{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "1293f184", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import os\n", "import requests\n", "import urllib\n", "import io\n", "import base64\n", "\n", "from matplotlib import pyplot as plt\n", "import matplotlib.image as mpimg" ] }, { "cell_type": "code", "execution_count": null, "id": "2946897b", "metadata": {}, "outputs": [], "source": [ "for file in os.listdir('temp'):\n", " os.remove(os.path.join('temp',file))" ] }, { "cell_type": "code", "execution_count": null, "id": "cb57d754", "metadata": {}, "outputs": [], "source": [ "user = \"Georg Brantegger\"\n", "with open('app_pw.txt','r') as f:\n", " app_pw = f.readline()\n", "\n", "base_url = \"https://nextcloud.karnelegger.eu/remote.php/webdav\"\n" ] }, { "cell_type": "code", "execution_count": null, "id": "234e6a70", "metadata": {}, "outputs": [], "source": [ "folders = [\"Forms\",\"6 - Kontaktdatenerhebung RV Villach\",\"Ergebnisse CSV\"]\n", "folder_string = '/'.join([urllib.parse.quote(folder) for folder in folders])\n", "filename_csv = \"Kontaktdatenerhebung RV Villach (Antworten).csv\"\n", "\n", "url_csv = '/'.join([base_url,folder_string,urllib.parse.quote(filename_csv)])\n", "\n", "\n", "r = requests.get(url_csv, auth=(user, app_pw))" ] }, { "cell_type": "code", "execution_count": null, "id": "a74d9129", "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv(io.StringIO(r.text),usecols=[3,4,5,6,7,8],header=0,names=['Vorname','Nachname','rower','Jahrgang','Foto','Telefonnummer'])\n" ] }, { "cell_type": "code", "execution_count": null, "id": "28dbb7e7", "metadata": {}, "outputs": [], "source": [ "df" ] }, { "cell_type": "code", "execution_count": null, "id": "fc8568d6", "metadata": {}, "outputs": [], "source": [ "def extract_tel(row,col):\n", " # remove the first ' and all whitespaces within number\n", "\n", " tel = str(row[col])\n", "\n", " replace_dict = {\"'\":'',' ':''}\n", " \n", " for key, value in replace_dict.items():\n", " tel = tel.replace(key,value)\n", "\n", " # handle people who don't know how country codes work\n", " if tel[0] == '0':\n", " tel = '+43'+tel[1:]\n", " if tel[0] == '6':\n", " tel = '+43'+tel\n", " if tel[0:4] == '+430':\n", " tel = '+43'+tel[4:]\n", " if tel[0:3] == '+49':\n", " tel = '+43'+tel[3:]\n", " if tel[0:2] == '43':\n", " tel = '+43'+tel[2:]\n", " if tel[0:2] == '+6':\n", " tel = '+43'+tel[1:]\n", " \n", " return tel\n", "\n", "def evaluate_rower(row,col='rower'):\n", " if row[col] == 'Ruder:in':\n", " return True\n", " else:\n", " return False" ] }, { "cell_type": "code", "execution_count": null, "id": "68eaead1", "metadata": {}, "outputs": [], "source": [ "df" ] }, { "cell_type": "code", "execution_count": null, "id": "e868df47", "metadata": {}, "outputs": [], "source": [ "df['Vorname'] = df['Vorname'].str.strip()\n", "df['Nachname'] = df['Nachname'].str.strip()\n", "df['rower'] = df.apply(evaluate_rower,axis=1,args=('rower',))\n", "df['Telefonnummer'] = df.apply(extract_tel,axis=1,args=('Telefonnummer',))\n", "df['Foto'] = df['Foto'].str.replace(' ','-').fillna('-')" ] }, { "cell_type": "code", "execution_count": null, "id": "614ac3b3", "metadata": {}, "outputs": [], "source": [ "df" ] }, { "cell_type": "code", "execution_count": null, "id": "df2c876c", "metadata": {}, "outputs": [], "source": [ "def get_photo_for_row(row):\n", "\n", " filename = row['Foto']\n", "\n", " if filename != '-':\n", "\n", " found_photo = False\n", " i = 30\n", " while found_photo is False:\n", " print(f\"trying folder {i}\")\n", " folders = [\"Forms\",\"6 - Kontaktdatenerhebung RV Villach\",f\"{i}\",\"23 - photo_name\"]\n", " folder_string = '/'.join([urllib.parse.quote(folder) for folder in folders])\n", " filename_photo = urllib.parse.quote(filename)\n", "\n", " url_photo = '/'.join([base_url,folder_string,urllib.parse.quote(filename_photo)])\n", "\n", " r = requests.get(url_photo, auth=(user, app_pw))\n", " if r.status_code != 200:\n", " i+=1\n", " if i > 200:\n", " break\n", " continue\n", " else:\n", " photo_bytes = r.content\n", " found_photo = True\n", " photo_b64 = base64.b64encode(photo_bytes).decode(\"utf-8\")\n", " \n", " i = base64.b64decode(photo_b64)\n", " i = io.BytesIO(i)\n", " i = mpimg.imread(i, format='JPG')\n", "\n", " plt.imshow(i, interpolation='nearest')\n", " plt.show()\n", "\n", " return photo_b64\n", " \n" ] }, { "cell_type": "code", "execution_count": null, "id": "8c9db8d6", "metadata": {}, "outputs": [], "source": [ "def convert_rows_to_vcf(row):\n", " # Map CSV columns to vCard fields\n", " first_name = row['Vorname']\n", " last_name = row['Nachname']\n", " phone = row['Telefonnummer']\n", " year = row['Jahrgang']\n", " rower = row['rower']\n", " cat = 'RVV Athlet:innen' if rower is True else 'RVV Eltern'\n", " address_book = 'rv-villach-athletinnen' if rower is True else 'rv-villach-eltern'\n", " photo_b64 = get_photo_for_row(row)\n", "\n", " lines = [\n", " \"BEGIN:VCARD\",\n", " \"VERSION:3.0\",\n", " f\"FN:{last_name} {first_name}\",\n", " f\"N:{last_name};{first_name};;;\",\n", " f\"CATEGORIES:{cat}\",\n", " f\"TEL;TYPE=CELL:{phone}\"]\n", " \n", " if rower is True:\n", " lines.append(f'NOTE:JG {int(year)}')\n", "\n", " if photo_b64:\n", " lines.append(f\"PHOTO;ENCODING=b;TYPE=JPEG:{photo_b64}\")\n", "\n", " lines.append(\"END:VCARD\")\n", "\n", " vcard_content = \"\\n\".join(lines)\n", "\n", " # Save to file\n", " filename = f\"{last_name}_{first_name}.vcf\".replace(\" \", \"_\")\n", " with open(os.path.join('temp', filename), 'w') as f:\n", " f.write(vcard_content)\n", "\n", " # upload file to correct address book\n", "\n", " base_url_contacts = \"https://nextcloud.karnelegger.eu/remote.php/dav/addressbooks/users/Georg%20Brantegger/\"\n", " url = base_url_contacts + address_book + '/' + filename\n", "\n", " with open(f'temp/{filename}', \"rb\") as f:\n", " r = requests.put(\n", " url,\n", " data=f,\n", " auth=(user, app_pw),\n", " headers={\"Content-Type\": \"text/vcard\"}\n", " )\n", "\n", " print(filename, r.status_code)" ] }, { "cell_type": "code", "execution_count": null, "id": "61659f5b", "metadata": {}, "outputs": [], "source": [ "df.apply(convert_rows_to_vcf,axis=1)" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "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.12.3" } }, "nbformat": 4, "nbformat_minor": 5 }