-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
305 lines (239 loc) · 10.7 KB
/
main.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
# Code by Steve Hou in 2022
# DO NOT use the code for other purposes except generating ics file.
import http.cookiejar
import requests
import urllib
import re
import time
import uuid
import json
from datetime import date
from urllib.request import urlopen, Request
# Get one day timetable html
def simulate_login(username, password, year, month, day):
year_str = str(year)
if month < 10:
month_str = '0' + str(month)
else:
month_str = str(month)
if day < 10:
day_str = '0' + str(day)
else:
day_str = str(day)
url = 'https://timetables.liverpool.ac.uk/services/get-events?start=' + year_str + '-' + month_str + '-' + day_str + '&end=' + year_str + '-' + month_str + '-' + day_str
data = {'Username': username,
'Password': password}
post_data = urllib.parse.urlencode(data).encode('utf-8')
headers = {
'User-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'}
login_url = 'https://timetables.liverpool.ac.uk/account?returnUrl=%2F'
req = urllib.request.Request(login_url, headers=headers, data=post_data)
cookie = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookie))
try:
resp = opener.open(req)
except Exception:
print('There might be some problem with your network.')
print('Check you network setting and rerun the program.')
quit()
req = urllib.request.Request(url, headers=headers)
resp = opener.open(req)
result = resp.read().decode('utf-8')
if 'Username:' in result:
result = 'password error'
return result
# Get a student's all timetable, valid until year 2100.
# Hopefully I will update it then.
def get_all_timetable(username, password):
url = 'https://timetables.liverpool.ac.uk/services/get-events?start=2010-01-01&end=2100-12-31'
data = {'Username': username,
'Password': password}
post_data = urllib.parse.urlencode(data).encode('utf-8')
headers = {
'User-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'}
login_url = 'https://timetables.liverpool.ac.uk/account?returnUrl=%2F'
req = urllib.request.Request(login_url, headers=headers, data=post_data)
cookie = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookie))
try:
resp = opener.open(req)
except Exception:
print('There might be some problem with your network.')
print('Check you network setting and rerun the program.')
quit()
req = urllib.request.Request(url, headers=headers)
resp = opener.open(req)
result = resp.read().decode('utf-8')
if 'Username:' in result:
result = 'password error'
return result
# Updated on 2023.01.30
# A Microsoft Duo has been added on the timetable website, so cookie have to be used.
def get_all_timetable_with_cookie(cookie):
headers = {
'Cookie': cookie,
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
}
today = str(date.today())
year = today[0:4]
month = int(today[5:7])
start_month = 0
if month < 9:
# 2nd semester
start_month = '01'
else:
# 1st semester
start_month = '09'
url = 'https://timetables.liverpool.ac.uk/services/get-events?start=' + year + '-' + start_month + '-' + '01&end=2100-12-31'
try:
request = Request(url=url, headers=headers)
respondse = urlopen(request)
return respondse.read().decode()
except Exception:
return 'cookie invalid'
def extract_information(html):
title_list = []
start_list = []
end_list = []
location_list = []
lecturer_list = []
html_json_load = (json.loads(html))
for one_class in html_json_load:
try:
title_list.append(one_class['activitydesc'])
start_list.append(make_dttime(one_class['start']))
end_list.append(make_dttime(one_class['end']))
location_list.append(one_class['locationdesc'])
try:
lecturer_info_json = one_class['staffs']
lecturer_info_formatted = 'Lecturer: ' + lecturer_info_json[0]['FullName'] + '\\r\\nEmail: ' + \
lecturer_info_json[
0]['Email'] + '\\r\\nPhone number:' + lecturer_info_json[0][
'PhoneNumber']
lecturer_list.append(lecturer_info_formatted)
except IndexError:
lecturer_list.append('')
except Exception:
print(
'An error occurred on one class. The class might near ' + str(
start_list[-1]) + ' . Check it with you timetable.')
continue
list_list = [title_list, start_list, end_list, location_list, lecturer_list]
return list_list
def make_dttime(time):
match_obj = re.match('(....)-(..)-(..)T(..):(..)', time)
dttime = match_obj.group(1) + match_obj.group(2) + match_obj.group(3) + 'T' + match_obj.group(
4) + match_obj.group(
5) + '00'
return dttime
def format_ics(list_list):
module_num = len(list_list[0])
for title_i in range(0, module_num):
list_list[0][title_i] = 'SUMMARY;LANGUAGE=en-us:' + list_list[0][title_i]
for start_i in range(0, module_num):
list_list[1][start_i] = 'DTSTART;VALUE=DATE-TIME:' + list_list[1][start_i]
for end_i in range(0, module_num):
list_list[2][end_i] = 'DTEND;VALUE=DATE-TIME:' + list_list[2][end_i]
for location_i in range(0, module_num):
list_list[3][location_i] = 'LOCATION:' + list_list[3][location_i]
for lecturer_i in range(0, module_num):
list_list[4][lecturer_i] = 'DESCRIPTION;ENCODING=QUOTED-PRINTABLE:' + list_list[4][lecturer_i]
return list_list
def request_user_info():
print('This program need your account username and password to run.')
username = input('Please input your UoL account username : ')
password = input('Please input your UoL account password : ')
if get_all_timetable(username, password) == 'password error':
print('Your username or password is incorrect.')
return request_user_info()
info_list = [username, password]
return info_list
def request_user_cookie():
print('This program need your cookie to run.')
cookie = input('Please input your timetable website cookie:')
if get_all_timetable_with_cookie(cookie) == 'cookie invalid':
print('Your cookie is invalid.')
return request_user_cookie()
return cookie
def write_ics(username, password):
with open('timetable.ics', 'w') as ics:
head = 'BEGIN:VCALENDAR\nPRODID:Calendar-//Steve Hou//auto-generate-ics//EN\nVERSION:2.0\n'
ics.write(head)
info_list = request_user_info()
username = info_list[0]
password = info_list[1]
print('Connecting to university server...')
if get_all_timetable(username, password) == 'password error':
print('Your username or password is incorrect.')
write_ics(username, password)
else:
print('Connection success. Starting generate ics file. \nIt may take few seconds.')
try:
html = get_all_timetable(username, password)
list_list = format_ics(extract_information(html))
module_num = len(list_list[0])
for i in range(0, module_num):
with open('timetable.ics', 'a') as ics:
ics.write('BEGIN:VEVENT\n')
ics.write('CLASS:PUBLIC\n')
ics.write('DESCRIPTION:\n')
ics.write('DTSTAMP;VALUE=DATE-TIME:20220201T111819\n')
ics.write('UID:' + str(uuid.uuid1()) + '\n')
for list in list_list:
with open('timetable.ics', 'a') as ics:
ics.write(list[i] + '\n')
with open('timetable.ics', 'a') as ics:
ics.write('TRANSP:TRANSPARENT\n')
ics.write('END:VEVENT\n')
except Exception as e:
print()
print('*************************************************************************')
print('********** An error occurs. Auto rebooting the progress... **********')
print('*************************************************************************')
print()
write_ics(username, password)
with open('timetable.ics', 'a') as ics:
ics.write('END:VCALENDAR')
def write_ics_with_cookie(cookie):
with open('timetable.ics', 'w') as ics:
head = 'BEGIN:VCALENDAR\nPRODID:Calendar-//Steve Hou//auto-generate-ics//EN\nVERSION:2.0\n'
ics.write(head)
cookie = request_user_cookie()
print('Connecting to university server...')
if get_all_timetable_with_cookie(cookie) == 'cookie invalid':
print('Your cookie is invalid.')
write_ics_with_cookie(cookie)
else:
print('Connection success. Starting generate ics file. \nIt may take few seconds.')
try:
html = get_all_timetable_with_cookie(cookie)
list_list = format_ics(extract_information(html))
module_num = len(list_list[0])
for i in range(0, module_num):
with open('timetable.ics', 'a') as ics:
ics.write('BEGIN:VEVENT\n')
ics.write('CLASS:PUBLIC\n')
ics.write('DESCRIPTION:\n')
ics.write('DTSTAMP;VALUE=DATE-TIME:20220201T111819\n')
ics.write('UID:' + str(uuid.uuid1()) + '\n')
for list in list_list:
with open('timetable.ics', 'a') as ics:
ics.write(list[i] + '\n')
with open('timetable.ics', 'a') as ics:
ics.write('TRANSP:TRANSPARENT\n')
ics.write('END:VEVENT\n')
with open('timetable.ics', 'a') as ics:
ics.write('END:VCALENDAR')
except Exception as e:
print()
print(e)
print('*************************************************************************')
print('********** An error occurs. Auto rebooting the progress... **********')
print('*************************************************************************')
print()
write_ics_with_cookie(cookie)
def main():
write_ics_with_cookie('')
print('Finished. Please find your timetable file named "timetable.ics".')
if __name__ == '__main__':
main()