-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
222 lines (167 loc) · 6.79 KB
/
parser.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
#!/usr/bin/python3
# Author: Evert Heylen
# License: WTFPL
# Depencies: selenium (and python bindings), python-icalendar and firefox.
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
from icalendar import Calendar, Event
from getpass import getpass
from datetime import *
from time import sleep
import re
# ----[ Settings ]------------------------------------------
user = input("User: ")
password = getpass()
filename = input("Filename (without extension)? ") + ".ics"
room_row = 5 # M.G.010
building = "CMI - gebouw G"
# ATTENTION: only specify the first day of a month here
start = date(2016,9,1)
end = date(2017,1,1)
# ----------------------------------------------------------
_ua_5000 = date(2013,9,9) # do not change
start_num = (start - _ua_5000).days + 5000
end_num = (end - _ua_5000).days + 5000
#print(start_num)
#print(end_num)
def main():
# initialize the calendar
cal = Calendar()
cal.add("summary", "Generated by lokaal-parser, made by Evert Heylen.")
cal.add('prodid', '-//lokaal parser//evertheylen.appspot.com//')
cal.add('version', '2.0')
# login prompt
br = webdriver.Chrome()
br.implicitly_wait(2) # ...
br.get("https://www.ua.ac.be/login/login.aspx?url=www.ua.ac.be&c=.LOKAALRESERVATIE&n=36899")
br.find_element_by_id("TextBox1").send_keys(user)
br.find_element_by_id("TextBox2").send_keys(password)
br.find_element_by_name("Button1").click()
# open 'lokalengebruik'
br.get("http://www.ua.ac.be/main.aspx?c=.LOKAALRESERVATIE&n=43599&ct=43846")
select = Select(br.find_element_by_name("ctl18$ddlGebouwen"))
select.select_by_visible_text(building)
# navigate to first month
current_first = first_day(br)
while current_first != start:
print("current_first", current_first, "start", start)
if current_first < start:
next_month(br)
else:
prev_month(br)
sleep(1)
current_first = first_day(br)
print("Currently at the right month.")
current_day = start
days = get_days(br)
days[current_day].click()
days = get_days(br)
while current_day != end:
# have to redo this every loop, because of the DOM being reconstructed etc...
days = get_days(br)
# ------- do something with current_day, actual parsing is here ---------
print('parse',current_day)
"""
//*[@id="ctl18_lblBeforeBody"]/table/tbody/tr[2]/td/table/tbody/tr[1]/td/p[3]/table/tbody/tr[5]/td[2]/img
...
//*[@id="ctl18_lblBeforeBody"]/table/tbody/tr[2]/td/table/tbody/tr[1]/td/p[3]/table/tbody/tr[5]/td[61]/img
"""
# current also contains the time
current = datetime(current_day.year, current_day.month, current_day.day, 7, 0, 0)
event = None
#event = Event() # ignore the first event
#event.add('dtstart', start)
#event.add('summary', 'Ignore me!')
for i in range(2,62):
cell = br.find_element_by_xpath('//*[@id="ctl18_lblBeforeBody"]/table/tbody/tr[2]/td/table/tbody/tr[1]/td/p[3]/table/tbody/tr[5]/td[{0}]/img'.format(i))
taken = cell.get_attribute("src") == "http://www.ua.ac.be/plugins/ua/context/cde/images/bezet.gif"
summ = ''
close = False
new = False
if taken:
summ = parse_summ(cell.get_attribute("onclick"))
if (not event is None) and str(event["SUMMARY"]) != summ:
# close old event, make new event
close = True
new = True
elif event is None:
# make new event
new = True
elif not event is None:
# close old event
close = True
# do the actions chosen
if close:
# end time
event.add('dtend', current-timedelta(minutes=15)) # possible bug. I don't care.
cal.add_component(event)
#print("added {}, from {} to {}".format(str(event['SUMMARY']), str(event['DTSTART']), str(event['DTEND'])))
event = None
if new:
event = Event()
# start time
event.add('dtstart', current)
event.add('summary', summ)
# next current
current += timedelta(minutes=15)
# -------
current_day += timedelta(days=1)
if current_day in days:
days[current_day].click() # next day
else:
# go to next month
next_month(br)
sleep(1)
days = get_days(br)
days[current_day].click()
br.close()
# save calendar
f = open(filename, 'wb')
f.write(cal.to_ical())
f.close()
print("Written file to %s"%filename)
print("All done!")
def num_to_date(num):
return _ua_5000 + timedelta(days=(num-5000))
def next_month(br):
br.find_element_by_xpath('//*[@id="ctl18_Calendar1"]/tbody/tr[1]/td/table/tbody/tr/td[3]/a').click()
def prev_month(br):
br.find_element_by_xpath('//*[@id="ctl18_Calendar1"]/tbody/tr[1]/td/table/tbody/tr/td[1]/a').click()
def first_day(br):
for row in range(3,9):
for col in range(1,8):
el = br.find_element_by_xpath('//*[@id="ctl18_Calendar1"]/tbody/tr[{0}]/td[{1}]/a'.format(row, col))
if "darkgray" not in el.get_attribute("style"):
return num_to_date(parse_num(el.get_attribute("href")))
def get_days(br):
days = {}
for row in range(3,9):
for col in range(1,8):
el = br.find_element_by_xpath('//*[@id="ctl18_Calendar1"]/tbody/tr[{0}]/td[{1}]/a'.format(row, col))
if "darkgray" not in el.get_attribute("style"):
num = parse_num(el.get_attribute("href"))
date = num_to_date(num)
days[date] = el
return days
"""
//*[@id="ctl18_Calendar1"]/tbody/tr[3]/td[1]/a
...
//*[@id="ctl18_Calendar1"]/tbody/tr[3]/td[7]/a
.....
.....
//*[@id="ctl18_Calendar1"]/tbody/tr[8]/td[1]/a
...
//*[@id="ctl18_Calendar1"]/tbody/tr[8]/td[7]/a
"""
_parse_num = re.compile(r"javascript:__doPostBack\('ctl18\$Calendar1','([0-9]*)'\)")
def parse_num(s):
return int(_parse_num.sub(r"\1", s))
def parse_summ(s):
return s[7:-3].replace("\\n", "\n")
def diff_months(a, b):
return (b.year - a.year)*12 + (b.month - a.month)
if __name__ == '__main__':
main()