-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
119 lines (101 loc) · 4.14 KB
/
main.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
from icalendar import Calendar, Event
from datetime import datetime
from pytz import UTC # timezone
import yaml
import requests
import create_cfg
import os.path
from os import path
#get the file using requests
def get_file(ical_link):
ical_file = requests.get(ical_link)
print("Source calendar downloaded")
cal = open("temp_calendar.ics", 'wb').write(ical_file.content)
cal = open("temp_calendar.ics", 'rb')
return cal
#get the calendar, make new organised calendars
def organise(ical_link, sorting_keywords):
org_cal = get_file(ical_link)
cal = Calendar.from_ical(org_cal.read())
#create calendars for each keyword
i = 0
while i < len(sorting_keywords):
Temp_cal = Calendar()
Temp_cal.add('prodid', '-//Arnouts great calendar//EN')
Temp_cal.add('version', '2.0')
#Temp_cal.add('X-WR-CALNAME', sorting_keywords[i])
for component in cal.walk():
if component.name == "VEVENT":
if "Type: " + sorting_keywords[i] in component.get('description'):
Temp_cal.add_component(create_event(component, i, sorting_keywords[i]))
new_cal = open('new_calendars/' + sorting_keywords[i] + '_cal.ics', 'wb') #create and open the new ical file
new_cal.write(Temp_cal.to_ical()) #write the created calendar to the file
new_cal.close()
print(sorting_keywords[i] + "_cal.ics created")
i +=1
#also create one for misc / remaining events
Misc_cal = Calendar()
Misc_cal.add('prodid', '-//Arnouts great calendar//EN')
Misc_cal.add('version', '2.0')
#Misc_cal.add('X-WR-CALNAME', 'Misc')
for component in cal.walk():
if component.name == "VEVENT":
isMiscEvent = True
i=0
while i < len(sorting_keywords):
if "Type: " + sorting_keywords[i] in component.get('description'):
isMiscEvent = False
i +=1
if isMiscEvent:
desc = component.get('description')
if 'Type: ' in desc:
start = desc.find('Type: ') + 6
end = desc[start:].find('\n') + start
eventType = desc[start:end]
else:
eventType = 'Misc'
Misc_cal.add_component(create_event(component, 10, eventType))
new_cal = open('new_calendars/Misc_cal.ics', 'wb')
new_cal.write(Misc_cal.to_ical())
new_cal.close()
print("Misc_cal.ics created")
org_cal.close()
return 0
#create a new description with only the staff members
def isolate_staff(org_desc):
if 'Staff member(s):' in org_desc:
start = org_desc.find('Staff member(s):')
end = len(org_desc) - start
org_desc = org_desc[start:]
end = org_desc.find('\n')
return org_desc[:end]
#create the new event by copying most properties of original cal, and alter some other
def create_event(component, i, sorting_keyword):
event = Event()
event.add('summary', sorting_keyword + " " + component.get('summary'))
event.add('dtstart', component.get('dtstart'))
event.add('dtend', component.get('dtend'))
event.add('dtstamp', component.get('dtstamp'))
event.add('location', component.get('location'))
event.add('description', isolate_staff(component.get('description')))
event.add('priority', i)
return event
#call the main function to run the organizing of calendar
def main():
#check wether the config file exists, if not make one
if not path.exists("config.yml"):
print("No config file found, lets create one")
if create_cfg.make_cfg():
print("Succesfully created config file.")
#check wether the dir for the new calendars exists
if not os.path.exists("new_calendars"):
os.makedirs("new_calendars")
print("Created new_calendars folder.")
#read the YAML config file
with open("config.yml", 'r') as stream:
config_data = yaml.safe_load(stream)
organise(config_data['Calendar_URL'], config_data['keywords'])
#clean up the temponary downloaded src calendar
os.remove("temp_calendar.ics")
if __name__ == '__main__':
main()