forked from sheep/Chronos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chronos.py
359 lines (292 loc) · 10.9 KB
/
chronos.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import os
import urllib2
import string
import re
import logging
from google.appengine.api import urlfetch
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext.webapp.util import run_wsgi_app
from HTMLParser import HTMLParser
from datetime import datetime, timedelta
## Class to hold the calendar and users in the DB
class Calendar(db.Model):
ICS = db.TextProperty()
dt = db.DateTimeProperty(auto_now=True)
def date(self):
return (self.dt + timedelta(hours = 2)).ctime()
class UserPref(db.Model):
user = db.UserProperty()
uid = db.StringProperty()
cal = db.ReferenceProperty()
query = db.StringProperty()
nbw = db.IntegerProperty()
@staticmethod
def getdata(u):
q = db.GqlQuery("SELECT * FROM UserPref WHERE user = :1", u)
return q.get()
## Parser for the data
class infoParser(HTMLParser):
def __init__(self):
self.result = []
self.nbrow = 0
self.active = 0
self.finished = 0
self.skipping=0
self.current_row = []
self.current_data = []
HTMLParser.__init__(self)
def start_table(self, attributes):
# print "begin table"
if not self.finished:
self.active=1
def end_table(self):
# print "end table"
self.active=0
self.finished=1
def start_tr(self,attributes):
# print " begin tr"
if self.active and not self.skipping:
self.current_row = []
def end_tr(self):
# print " end tr"
if self.active and not self.skipping:
self.result.append(self.current_row)
def start_td(self,attributes):
# print " begin td"
if self.active and not self.skipping:
self.current_data = []
def end_td(self):
# print " end td"
if self.active and not self.skipping:
self.current_row.append(
string.join(self.current_data))
def handle_data(self, data):
if self.active and not self.skipping:
# print " datafound:"
# print data
# print " end of data"
self.current_data.append(data)
def handle_starttag(self, tag, attrs):
# print "Encountered the beginning of a %s tag" % tag
if tag == "table":
self.start_table(attrs)
elif tag == "tr":
self.start_tr(attrs)
elif tag == "td":
self.start_td(attrs)
def handle_endtag(self, tag):
# print "Encountered the end of a %s tag" % tag
if tag == "table":
self.end_table()
elif tag == "tr":
self.end_tr()
elif tag == "td":
self.end_td()
## Parser to get the number of the current week
class weekParser(HTMLParser):
def __init__(self):
self.pianoSelected = False
self.nweek = 1
HTMLParser.__init__(self)
def handle_starttag(self, tag, attrs):
if tag == "div" and len(attrs) > 0 and attrs[0][1] == "pianoselected":
self.pianoSelected = True
elif tag == "map" and len(attrs) > 0 and self.pianoSelected:
self.nweek = int(re.sub("[\D]","",attrs[0][1]))
def handle_endtag(self, tag):
if tag == "div" and self.pianoSelected:
self.pianoSelected = False
def dateICal(date):
return date.strftime("%Y%m%dT%H%M%S")
## Build the ICS
def make_event_list(parsed):
events = []
for i in parsed:
if len(i) < 7:
continue
start = datetime.strptime("%s %s" % (i[0], i[1]), "%d/%m/%Y %Hh%M")
if re.match("^\d{1,2}h$", i[2]):
delta = datetime.strptime(i[2], "%Hh")
else: # /2h30min/
delta = datetime.strptime(i[2], "%Hh%Mmin")
end = start + timedelta(hours = delta.hour, minutes = delta.minute)
event = {"groups": i[4],
"prof": i[5],
"room": i[6],
"name": i[3],
"start": dateICal(start),
"end": dateICal(end)
}
event_condensed_name = "%s-%s" % (event["name"], event["prof"])
event_condensed_name = re.sub('[^\w]','_', event_condensed_name)
event["uid"] = "%s-%s-%s" % (event["groups"], event["start"], event_condensed_name)
events.append(event)
# print ""
# print "UID:chronos-%s-%s-%s" % (event["groups"], dateICal(event["start"]), event_condensed_name)
# print "DTSTAMP:%s" % dateICal(datetime.now())
# print "SUMMARY:%s - %s %s" % (event["name"], event["prof"], event["room"])
# print "DESCRIPTION:Cours: %s\\nProf: %s\\nSalle: %s\\nGroupes: %s" % (event["name"], event["prof"], event["room"], event["groups"])
# print "DTSTART:%s" % dateICal(event["start"])
# print "DTEND:%s" % dateICal(event["end"])
# print "LOCATION:%s" % event["room"]
return events
## Fetch the data from Chronos
def getICS(cal, query, nbw):
url = "http://chronos.epita.net/"
url = "http://chronos.epita.net/ade/standard/gui/interface.jsp?projectId=4&login=student&password="
session_id = ""
result = urlfetch.fetch(url)
if result.status_code == 200:
session_id = result.headers['set-cookie']
url = "http://chronos.epita.net/ade/standard/gui/interface.jsp?projectId=4&login=student&password="
result = urlfetch.fetch(url, headers = { 'Cookie': session_id })
#DisplaySav51 = result.headers['set-cookie']
tree = "http://chronos.epita.net/ade/standard/gui/tree.jsp"
#cookie = "%s; %s" % (DisplaySav51, session_id)
cookie = session_id
# Find the leaf following the given path
"""categorie = "category=%s" % "trainee"
url = "%s?%s&expand=false&forceLoad=false&reload=false" % (tree, categorie)
result = urlfetch.fetch(url, headers = { 'cookie': cookie })
index_epita = 1
branch = "branchId=%i" % index_epita
url = "%s?%s&expand=false&forceLoad=false&reload=false" % (tree, branch)
result = urlfetch.fetch(url, headers = { 'cookie': cookie })
branch = "branchId=%i" % 748
url = "%s?%s&expand=false&forceLoad=false&reload=false" % (tree, branch)
result = urlfetch.fetch(url, headers = { 'cookie': cookie })
branch = "branchId=%i" % 989
url = "%s?%s&expand=false&forceLoad=false&reload=false" % (tree, branch)
result = urlfetch.fetch(url, headers = { 'cookie': cookie })
# Access the leaf
select = "selectId=%i" % 1089
url = "%s?%s&forceLoad=false&scroll=0" % (tree, select)
result = urlfetch.fetch(url, headers = { 'cookie': cookie })"""
url = "%s?nbFilters=1&what0=name&how0=contains&filterValue0=%s" % (tree, query)
result = urlfetch.fetch(url, headers = { 'cookie': cookie })
# Get the time bar
url = "http://chronos.epita.net/ade/custom/modules/plannings/pianoWeeks.jsp"
result = urlfetch.fetch(url, headers = { 'cookie': cookie })
parser = weekParser()
parser.feed(result.content)
parser.close()
nweek = parser.nweek - 1
# Set the weeks
bounds = "http://chronos.epita.net/ade/custom/modules/plannings/bounds.jsp"
## Obtain next 8 weeks of the calendar
week = "week=%i" % nweek
url = "%s?%s&reset=true" % (bounds, week)
result = urlfetch.fetch(url, headers = { 'cookie': cookie })
for i in range(1, nbw - 1):
week = "week=%i" % (nweek + i)
url = "%s?%s&reset=false" % (bounds, week)
result = urlfetch.fetch(url, headers = { 'cookie': cookie })
# Retrieve the content and parse it
info = "http://chronos.epita.net/ade/custom/modules/plannings/info.jsp"
url = info
result = urlfetch.fetch(url, headers = { 'cookie': cookie })
parser = infoParser()
parser.feed(unicode(result.content, "utf-8", "ignore"))
parser.close()
# result:
# parser.result[*][0]: start date
# after: start hour, length, cours, groups, professor, room
events = make_event_list(parser.result)
template_values = {
'status': result.status_code,
'header': result.headers,
'content': string.replace(result.content,'<','_'),
'parseres': parser.result,
'events': events,
'stamp': dateICal(datetime.now()),
'id': session_id
}
path = os.path.join(os.path.dirname(__file__), 'calendar.ics')
cal.ICS = template.render(path, template_values)
cal.put()
class GetAll(webapp.RequestHandler):
def get(self):
ups = UserPref.all()
for up in ups:
getICS(up.cal, up.query, up.nbw)
## ics of user_id
class ShowICS(webapp.RequestHandler):
def get(self):
uid = self.request.get('uid')
if uid:
q = db.GqlQuery("SELECT * FROM UserPref WHERE uid = :1", uid)
user = q.get()
self.response.out.write(user.cal.ICS)
else:
self.redirect("/")
## Register ##
class Register(webapp.RequestHandler):
def get(self):
user = users.get_current_user()
query = self.request.get('query')
nbw = self.request.get('nbw')
ud = UserPref.getdata(user);
if (query == "" or nbw == "") and not ud:
self.response.out.write("""
<html>
<body>
<h2>Register</h2>
<form action="/user/register" method="get">
<div>Search query <input type="text" name="query" size="10"></input></div>
<div>Number of weeks <input type="text" name="nbw" size="3"></input></div>
<div><input type="submit" value="Register"></div>
</form>
</body>
</html>""")
else:
if not ud:
up = UserPref()
up.user = user
up.uid = user.user_id()
up.query = query
up.nbw = int(nbw)
cal = Calendar()
getICS(cal, up.query, up.nbw)
up.cal = cal
up.put()
self.redirect("/")
else:
self.redirect("/")
## Refresh
class Refresh(webapp.RequestHandler):
def get(self):
user = users.get_current_user()
ud = UserPref.getdata(user)
if ud:
getICS(ud.cal, ud.query, ud.nbw)
self.redirect("/")
else:
self.redirect("/user/register")
## MainPage
class MainPage(webapp.RequestHandler):
def get(self):
user = users.get_current_user()
ud = UserPref.getdata(user)
if ud:
template_values = {
'email': user.email(),
'updated' : ud.cal.date(),
'uid' : user.user_id()
}
path = os.path.join(os.path.dirname(__file__), 'view.html')
self.response.out.write(template.render(path, template_values))
else:
self.redirect("/user/register")
application = webapp.WSGIApplication([('/', MainPage),
('/ics', ShowICS),
('/update', GetAll),
('/user/register', Register),
('/user/refresh', Refresh)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()