-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcalendar.fetch
executable file
·84 lines (61 loc) · 2.07 KB
/
calendar.fetch
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
#!/usr/bin/env python3
import sys
import os
import traceback
import urllib.request
import urllib.error
import arrow.parser
import ics
import ics.grammar.parse
from events import log
from events import CACHE
from events import calendars
from events import url_hash
from events import dropalarms
try:
os.makedirs(CACHE.format(''))
except:
pass
for _calendar in calendars():
url = _calendar['url']
try:
log('downloading {}'.format(url))
req = urllib.request.Request(
url,
data=None,
headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
}
)
stream = urllib.request.urlopen(req)
# to check that we can parse the data
body = stream.read()
text = body.decode()
# use workaround for nonrfc valarm blocks (see https://github.com/C4ptainCrunch/ics.py/issues/127)
text = dropalarms(text)
calendar = ics.Calendar(text)
h = url_hash(url)
filename = CACHE.format(h)
log('writing {} to {}'.format(url, filename))
with open(filename, 'w') as fd:
fd.write(text)
except urllib.error.URLError as err:
log('Error opening url: {}'.format(url))
log(err)
traceback.print_tb(err.__traceback__, file=sys.stderr)
except ics.grammar.parse.ParseError as err:
log('Error parsing file: {}'.format(url))
log(err)
traceback.print_tb(err.__traceback__, file=sys.stderr)
except arrow.parser.ParserError as err:
log('Error parsing time for {}'.format(url))
log(err)
traceback.print_tb(err.__traceback__, file=sys.stderr)
except FileNotFoundError as err:
log('Error writing file for {}'.format(url))
log(err)
traceback.print_tb(err.__traceback__, file=sys.stderr)
except Exception as err:
log('Unknown exception for {}'.format(url))
log(err)
traceback.print_tb(err.__traceback__, file=sys.stderr)