-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.ts
733 lines (665 loc) · 23.8 KB
/
index.ts
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
import * as cheerio from 'cheerio';
import type * as DOM from 'domhandler';
import { Base64 } from 'js-base64';
import makeFetch from 'node-fetch-cookie-native';
import * as URL from './urls';
import {
CredentialProvider,
Fetch,
HelperConfig,
Content,
ContentType,
CourseContent,
CourseInfo,
Discussion,
FailReason,
File,
Homework,
IDiscussionBase,
IHomeworkDetail,
IHomeworkStatus,
INotification,
INotificationDetail,
Notification,
Question,
SemesterInfo,
CourseType,
CalendarEvent,
ApiError,
RemoteFile,
IHomeworkSubmitAttachment,
IHomeworkSubmitResult,
Language,
HomeworkTA,
UserInfo,
} from './types';
import {
decodeHTML,
parseSemesterType,
trimAndDefine,
JSONP_EXTRACTOR_NAME,
extractJSONPResult,
GRADE_LEVEL_MAP,
} from './utils';
const CHEERIO_CONFIG: cheerio.CheerioOptions = {
// _useHtmlParser2
xml: true,
decodeEntities: false,
};
const $ = (html: string | DOM.Element | DOM.Element[]): cheerio.CheerioAPI => {
return cheerio.load(html, CHEERIO_CONFIG);
};
const noLogin = (res: Response) => res.url.includes('login_timeout') || res.status == 403;
/** add CSRF token to any request URL as parameters */
export const addCSRFTokenToUrl = (url: string, token: string): string => {
if (url.includes('?')) {
url += `&_csrf=${token}`;
} else {
url += `?_csrf=${token}`;
}
return url;
};
/** the main helper class */
export class Learn2018Helper {
readonly #provider?: CredentialProvider;
readonly #rawFetch: Fetch;
readonly #myFetch: Fetch;
readonly #myFetchWithToken: Fetch = async (...args) => {
if (this.#csrfToken == '') {
await this.login();
}
const [url, ...remaining] = args;
return this.#myFetch(addCSRFTokenToUrl(url as string, this.#csrfToken), ...remaining);
};
#csrfToken = '';
#lang = Language.ZH;
readonly #withReAuth = (rawFetch: Fetch): Fetch => {
const login = this.login.bind(this);
return async function wrappedFetch(...args) {
const retryAfterLogin = async () => {
await login();
return await rawFetch(...args).then((res: Response) => {
if (noLogin(res)) {
return Promise.reject({
reason: FailReason.NOT_LOGGED_IN,
} as ApiError);
} else if (res.status != 200) {
return Promise.reject({
reason: FailReason.UNEXPECTED_STATUS,
extra: {
code: res.status,
text: res.statusText,
},
} as ApiError);
} else {
return res;
}
});
};
return await rawFetch(...args).then((res: Response) => (noLogin(res) ? retryAfterLogin() : res));
};
};
public previewFirstPage: boolean;
/** you can provide a CookieJar and / or CredentialProvider in the configuration */
constructor(config?: HelperConfig) {
this.previewFirstPage = config?.generatePreviewUrlForFirstPage ?? true;
this.#provider = config?.provider;
this.#rawFetch = config?.fetch ?? makeFetch(config?.cookieJar);
this.#myFetch = this.#provider
? this.#withReAuth(this.#rawFetch)
: async (...args) => {
const result = await this.#rawFetch(...args);
if (noLogin(result))
return Promise.reject({
reason: FailReason.NOT_LOGGED_IN,
} as ApiError);
return result;
};
}
/** fetch CSRF token from helper (invalid after login / re-login), might be '' if not logged in */
public getCSRFToken(): string {
return this.#csrfToken;
}
/** login is necessary if you do not provide a `CredentialProvider` */
public async login(username?: string, password?: string): Promise<void> {
if (!username || !password) {
if (!this.#provider)
return Promise.reject({
reason: FailReason.NO_CREDENTIAL,
} as ApiError);
const credential = await this.#provider();
username = credential.username;
password = credential.password;
}
const ticketResponse = await this.#rawFetch(URL.ID_LOGIN(), {
body: URL.ID_LOGIN_FORM_DATA(username, password),
method: 'POST',
});
if (!ticketResponse.ok) {
return Promise.reject({
reason: FailReason.ERROR_FETCH_FROM_ID,
} as ApiError);
}
// check response from id.tsinghua.edu.cn
const ticketResult = await ticketResponse.text();
const body = $(ticketResult);
const targetURL = body('a').attr('href') as string;
const ticket = targetURL.split('=').slice(-1)[0];
if (ticket === 'BAD_CREDENTIALS') {
return Promise.reject({
reason: FailReason.BAD_CREDENTIAL,
} as ApiError);
}
const loginResponse = await this.#rawFetch(URL.LEARN_AUTH_ROAM(ticket));
if (loginResponse.ok !== true) {
return Promise.reject({
reason: FailReason.ERROR_ROAMING,
} as ApiError);
}
const courseListPageSource: string = await (await this.#rawFetch(URL.LEARN_STUDENT_COURSE_LIST_PAGE())).text();
const tokenRegex = /^.*&_csrf=(\S*)".*$/gm;
const tokenMatches = [...courseListPageSource.matchAll(tokenRegex)];
if (tokenMatches.length == 0) {
return Promise.reject({
reason: FailReason.INVALID_RESPONSE,
extra: 'cannot fetch CSRF token from source',
} as ApiError);
}
this.#csrfToken = tokenMatches[0][1];
const langRegex = /<script src="\/f\/wlxt\/common\/languagejs\?lang=(zh|en)"><\/script>/g;
const langMatches = [...courseListPageSource.matchAll(langRegex)];
if (langMatches.length !== 0) this.#lang = langMatches[0][1] as Language;
}
/** logout (to make everyone happy) */
public async logout(): Promise<void> {
await this.#rawFetch(URL.LEARN_LOGOUT(), { method: 'POST' });
}
/** get user's name and department */
public async getUserInfo(courseType = CourseType.STUDENT): Promise<UserInfo> {
const content = await (await this.#myFetchWithToken(URL.LEARN_HOMEPAGE(courseType))).text();
const dom = $(content);
const name = dom('a.user-log').text().trim();
const department = dom('.fl.up-img-info p:nth-child(2) label').text().trim();
return {
name,
department,
};
}
/**
* Get calendar items during the specified period (in yyyymmdd format).
* @param startDate start date (inclusive)
* @param endDate end date (inclusive)
* If the API returns any error, this function will throw `FailReason.INVALID_RESPONSE`,
* and we currently observe a limit of no more that 29 days.
* Otherwise it will return the parsed data (might be empty if the period is too far away from now)
*/
public async getCalendar(startDate: string, endDate: string, graduate = false): Promise<CalendarEvent[]> {
const ticketResponse = await this.#myFetchWithToken(URL.REGISTRAR_TICKET(), {
method: 'POST',
body: URL.REGISTRAR_TICKET_FORM_DATA(),
});
let ticket = (await ticketResponse.text()) as string;
ticket = ticket.substring(1, ticket.length - 1);
await this.#myFetch(URL.REGISTRAR_AUTH(ticket));
const response = await this.#myFetch(URL.REGISTRAR_CALENDAR(startDate, endDate, graduate, JSONP_EXTRACTOR_NAME));
if (!response.ok) {
return Promise.reject({
reason: FailReason.INVALID_RESPONSE,
} as ApiError);
}
const result = extractJSONPResult(await response.text()) as any[];
return result.map<CalendarEvent>((i) => ({
location: i.dd,
status: i.fl,
startTime: i.kssj,
endTime: i.jssj,
date: i.nq,
courseName: i.nr,
}));
}
public async getSemesterIdList(): Promise<string[]> {
const json = await (await this.#myFetchWithToken(URL.LEARN_SEMESTER_LIST())).json();
if (!Array.isArray(json)) {
return Promise.reject({
reason: FailReason.INVALID_RESPONSE,
extra: json,
} as ApiError);
}
const semesters = json as string[];
// sometimes web learning returns null, so confusing...
return semesters.filter((s) => s != null);
}
public async getCurrentSemester(): Promise<SemesterInfo> {
const json = await (await this.#myFetchWithToken(URL.LEARN_CURRENT_SEMESTER())).json();
if (json.message !== 'success') {
return Promise.reject({
reason: FailReason.INVALID_RESPONSE,
extra: json,
} as ApiError);
}
const result = json.result;
return {
id: result.id,
startDate: new Date(result.kssj),
endDate: new Date(result.jssj),
startYear: Number(result.xnxq.slice(0, 4)),
endYear: Number(result.xnxq.slice(5, 9)),
type: parseSemesterType(Number(result.xnxq.slice(10, 11))),
};
}
/** get all courses in the specified semester */
public async getCourseList(semesterID: string, courseType: CourseType = CourseType.STUDENT): Promise<CourseInfo[]> {
const json = await (await this.#myFetchWithToken(URL.LEARN_COURSE_LIST(semesterID, courseType, this.#lang))).json();
if (json.message !== 'success' || !Array.isArray(json.resultList)) {
return Promise.reject({
reason: FailReason.INVALID_RESPONSE,
extra: json,
} as ApiError);
}
const result = (json.resultList ?? []) as any[];
const courses: CourseInfo[] = [];
await Promise.all(
result.map(async (c) => {
courses.push({
id: c.wlkcid,
name: decodeHTML(c.zywkcm),
chineseName: decodeHTML(c.kcm),
englishName: decodeHTML(c.ywkcm),
timeAndLocation: await (await this.#myFetchWithToken(URL.LEARN_COURSE_TIME_LOCATION(c.wlkcid))).json(),
url: URL.LEARN_COURSE_PAGE(c.wlkcid, courseType),
teacherName: c.jsm ?? '', // teacher can not fetch this
teacherNumber: c.jsh,
courseNumber: c.kch,
courseIndex: Number(c.kxh), // c.kxh could be string (teacher mode) or number (student mode)
courseType,
});
}),
);
return courses;
}
/**
* Get certain type of content of all specified courses.
* It actually wraps around other `getXXX` functions. You can ignore the failure caused by certain courses.
*/
public async getAllContents(
courseIDs: string[],
type: ContentType,
courseType: CourseType = CourseType.STUDENT,
allowFailure = false,
): Promise<CourseContent> {
let fetchFunc: (courseID: string, courseType: CourseType) => Promise<Content[]>;
switch (type) {
case ContentType.NOTIFICATION:
fetchFunc = this.getNotificationList;
break;
case ContentType.FILE:
fetchFunc = this.getFileList;
break;
case ContentType.HOMEWORK:
fetchFunc = this.getHomeworkList;
break;
case ContentType.DISCUSSION:
fetchFunc = this.getDiscussionList;
break;
case ContentType.QUESTION:
fetchFunc = this.getAnsweredQuestionList;
break;
}
const contents: CourseContent = {};
const results = await Promise.allSettled(
courseIDs.map(async (id) => {
contents[id] = await fetchFunc.bind(this)(id, courseType);
}),
);
if (!allowFailure) {
for (const r of results) {
if (r.status == 'rejected') {
return Promise.reject({
reason: FailReason.INVALID_RESPONSE,
extra: {
reason: r.reason,
},
} as ApiError);
}
}
}
return contents;
}
/** Get all notifications (课程公告) of the specified course. */
public async getNotificationList(
courseID: string,
courseType: CourseType = CourseType.STUDENT,
): Promise<Notification[]> {
const json = await (await this.#myFetchWithToken(URL.LEARN_NOTIFICATION_LIST(courseID, courseType))).json();
if (json.result !== 'success') {
return Promise.reject({
reason: FailReason.INVALID_RESPONSE,
extra: json,
} as ApiError);
}
const result = (json.object?.aaData ?? json.object?.resultsList ?? []) as any[];
const notifications: Notification[] = [];
await Promise.all(
result.map(async (n) => {
const notification: INotification = {
id: n.ggid,
content: decodeHTML(Base64.decode(n.ggnr ?? '')),
title: decodeHTML(n.bt),
url: URL.LEARN_NOTIFICATION_DETAIL(courseID, n.ggid, courseType),
publisher: n.fbrxm,
hasRead: n.sfyd === '是',
markedImportant: Number(n.sfqd) === 1, // n.sfqd could be string '1' (teacher mode) or number 1 (student mode)
publishTime: new Date(n.fbsj && typeof n.fbsj === 'string' ? n.fbsj : n.fbsjStr),
};
let detail: INotificationDetail = {};
const attachmentName = courseType === CourseType.STUDENT ? n.fjmc : n.fjbt;
if (attachmentName) {
detail = await this.parseNotificationDetail(courseID, notification.id, courseType, attachmentName);
}
notifications.push({ ...notification, ...detail });
}),
);
return notifications;
}
/** Get all files (课程文件) of the specified course. */
public async getFileList(courseID: string, courseType: CourseType = CourseType.STUDENT): Promise<File[]> {
const json = await (await this.#myFetchWithToken(URL.LEARN_FILE_LIST(courseID, courseType))).json();
if (json.result !== 'success') {
return Promise.reject({
reason: FailReason.INVALID_RESPONSE,
extra: json,
} as ApiError);
}
let result: any[] = [];
if (Array.isArray(json.object?.resultsList)) {
// teacher
result = json.object.resultsList;
} else if (Array.isArray(json.object)) {
// student
result = json.object;
}
const files: File[] = [];
await Promise.all(
result.map(async (f) => {
const title = decodeHTML(f.bt);
const downloadUrl = URL.LEARN_FILE_DOWNLOAD(
courseType === CourseType.STUDENT ? f.wjid : f.id,
courseType,
courseID,
);
const previewUrl = URL.LEARN_FILE_PREVIEW(ContentType.FILE, f.wjid, courseType, this.previewFirstPage);
files.push({
id: f.wjid,
title: decodeHTML(f.bt),
description: decodeHTML(f.ms),
rawSize: f.wjdx,
size: f.fileSize,
uploadTime: new Date(f.scsj),
downloadUrl,
previewUrl,
isNew: f.isNew,
markedImportant: f.sfqd === 1,
visitCount: f.llcs ?? 0,
downloadCount: f.xzcs ?? 0,
fileType: f.wjlx,
remoteFile: {
id: f.wjid,
name: title,
downloadUrl,
previewUrl,
size: f.fileSize,
},
});
}),
);
return files;
}
/** Get all homeworks (课程作业) of the specified course. */
public async getHomeworkList(courseID: string): Promise<Homework[]>;
public async getHomeworkList(courseID: string, courseType: CourseType.STUDENT): Promise<Homework[]>;
public async getHomeworkList(courseID: string, courseType: CourseType.TEACHER): Promise<HomeworkTA[]>;
public async getHomeworkList(
courseID: string,
courseType: CourseType = CourseType.STUDENT,
): Promise<Homework[] | HomeworkTA[]> {
if (courseType === CourseType.TEACHER) {
const json = await (await this.#myFetchWithToken(URL.LEARN_HOMEWORK_LIST_TEACHER(courseID))).json();
if (json.result !== 'success') {
return Promise.reject({
reason: FailReason.INVALID_RESPONSE,
extra: json,
} as ApiError);
}
const result = (json.object?.aaData ?? []) as any[];
const homeworks: HomeworkTA[] = [];
await Promise.all(
result.map(async (d) => {
homeworks.push({
id: d.zyid,
index: d.wz,
title: decodeHTML(d.bt),
description: decodeHTML(Base64.decode(d.nr)),
publisherId: d.fbr,
publishTime: new Date(d.fbsj),
startTime: new Date(d.kssj),
deadline: new Date(d.jzsj),
url: URL.LEARN_HOMEWORK_DETAIL_TEACHER(courseID, d.zyid),
completionType: d.zywcfs,
submissionType: d.zytjfs,
gradedCount: d.ypys,
submittedCount: d.yjs,
unsubmittedCount: d.wjs,
});
}),
);
return homeworks;
} else {
const allHomework: Homework[] = [];
await Promise.all(
URL.LEARN_HOMEWORK_LIST_SOURCE(courseID).map(async (s) => {
const homeworks = await this.getHomeworkListAtUrl(s.url, s.status);
allHomework.push(...homeworks);
}),
);
return allHomework;
}
}
/** Get all discussions (课程讨论) of the specified course. */
public async getDiscussionList(courseID: string, courseType: CourseType = CourseType.STUDENT): Promise<Discussion[]> {
const json = await (await this.#myFetchWithToken(URL.LEARN_DISCUSSION_LIST(courseID, courseType))).json();
if (json.result !== 'success') {
return Promise.reject({
reason: FailReason.INVALID_RESPONSE,
extra: json,
} as ApiError);
}
const result = (json.object?.resultsList ?? []) as any[];
const discussions: Discussion[] = [];
await Promise.all(
result.map(async (d) => {
discussions.push({
...this.parseDiscussionBase(d),
boardId: d.bqid,
url: URL.LEARN_DISCUSSION_DETAIL(d.wlkcid, d.bqid, d.id, courseType),
});
}),
);
return discussions;
}
/**
* Get all notifications (课程答疑) of the specified course.
* The student version supports only answered questions, while the teacher version supports all questions.
*/
public async getAnsweredQuestionList(
courseID: string,
courseType: CourseType = CourseType.STUDENT,
): Promise<Question[]> {
const json = await (await this.#myFetchWithToken(URL.LEARN_QUESTION_LIST_ANSWERED(courseID, courseType))).json();
if (json.result !== 'success') {
return Promise.reject({
reason: FailReason.INVALID_RESPONSE,
extra: json,
} as ApiError);
}
const result = (json.object?.resultsList ?? []) as any[];
const questions: Question[] = [];
await Promise.all(
result.map(async (q) => {
questions.push({
...this.parseDiscussionBase(q),
question: Base64.decode(q.wtnr),
url: URL.LEARN_QUESTION_DETAIL(q.wlkcid, q.id, courseType),
});
}),
);
return questions;
}
private async getHomeworkListAtUrl(url: string, status: IHomeworkStatus): Promise<Homework[]> {
const json = await (await this.#myFetchWithToken(url)).json();
if (json.result !== 'success') {
return Promise.reject({
reason: FailReason.INVALID_RESPONSE,
extra: json,
} as ApiError);
}
const result = (json.object?.aaData ?? []) as any[];
const homeworks: Homework[] = [];
await Promise.all(
result.map(async (h) => {
homeworks.push({
id: h.zyid,
studentHomeworkId: h.xszyid,
title: decodeHTML(h.bt),
url: URL.LEARN_HOMEWORK_DETAIL(h.wlkcid, h.zyid, h.xszyid),
deadline: new Date(h.jzsj),
submitUrl: URL.LEARN_HOMEWORK_SUBMIT_PAGE(h.wlkcid, h.xszyid),
submitTime: h.scsj === null ? undefined : new Date(h.scsj),
grade: h.cj === null ? undefined : h.cj,
gradeLevel: GRADE_LEVEL_MAP.get(h.cj),
graderName: trimAndDefine(h.jsm),
gradeContent: trimAndDefine(h.pynr),
gradeTime: h.pysj === null ? undefined : new Date(h.pysj),
...status,
...(await this.parseHomeworkDetail(h.wlkcid, h.zyid, h.xszyid)),
});
}),
);
return homeworks;
}
private async parseNotificationDetail(
courseID: string,
id: string,
courseType: CourseType,
attachmentName: string,
): Promise<INotificationDetail> {
/// from JSON (backup, currently not used)
// const postParams = new FormData();
// postParams.append('id', id);
// postParams.append('wlkcid', courseID);
// const metadata = await (await this.#myFetchWithToken(URL.LEARN_NOTIFICATION_EDIT(courseType), {
// 'method': 'POST',
// 'body': postParams,
// })).json();
// const attachmentId = metadata.ggfjid as string;
/// parsed from HTML
const response = await this.#myFetchWithToken(URL.LEARN_NOTIFICATION_DETAIL(courseID, id, courseType));
const result = $(await response.text());
let path = '';
if (courseType === CourseType.STUDENT) {
path = result('.ml-10').attr('href')!;
} else {
path = result('#wjid').attr('href')!;
}
const size = trimAndDefine(result('div#attachment > div.fl > span[class^="color"]').first().text())!;
const params = new URLSearchParams(path.split('?').slice(-1)[0]);
const attachmentId = params.get('wjid')!;
if (!path.startsWith(URL.LEARN_PREFIX)) {
path = URL.LEARN_PREFIX + path;
}
return {
attachment: {
name: attachmentName,
id: attachmentId,
downloadUrl: path,
previewUrl: URL.LEARN_FILE_PREVIEW(ContentType.NOTIFICATION, attachmentId, courseType, this.previewFirstPage),
size,
},
};
}
private async parseHomeworkDetail(courseID: string, id: string, studentHomeworkID: string): Promise<IHomeworkDetail> {
const response = await this.#myFetchWithToken(URL.LEARN_HOMEWORK_DETAIL(courseID, id, studentHomeworkID));
const result = $(await response.text());
const fileDivs = result('div.list.fujian.clearfix');
return {
description: trimAndDefine(result('div.list.calendar.clearfix > div.fl.right > div.c55').slice(0, 1).html()),
answerContent: trimAndDefine(result('div.list.calendar.clearfix > div.fl.right > div.c55').slice(1, 2).html()),
submittedContent: trimAndDefine($(result('div.boxbox').slice(1, 2).toArray())('div.right').slice(2, 3).html()),
attachment: this.parseHomeworkFile(fileDivs[0]),
answerAttachment: this.parseHomeworkFile(fileDivs[1]),
submittedAttachment: this.parseHomeworkFile(fileDivs[2]),
gradeAttachment: this.parseHomeworkFile(fileDivs[3]),
};
}
private parseHomeworkFile(fileDiv: DOM.Element): RemoteFile | undefined {
const fileNode = ($(fileDiv)('.ftitle').children('a')[0] ?? $(fileDiv)('.fl').children('a')[0]) as DOM.Element;
if (fileNode !== undefined) {
const size = trimAndDefine($(fileDiv)('.fl > span[class^="color"]').first().text())!;
const params = new URLSearchParams(fileNode.attribs.href.split('?').slice(-1)[0]);
const attachmentId = params.get('fileId')!;
// so dirty here...
let downloadUrl = URL.LEARN_PREFIX + fileNode.attribs.href;
if (params.has('downloadUrl')) {
downloadUrl = URL.LEARN_PREFIX + params.get('downloadUrl')!;
}
return {
id: attachmentId,
name: (fileNode.children[0] as DOM.Text).data!,
downloadUrl,
previewUrl: URL.LEARN_FILE_PREVIEW(
ContentType.HOMEWORK,
attachmentId,
CourseType.STUDENT,
this.previewFirstPage,
),
size,
};
} else {
return undefined;
}
}
private parseDiscussionBase(d: any): IDiscussionBase {
return {
id: d.id,
title: decodeHTML(d.bt),
publisherName: d.fbrxm,
publishTime: new Date(d.fbsj),
lastReplyTime: new Date(d.zhhfsj),
lastReplierName: d.zhhfrxm,
visitCount: d.djs ?? 0, // teacher cannot fetch this
replyCount: d.hfcs,
};
}
public async submitHomework(
studentHomeworkID: string,
content = '',
attachment?: IHomeworkSubmitAttachment,
removeAttachment = false,
): Promise<IHomeworkSubmitResult> {
return await (
await this.#myFetchWithToken(URL.LEARN_HOMEWORK_SUBMIT(), {
method: 'POST',
body: URL.LEARN_HOMEWORK_SUBMIT_FORM_DATA(studentHomeworkID, content, attachment, removeAttachment),
})
).json();
}
public async setLanguage(lang: Language): Promise<void> {
await this.#myFetchWithToken(URL.LEARN_WEBSITE_LANGUAGE(lang), {
method: 'POST',
});
this.#lang = lang;
}
public getCurrentLanguage(): Language {
return this.#lang;
}
}
export * from './types';