forked from guenter-r/calendar_file_splitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect_contacts_from_ics.py
48 lines (38 loc) · 1.24 KB
/
select_contacts_from_ics.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
## terminal: python select_contacts_from_ics.py <file-name>
import os, re, sys
try:
file = open(sys.argv[1]).read()
except:
print('ERROR: Add a ICS file to your command: python select_contacts_from_ics.py <file-name>')
start_pattern = 'BEGIN:VEVENT'
end_pattern = 'END:VCALENDAR'
start = re.search(start_pattern,file).span()[0]
end = re.search(end_pattern,file).span()[0]
header = file[:start].strip()
footer = '\n' + file[end:]
file = file[start:end]
file = file.replace('END:VEVENT','END:VEVENT&sep')
data = file.split('&sep')
pattern = '(SUMMARY\:(.*?)\n)'
i = 1
new_list = []
for element in data:
if element == '\n':
continue
try:
match = re.search(pattern, element).group(2)
print(match)
except:
print('Unknown pattern: ', element)
print('{} pattern not found - check cal file'.format(i))
i = i+1
print('relevant? y (YES) / (NO) any other key')
user_input = raw_input()
if user_input == 'y':
new_list.append(element)
return_cal = '\n'.join(new_list)
return_cal = return_cal.replace('END:VEVENT\n','END:VEVENT')
return_cal = header+return_cal+footer
with open('new_birthdays.ics','w') as f:
f.write(return_cal)
print('Success, new cal generated: new_birthdays.ics')