Files
Python-RV/Nextcloud Contact Infos/convert_trainer.ipynb

221 lines
6.2 KiB
Plaintext

{
"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",
"\n",
"base_url_csv = \"https://nextcloud.karnelegger.eu/remote.php/webdav\"\n",
"folders = [\"Forms\",\"4 - Kontaktdatenerhebung Trainerteam\",\"Ergebnisse CSV\"]\n",
"folder_string = '/'.join([urllib.parse.quote(folder) for folder in folders])\n",
"filename_csv = \"Kontaktdatenerhebung Trainerteam (Antworten).csv\"\n",
"\n",
"url_csv = '/'.join([base_url_csv,folder_string,urllib.parse.quote(filename_csv)])\n",
"\n",
"\n",
"r = requests.get(url_csv, auth=(user, app_pw))\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a74d9129",
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv(io.StringIO(r.text))\n",
"\n",
"df[['Nachname','Vorname']] = df.apply(lambda row: row['Nachname Vorname'].strip().split(' ',maxsplit=1),axis=1,result_type='expand')\n",
"df = df.copy()\n",
"df['Nachname'] = df['Nachname'].replace({'Anton': 'Pfurtscheller','David': 'Kobau'})\n",
"df['Vorname'] = df['Vorname'].replace({'Franz Pfurtscheller': 'Anton Franz', 'Kobau': 'David'})\n",
"df['Telefonnummer'] = df['Telefonnummer'].str.replace(\"'\",'').str.replace('06','+436',n=1)\n",
"df['Foto'] = df['Foto'].str.replace(' ','-')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fc8568d6",
"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",
" found_photo = False\n",
" i = 0\n",
" while found_photo is False:\n",
" print(f\"trying folder {i}\")\n",
" base_url_photo = \"https://nextcloud.karnelegger.eu/remote.php/webdav\"\n",
" folders = [\"Forms\",\"4 - Kontaktdatenerhebung Trainerteam\",f\"{i}\",\"19 - Foto\"]\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_photo,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 > 100:\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",
" org = 'Ruderverein Villach von 1881'\n",
" photo_b64 = get_photo_for_row(row)\n",
" \n",
"\n",
" lines = [\n",
" \"BEGIN:VCARD\",\n",
" \"VERSION:3.0\",\n",
" f\"FN:{first_name} {last_name}\",\n",
" f\"N:{last_name};{first_name};;;\",\n",
" f\"ORG:{org}\",\n",
" f\"TEL;TYPE=CELL:{phone}\"]\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"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "61659f5b",
"metadata": {},
"outputs": [],
"source": [
"df.apply(convert_rows_to_vcf,axis=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "37b2a51b",
"metadata": {},
"outputs": [],
"source": [
"base_url = \"https://nextcloud.karnelegger.eu/remote.php/dav/addressbooks/users/Georg%20Brantegger/rv-villach-trainer/\"\n",
"\n",
"for vcf_file in os.listdir('temp'):\n",
"\n",
" url = base_url + vcf_file\n",
"\n",
" with open(f'temp/{vcf_file}', \"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(vcf_file, r.status_code)"
]
}
],
"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
}