forked from goweiwen/ivle-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathivle-sync.py
executable file
·267 lines (209 loc) · 7.85 KB
/
ivle-sync.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
#! /usr/bin/env python3
from bs4 import BeautifulSoup
from getpass import getpass
from os import makedirs
from sys import argv, exit
from os.path import join, dirname, isfile, realpath
import re
import requests
import json
# Fill up ./credentials.json with your LAPI key
# http://ivle.nus.edu.sg/LAPI/default.aspx
with open(
join(dirname(realpath(__file__)), 'credentials.json'),
encoding='utf-8') as file:
credentials = json.loads(file.read())
USER_AGENT = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36"
class Module:
def __init__(self, moduleId, name, code):
self.id = moduleId
self.name = name
self.code = code
class WorkbinFolder:
def __init__(self, folderJson, path=""):
self.name = folderJson["FolderName"]
self.id = folderJson["ID"]
self.path = join(path, self.name)
self.folders = []
for fileJson in folderJson["Folders"]:
self.folders.append(WorkbinFolder(fileJson, self.path))
self.files = []
for fileJson in folderJson["Files"]:
self.files.append(WorkbinFile(fileJson, self.path))
def printPath(self):
print(self.path)
for folder in self.folders:
folder.printPath()
for file in self.files:
print(file.path)
def print(self, indent=0):
print(" " * indent + self.name + "/")
for folder in self.folders:
folder.print(indent + 1)
for file in self.files:
print(" " * (indent + 1) + file.name)
class WorkbinFile:
def __init__(self, fileJson, path=""):
self.name = fileJson["FileName"]
self.id = fileJson["ID"]
self.path = join(path, self.name)
class IVLESession:
def __init__(self, userid, password):
self.userid = userid
self.password = password
self.s = requests.Session()
self.s.headers.update({"User-Agent": USER_AGENT})
self.token = self.get_token()
if self.token == '':
print("Login fail, please check your userid and password")
def get_token(self):
r = self.s.get("https://ivle.nus.edu.sg/api/login/?apikey=" +
credentials['LAPI_KEY'])
soup = BeautifulSoup(r.content, "html.parser")
VIEWSTATE = soup.find(id="__VIEWSTATE")['value']
VIEWSTATEGENERATOR = soup.find(id="__VIEWSTATEGENERATOR")['value']
data = {
"__VIEWSTATE": VIEWSTATE,
"__VIEWSTATEGENERATOR": VIEWSTATEGENERATOR,
"userid": self.userid,
"password": self.password
}
r = self.s.post("https://ivle.nus.edu.sg/api/login/?apikey=" +
credentials['LAPI_KEY'], data)
if len(r.text) > 1000: # hacky way to check if return is a HTML page
return ''
return r.text
def get_modules(self):
result = self.lapi("Modules")
modules = []
for module in result["Results"]:
modules.append(
Module(module["ID"], module["CourseName"], module[
"CourseCode"]))
return modules
def get_workbin(self, module):
result = self.lapi("Workbins", {"CourseID": module.id})
folders = []
for workbin in result["Results"]:
for folder in workbin["Folders"]:
folders.append(WorkbinFolder(folder, module.code))
return folders
def lapi(self, method, params={}):
params["APIKey"] = credentials['LAPI_KEY']
params["AuthToken"] = self.token
return self.s.get(
"https://ivle.nus.edu.sg/api/Lapi.svc/" + method,
params=params).json()
def download_file(self, file, base_dir):
params = {
"APIKey": credentials['LAPI_KEY'],
"AuthToken": self.token,
"ID": file.id,
"target": "workbin"
}
file.path = base_dir + file.path
makedirs(dirname(file.path), exist_ok=True)
if isfile(file.path):
# print("Skipping " + file.path + ".")
return
print("Downloading " + file.path + ".")
r = self.s.get(
"https://ivle.nus.edu.sg/api/downloadfile.ashx",
stream=True,
params=params)
try:
with open(file.path, 'wb') as f:
for chunk in r.iter_content(1024):
f.write(chunk)
except:
os.remove(file.path)
def download_folder(self, target_folder, base_dir):
for folder in target_folder.folders:
self.download_folder(folder, base_dir)
for file in target_folder.files:
self.download_file(file, base_dir)
def sync_files(session, base_dir):
modules = session.get_modules()
for module in modules:
folders = session.get_workbin(module)
for folder in folders:
session.download_folder(folder, base_dir)
def sync_announcements(session):
modules = session.get_modules()
DURATION = 60 * 24 * 5
for module in modules:
announcements = session.lapi(
"Announcements", {"CourseID": module.id,
"Duration": DURATION})
for announcement in announcements["Results"]:
print("\n\n\n")
print("=== " + announcement["Title"] + " ===")
description = BeautifulSoup(announcement["Description"],
"html.parser").get_text()
description = re.sub(r'\n\s*\n', '\n', description)
print(description)
input()
def get_credentials():
userid = credentials['USERID']
if userid == '':
userid = input("UserID: ")
password = credentials['PASSWORD']
if password == '':
password = getpass("Password: ")
return (userid, password)
def collect(argv):
args = {}
while argv:
if argv[0][0] == '-':
args[argv[0]] = argv[1]
argv = argv[1:]
return args
def main():
if credentials['LAPI_KEY'] == '':
print("Fill up ./credentials.json with your LAPI key")
print("http://ivle.nus.edu.sg/LAPI/default.aspx")
exit(1)
args = collect(argv)
base_dir = ""
try:
if '-d' in args:
base_dir = args['-d'];
if not base_dir.endswith('/'):
base_dir = base_dir + '/'
print("-d found, placing file in: " + base_dir + "<course code>")
if '-f' in args:
userid, password = get_credentials()
session = IVLESession(userid, password)
if session.token != '':
sync_files(session, base_dir)
if '-a' in args:
userid, password = get_credentials()
session = IVLESession(userid, password)
if session.token != '':
sync_announcements(session)
# try:
# if len(argv) > 1:
# if argv[1] == "files" or argv[1] == "f":
# userid, password = get_credentials()
# session = IVLESession(userid, password)
# if session.token != '':
# sync_files(session)
# elif argv[1] == "announcements" or argv[1] == "a":
# userid, password = get_credentials()
# session = IVLESession(userid, password)
# if session.token != '':
# sync_announcements(session)
# exit(1)
except (requests.exceptions.RequestException):
print("Error: Connection refused.")
exit(-1)
except (KeyboardInterrupt, SystemExit):
print("Aborting...")
exit(-1)
# print("Usage: " + argv[0] + " [files|announcements]")
print("Usage: " + argv[0] )
print(" -f for files. ")
print(" -a for announcements. ")
print(" -d <directory> to output files to a different directory. ")
if __name__ == "__main__":
main()