-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_ical.py
60 lines (54 loc) · 1.77 KB
/
filter_ical.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
import requests
import urllib.parse
import config
icscontent = requests.get(config.CALENDAR_URL, auth=(config.USER, config.PASSWORD)).text
with open(config.TARGET_ALL, 'w') as alleics:
alleics.write(icscontent)
def prep_links(event):
summary = ''
desc = False
for line in event:
if line.startswith('SUMMARY'):
summary = line[8:]
if line.startswith('DESCRIPTION'):
#check for description field
desc = True
for line in event:
if line.startswith('DESCRIPTION'):
desc = True
url = line[12:]
if url:
line = 'DESCRIPTION:<a href="{}">{}</a>'.format(url, summary)
else:
line = 'DESCRIPTION:<b>{}</b>'.format(summary)
if line.startswith('SUMMARY') and not desc:
#insert description
yield 'DESCRIPTION:<b>{}</b>'.format(summary)
yield line
public_ics = []
in_event = False
events = []
current_event = []
current_is_public = False
current_summary = ''
for line in icscontent.splitlines():
line = line.strip()
if line.startswith('BEGIN:VEVENT'):
in_event = True
if in_event:
current_event.append(line)
else:
public_ics.append(line)
if line.startswith('CLASS:PUBLIC'):
current_is_public = True
if line.startswith('END:VEVENT'):
in_event = False
if current_event and not in_event and current_is_public:
current_event = prep_links(current_event)
public_ics.append('\n'.join(current_event))
current_event = []
current_is_public = False
elif current_event and not in_event and not current_is_public:
current_event = []
with open(config.TARGET_PUBLIC, 'w') as pubics:
pubics.write('\n'.join(public_ics))