-
Notifications
You must be signed in to change notification settings - Fork 0
/
calennar.py
314 lines (273 loc) · 10.7 KB
/
calennar.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/env python
#
# Copyright (c) 2010 Ron Huang
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
from google.appengine.ext import webapp
from google.appengine.api import urlfetch
from google.appengine.ext import deferred
from google.appengine.api import memcache
from google.appengine.runtime import DeadlineExceededError
import logging
import re
from datetime import datetime, timedelta
from models import Seminar, Cache
from icalendar import Calendar, Event
import utils
import chardet
from sgmllib import SGMLParser
import htmlentitydefs
SGT = utils.SGT()
SPLIT_TIME = re.compile("(.*) - (.*)")
STATE_DATE = 1
STATE_TIME = 2
STATE_TITLE = 3
STATE_SPEAKER = 4
class NusCsParser(SGMLParser):
def reset(self):
SGMLParser.reset(self)
self._in_entry = None
self._state = None
self._smnr = None
def unknown_starttag(self, tag, attrs):
if tag == "tr":
self._in_entry = True
self._state = STATE_DATE
self._smnr = {}
elif self._in_entry and tag == "a":
for key, value in attrs:
if key == "href":
self._smnr['url'] = value
break
elif self._in_entry and self._state == STATE_SPEAKER and tag == "br":
if 'speaker' not in self._smnr:
self._smnr['speaker'] = []
self._smnr['speaker'].append("\n")
def unknown_endtag(self, tag):
if self._in_entry and self._state == STATE_SPEAKER and tag == "td":
self._in_entry = None
if "url" not in self._smnr or \
"date" not in self._smnr or \
"time" not in self._smnr or \
"title" not in self._smnr or \
"speaker" not in self._smnr:
# might not be in the correct <tr>
return
# check if already exist
url = self._smnr['url']
exist = memcache.get(url)
if exist:
return
q = Seminar.gql("WHERE url = :url", url=url)
if q.count() > 0:
memcache.add(url, True)
return
# start and end time
date = self._smnr['date']
time = self._smnr['time']
m = SPLIT_TIME.search(time)
if not m:
# might not be in the correct <tr>
return
start = m.group(1)
end = m.group(2)
try:
# convert time
# sample: July 16, 2010 10.00am
start = " ".join([date, start])
start = datetime.strptime(start, "%B %d, %Y %I.%M%p")
start = start.replace(tzinfo=SGT)
end = " ".join([date, end])
end = datetime.strptime(end, "%B %d, %Y %I.%M%p")
end = end.replace(tzinfo=SGT)
except Exception, e:
# might not be in the correct <tr>
logging.error("%s @ %s" % (e, url))
return
# title
title = "".join(self._smnr['title'])
if len(title) > 0:
e = chardet.detect(title)["encoding"]
title = unicode(title, e)
title = utils.unescape(title)
# speaker
sp = self._smnr['speaker']
while len(sp) > 0 and sp[0] == "\n":
sp.pop(0)
speaker = "".join(sp)
if len(speaker) > 0:
e = chardet.detect(speaker)["encoding"]
speaker = unicode(speaker, e)
speaker = utils.unescape(speaker)
# create model
s = Seminar(
start = start,
end = end,
title = title,
speaker = speaker,
url = url,
)
deferred.defer(s.fetch_and_put)
elif self._in_entry and tag == "a":
if self._state == STATE_TITLE:
self._state = STATE_SPEAKER
def handle_data(self, text):
if self._in_entry:
if self._state == STATE_DATE:
self._smnr['date'] = text
self._state = STATE_TIME
elif self._state == STATE_TIME:
self._smnr['time'] = text
self._state = STATE_TITLE
elif self._state == STATE_TITLE:
if 'title' not in self._smnr:
self._smnr['title'] = []
self._smnr['title'].append(text)
else: # rest are STATE_SPEAKER
if 'speaker' not in self._smnr:
self._smnr['speaker'] = []
self._smnr['speaker'].append(text)
def handle_charref(self, ref):
if self._in_entry and (self._state == STATE_TITLE or self._state == STATE_SPEAKER):
# called for each character reference, e.g. for " ", ref will be "160"
# Reconstruct the original character reference.
text = "&#%(ref)s;" % locals()
if self._state == STATE_TITLE:
if 'title' not in self._smnr:
self._smnr['title'] = []
self._smnr['title'].append(text)
elif self._state == STATE_SPEAKER:
if 'speaker' not in self._smnr:
self._smnr['speaker'] = []
self._smnr['speaker'].append(text)
def handle_entityref(self, ref):
if self._in_entry and (self._state == STATE_TITLE or self._state == STATE_SPEAKER):
# called for each entity reference, e.g. for "©", ref will be "copy"
# Reconstruct the original entity reference.
# standard HTML entities are closed with a semicolon; other entities are not
text = None
if htmlentitydefs.entitydefs.has_key(ref):
text = "&%(ref)s;" % locals()
else:
text = "&%(ref)s" % locals()
if self._state == STATE_TITLE:
if 'title' not in self._smnr:
self._smnr['title'] = []
self._smnr['title'].append(text)
elif self._state == STATE_SPEAKER:
if 'speaker' not in self._smnr:
self._smnr['speaker'] = []
self._smnr['speaker'].append(text)
def getUrl(self):
if self._in_entry and "url" in self._smnr:
return self._smnr["url"]
else:
return "N/A"
class UpdateHandler(webapp.RequestHandler):
def get(self):
# Read HTML from seminar page.
url = "https://mysoc.nus.edu.sg/~cmsem/seminar_files/"
result = None
try:
result = urlfetch.fetch(url)
except urlfetch.InvalidURLError, e:
logging.error(e)
self.error(500)
except urlfetch.DownloadError, e:
logging.error(e)
self.error(500)
except urlfetch.ResponseTooLargeError, e:
logging.error(e)
self.error(500)
except:
self.error(500)
if result.status_code != 200:
self.error(result.status_code)
# Parse content of the page.
p = NusCsParser()
try:
p.feed(result.content)
except DeadlineExceededError, e:
logging.warning("Only made to %s" % p.getUrl())
finally:
p.close()
class NusCsHandler(webapp.RequestHandler):
def get(self, year='current'):
"""year can be 'all', 'current', '2002', '2011', etc."""
self.response.headers['Content-Type'] = "text/calendar; charset=utf-8"
# find from cache
q = Cache.gql("WHERE site = :site AND year = :year", site="nuscs", year=year)
c = q.get()
if c:
self.response.out.write(c.data)
return
# generate now
cal = Calendar()
cal.add('prodid', '-//NUS CS Seminars//ronhuang.org//')
cal.add('version', '2.0')
cal.add('X-WR-CALNAME', 'NUS CS Seminars')
cal.add('X-WR-CALDESC', "Seminars are open to the public, and usually held in the School's Seminar Room.")
q = Seminar.all().order('start')
if year != "all":
yn = None
try:
yn = int(year)
except:
pass
if yn:
# return events within that year
q = q.filter("start >=", datetime(yn, 1, 1, tzinfo=SGT))
q = q.filter("start <", datetime(yn + 1, 1, 1, tzinfo=SGT))
else: #default
# return a year of events.
q = q.filter("start >=", datetime.now(SGT) - timedelta(days=366))
for s in q:
event = Event()
event['uid'] = s.url
event.add('summary', s.title)
event.add('dtstart', s.start)
event.add('dtend', s.end)
event.add('dtstamp', s.stamp)
event.add('location', s.venue)
event.add('url', s.url)
event.add('description', s.speaker)
event.add('categories', 'seminar')
event.add('class', 'PUBLIC')
cal.add_component(event)
# generated data
data = cal.as_string()
encoding = chardet.detect(data)['encoding']
data = unicode(data, encoding)
# store in datastore
q = Cache.gql("WHERE site = :site AND year = :year", site="nuscs", year=year)
c = q.get()
if c:
c.data = data
else:
c = Cache(site = "nuscs", data = data, year = year)
c.put()
self.response.out.write(data)
app = webapp.WSGIApplication([
('/calennar/update$', UpdateHandler),
('/calennar/nuscs.ics$', NusCsHandler),
('/calennar/nuscs-(all|current|[0-9]+).ics$', NusCsHandler),
], debug=True)