-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfewo-calendar.py
128 lines (118 loc) · 4.48 KB
/
fewo-calendar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import os
from dotenv import load_dotenv
import pycurl
import certifi
import json
from io import BytesIO
import caldav
from datetime import datetime
from icalendar import Calendar, Event
import uuid
import pytz
# Load .env file
load_dotenv()
# Load environment variables
USERNAME = os.getenv('USERNAME')
PASSWORD = os.getenv('PASSWORD')
TABLESHEADURL = os.getenv('TABLESHEADURL')
TABLESDATAURL = os.getenv('TABLESDATAURL')
CALURL = os.getenv('CALURL')
# Calendar colors
COLORS = {
'Eigenbelegung': 'blue',
'Buchung': 'green',
'Stornierung': 'silver'
}
# Function
# cURL request for getting data from Nextcloud Tables View
# Parameter: URL, Username, Password
def get_tables_data(url, username, password):
buffer = BytesIO()
c = pycurl.Curl()
c.setopt(c.URL, url)
c.setopt(c.USERPWD, '%s:%s' %(username, password))
c.setopt(c.HTTPHEADER, ['Accept: application/json'])
c.setopt(c.WRITEDATA, buffer)
c.setopt(c.CAINFO, certifi.where())
c.perform()
c.close()
body = buffer.getvalue()
return json.loads(body.decode('iso-8859-1'))
# Function
# Extract dates (begin, end) from data
# Parameter: data, id
def get_dates(data, id):
for item in data:
if item['columnId'] == id:
date = item['value']
year, month, day = date.split('-')
caldavDate = datetime(int(year), int(month), int(day), 12, 0, 0, tzinfo=pytz.timezone("Europe/Berlin"))
formattedDate = caldavDate.strftime('%d.%m.%Y')
return caldavDate, formattedDate
# Load data from Nextcloud Tables
head = get_tables_data(TABLESHEADURL, USERNAME, PASSWORD)
data = get_tables_data(TABLESDATAURL, USERNAME, PASSWORD)
# Open Calendar
client = caldav.DAVClient(CALURL, username=USERNAME, password=PASSWORD)
principal = client.principal()
calendars = principal.calendars()
calendar = calendars[0]
# Delete all events in calendar
dateStart = datetime(1900, 1, 1, 0, 0, 0)
dateEnd = datetime(2100, 1, 1, 0, 0, 0)
events = calendar.date_search(start=dateStart, end=dateEnd)
for event in events:
event.delete()
# Create new events and save them
# Parsing throw data
for bookingItem in data:
cal = Calendar()
event = Event()
random_uid = str(uuid.uuid4())
event.add('uid', random_uid)
summary = ''
description = ''
buchungstyp = ''
for i in range(len(head)):
match head[i]['title']:
case 'Anreise':
caldavDate, formattedDate = get_dates(bookingItem['data'], head[i]['id'])
event.add('dtstart', caldavDate)
description += head[i]['title'] + ': ' + formattedDate + '\n'
case 'Abreise':
caldavDate, formattedDate = get_dates(bookingItem['data'], head[i]['id'])
event.add('dtend', caldavDate)
description += head[i]['title'] + ': ' + formattedDate + '\n'
case 'Belegungstyp':
for detailItem in bookingItem['data']:
if detailItem['columnId'] == head[i]['id']:
for value in head[i]['selectionOptions']:
if value['id'] == int(detailItem['value']):
belegungstyp = value['label']
event.add('color', COLORS[belegungstyp])
description += head[i]['title'] + ': ' + belegungstyp + '\n'
case 'Vorname':
for detailItem in bookingItem['data']:
if detailItem['columnId'] == head[i]['id']:
firstname = detailItem['value']
case 'Nachname':
for detailItem in bookingItem['data']:
if detailItem['columnId'] == head[i]['id']:
lastname = detailItem['value']
case 'Buchungstyp':
for detailItem in bookingItem['data']:
if detailItem['columnId'] == head[i]['id']:
for value in head[i]['selectionOptions']:
if value['id'] == int(detailItem['value']):
buchungstyp = value['label']
if belegungstyp == 'Eigenbelegung':
summary = 'Eigenbelegung'
description += 'Eigenbelegung'
else:
summary = firstname + ' ' + lastname
description += 'Gast: ' + firstname + ' ' + lastname + '\n'
description += 'Buchungstyp: ' + buchungstyp
event.add('summary', summary)
event.add('description', description)
cal.add_component(event)
calendar.add_event(cal.to_ical())