-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchronos.py
More file actions
230 lines (187 loc) · 7.4 KB
/
Copy pathchronos.py
File metadata and controls
230 lines (187 loc) · 7.4 KB
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
import os
import urllib2
import string
import re
from google.appengine.api import urlfetch
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from HTMLParser import HTMLParser
from datetime import datetime, timedelta
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()
def dateICal(date):
return date.strftime("%Y%m%dT%H%M%S")
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
class getICS(webapp.RequestHandler):
def get(self):
url = "http://chronos.epita.net/"
session_id = ""
result = urlfetch.fetch(url)
if result.status_code == 200:
session_id = result.headers['set-cookie']
url = "http://chronos.epita.net/ade/standard/direct_planning.jsp?projectId=3&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)
# 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" % 13
url = "%s?%s&expand=false&forceLoad=false&reload=false" % (tree, branch)
result = urlfetch.fetch(url, headers = { 'cookie': cookie })
branch = "branchId=%i" % 15
url = "%s?%s&expand=false&forceLoad=false&reload=false" % (tree, branch)
result = urlfetch.fetch(url, headers = { 'cookie': cookie })
# Access the leaf
select = "selectId=%i" % 18
url = "%s?%s&forceLoad=false&scroll=0" % (tree, select)
result = urlfetch.fetch(url, headers = { 'cookie': cookie })
# Get the time bar
url = "http://chronos.educplanet.com/ade/custom/modules/plannings/pianoWeeks.jsp"
result = urlfetch.fetch(url, headers = { 'cookie': cookie })
# Set the weeks
bounds = "http://chronos.educplanet.com/ade/custom/modules/plannings/bounds.jsp"
week = "week=%i" % 1
url = "%s?%s&reset=true" % (bounds, week)
result = urlfetch.fetch(url, headers = { 'cookie': cookie })
# week 2
week = "week=%i" % 2
url = "%s?%s&reset=false" % (bounds, week)
result = urlfetch.fetch(url, headers = { 'cookie': cookie })
# week 3
week = "week=%i" % 3
url = "%s?%s&reset=false" % (bounds, week)
result = urlfetch.fetch(url, headers = { 'cookie': cookie })
# week 4
week = "week=%i" % 4
url = "%s?%s&reset=false" % (bounds, week)
result = urlfetch.fetch(url, headers = { 'cookie': cookie })
# Retrieve the content and parse it
info = "http://chronos.educplanet.com/ade/custom/modules/plannings/info.jsp"
url = info
result = urlfetch.fetch(url, headers = { 'cookie': cookie })
parser = infoParser()
parser.feed(result.content)
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': "%s;\n%s" % (DisplaySav51, session_id)
}
path = os.path.join(os.path.dirname(__file__), 'calendar.ics')
self.response.out.write(template.render(path, template_values))
class MainPage(webapp.RequestHandler):
def get(self):
template_values = {
'status' : "no status"}
path = os.path.join(os.path.dirname(__file__), 'view.html')
self.response.out.write(template.render(path, template_values))
application = webapp.WSGIApplication(
[('/', MainPage),
('/gistr.ics', getICS)
],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()