-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
639 lines (548 loc) · 20 KB
/
index.js
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
#!/usr/bin/env node
const axios = require('axios')
const HtmlParser = require('node-html-parser')
const fs = require('fs')
const date = require('date-and-time')
const { program } = require('commander')
/**
* @type { Map<string, string> }
*/
const globalCookieStore = new Map()
/**
* Setup http client
* withCredentials somehow not working. So please manage cookie by your self
*
* @param {string} baseURL
* @returns {axios.Axios}
*/
async function init(baseURL) {
const httpClient = new axios.Axios({
baseURL,
headers: {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
},
withCredentials: true,
})
const response = await httpClient.get("/")
if (response.status != 200) {
throw new Error(`Init failed! Server responsed a none 200 status code: ${response.status}. Response dumpped to: ${await dumpResponse(response)}`)
}
updateCoolkies(response)
const tokenSearcher = /'X-CSRF-TOKEN': ?'(.+)'/g
const [[_, token]] = response.data.matchAll(tokenSearcher)
httpClient.defaults.headers["X-CSRF-TOKEN"] = token
return httpClient
}
/**
*
* @param { axios.Axios } httpClient
* @param { string } password
* @param { string } username
* @returns { { userNumber: string } }
*/
async function login(httpClient, username, password) {
const body = new FormData()
body.append("login_user_name", username)
body.append("login_password", password)
body.append("login-submit", "Login")
body.append("action", "login")
let response = await httpClient.request({
url: "/",
method: 'POST',
data: body,
maxRedirects: 0,
headers: {
...exportCookies()
}
})
if (response.status != 302) {
throw new Error(`Login failed #1! Server responsed a none 302 status code: ${response.status}. Check your password and try again later. Response dumpped to: ${await dumpResponse(response)}`)
}
updateCoolkies(response)
response = await httpClient.request({
url: "/",
method: "GET",
headers: {
...exportCookies()
}
})
if (response.status != 200) {
throw new Error(`Login failed #2! Server responsed a none 200 status code: ${response.status}. Response dumpped to: ${await dumpResponse(response)}`)
}
updateCoolkies(response)
const userNumberFinder = /<input ?type='hidden' ?name='user_num' ?id='user_num' ?value='([0-9]+)' ?\/>/g
const result = "" + response.data
const [[__, userNumber]] = result.matchAll(userNumberFinder)
if (!userNumber) {
throw new Error(`Login failed #3! Failed to search for token or userNumber. Response dumpped to: ${await dumpResponse(response)}`)
}
return { userNumber }
}
/**
*
* @param {axios.Axios} httpClient
* @param {*} credentials
* @param {Date} d
* @returns { { name: string, staffID: string, room: string }[] }
*/
async function fetchDataTable(httpClient, credentials, d) {
const response = await httpClient.get("/ajax.DataTable.Fetch.php", {
headers: {
...exportCookies()
},
params: {
"table_name": "staff",
"sequence_flag": "N",
"action": "load-staff",
"block_num": 1,
"selected_date": date.format(d, "YYYY-MM-DD"),
"user_num": credentials.userNumber,
"_": Date.now()
}
})
updateCoolkies(response)
if (response.status != 200) {
throw new Error(`fetchDataTable failed #1! Server responsed a none 302 status code: ${response.status}. Check your password and try again later. Response dumpped to: ${await dumpResponse(response)}`)
}
let responseBody = "" + JSON.parse(response.data).content
const begin = responseBody.indexOf("<tbody>")
const end = responseBody.indexOf("</tbody>")
responseBody = responseBody.substring(begin, end + 9) // </tbody> length + 1
const html = HtmlParser.parse(responseBody, { lowerCaseTagName: true })
const tBody = html.childNodes[0]
const teachers = []
// Lazy :P | If someday this stops working then I'll consider change this to resolve the DOM
const roomNumberFinder = /<br ?\/?> ?-- ?([0-9a-zA-z]+)/g
const teacherFinder = /<span id='name([0-9]+)'> ?([a-zA-z \-]+) ?<\/span>/g // Group1: StaffID, Group2: Name
let errorTracker = ""
for (const i of tBody.childNodes) {
if (i instanceof HtmlParser.TextNode) {
continue
}
const content = i.toString()
try {
const [[_, room]] = content.matchAll(roomNumberFinder)
const [[__, staffID, name]] = content.matchAll(teacherFinder)
teachers.push({ name, staffID, room })
} catch (err) {
errorTracker += "\n==========================\n"
errorTracker += content
errorTracker += "\n\nPROCESS ERROR\n"
}
}
if (errorTracker.length > 0) {
if (fs.existsSync("tableProcessError.report.txt")) {
fs.rmSync("tableProcessError.report.txt")
}
fs.writeFileSync("tableProcessError.report.txt", errorTracker)
console.log("Error: Failed to fully parse data table! Error dumped to: tableProcessError.report.txt")
}
return teachers
}
/**
*
* @param {axios.Axios} httpClient
* @param {*} credentials
* @param {'current' | 'nextweek'} week
*/
async function fetchAvailableDates(httpClient, credentials, week) {
const response = await httpClient.get("/ajax.DataTable.Fetch.php", {
headers: {
...exportCookies()
},
params: {
"table_name": "plandates",
"sequence_flag": "N",
"action": "load-week",
"block_num": 1,
"selected_week_flag": week,
"selected_user": credentials.userNumber,
"_": Date.now()
}
})
updateCoolkies(response)
if (response.status != 200) {
throw new Error(`fetchDataTable failed #1! Server responsed a none 302 status code: ${response.status}. Check your password and try again later. Response dumpped to: ${await dumpResponse(response)}`)
}
const { content } = JSON.parse(response.data)
if (!content) {
throw new Error(`fetchDataTable failed #2! Illegal response data. Response dumpped to: ${await dumpResponse(response)}`)
}
const finder = /<td align='left' valign='top' class='(.+?)' onClick='' editable-by-student='([YN])' cell-date='([0-9]{4}-[0-9]{2}-[0-9]{2})' cell-block='([0-9])'/g
// 0: <drop>
// 1: flags (plan-day plan today datePlanComplete)
// 2: editable-by-student (Y/N)
// 3: date (yyyy-mm-dd)
// 4: cell block (i guess useless)
const matches = content.matchAll(finder)
const avaDates = []
for (const [_, flags, editable, date, block] of matches) {
if (!flags.includes("datePlanComplete") && editable.startsWith("Y")) {
avaDates.push(date)
}
}
// TODO skip days that already been assigned.
return avaDates
}
/**
*
* @param {axios.Axios} httpClient
* @param {number} staffNumber
* @param {Date} d
* @returns {boolean}
*/
async function checkCapacity(httpClient, staffNumber, d) {
const response = await httpClient.get("/ajax.checkStaffCapacity.php", {
headers: {
...exportCookies()
},
params: {
"block_num": 1,
"plandate": date.format(d, "YYYY-MM-DD"),
"staff_num": staffNumber,
"_": Date.now()
}
})
updateCoolkies(response)
if (response.status != 200) {
throw new Error(`checkCapacity failed #1! Server responsed a none 200 status code: ${response.status}. Check your password and try again later. Response dumpped to: ${await dumpResponse(response)}`)
}
const { available } = JSON.parse(response.data)
if (typeof available != 'number') {
return available.parseInt(available) > 0
} else {
return available > 0
}
}
/**
*
* @param {axios.Axios} httpClient
* @param {number} staffNumber
* @param {{ userNumber: string }} credentials
* @param {string} plan
* @param {Date} d
* @returns {boolean}
*/
async function savePlan(httpClient, staffNumber, { userNumber }, plan, d) {
const response = await httpClient.get("/ajax.savePlan.php", {
headers: {
...exportCookies()
},
params: {
"plan_title": "",
"plan": plan,
"record_num": 0,
"user_num": userNumber,
"plandate": date.format(d, "YYYY-MM-DD"),
"block_num": 1,
"staff_num": staffNumber,
"plan_type": "",
"table_name": "dailyplan",
"editable_by_student": "Y",
"function": ""
}
})
updateCoolkies(response)
if (response.status != 200) {
throw new Error(`savePlan failed #1! Server responsed a none 200 status code: ${response.status}. Check your password and try again later. Response dumpped to: ${await dumpResponse(response)}`)
}
const { ajax_return_code, ajax_message } = JSON.parse(response.data)
if (ajax_return_code == 1) {
return { err: null }
} else {
return { err: ajax_message }
}
}
/**
*
* @param {string} token
* @param {string | number} target
* @param {string} profile
* @param {"log" | "errorlog"} status
* @param {string | undefined | fs.PathLike} extramsg
*/
async function tgNotify(token, target, profile, status, extramsg) {
const http = new axios.Axios({
baseURL: "https://api.telegram.org/bot" + token
})
if (status === 'errorlog') {
const data = new FormData()
data.append("document", new Blob([fs.readFileSync(extramsg)]), extramsg)
const response = await http.post("/sendDocument", data, {
params: {
"chat_id": target
},
headers: {
"Content-Type": "multipart/form-data"
}
})
if (response.status != 200) {
dumpResponse(response, "tglog.txt", true)
}
} else {
const response = await http.post("/sendMessage", JSON.stringify({
"chat_id": target,
"parse_mode": "MarkdownV2",
"text": `MyWeeklyPlanner\\-Auto Report [\`${status}\`] for \`${profile}\`\n\n \`` + (extramsg ?? "Nothing") + "\`"
}), { headers: { "Content-Type": "application/json" }})
if (response.status != 200) {
dumpResponse(response, "tglog.txt", true)
}
}
}
/**
*
* @param {axios.Axios} httpClient
*/
async function logout(httpClient) {
await httpClient.get("/", {
headers: {
...exportCookies()
},
params: {
"action": "logout"
}
})
}
/**
* Dump axios response. Used to generate error report
*
* @param {axios.AxiosResponse<any, any>} r
* @param {fs.PathLike} fn
* @returns {fs.PathLike}
*/
async function dumpResponse(r, fn, notg) {
let dump = `Request failed!\n`
dump += `Status: ${r.status}(${r.statusText})`
dump += "\n============== CONFIG ==============\n"
try {
dump += JSON.stringify(r.config, null, 4)
} catch (err) {
dump += "\n< FAIELD TO DUMP CONFIG >\n"
}
dump += "\n============== REQUEST ==============\n"
dump += JSON.stringify(r.request.headers, null, 4)
dump += "\n============== HEADER ==============\n"
dump += JSON.stringify(r.headers, null, 4)
dump += "\n============== BODY ==============\n"
dump += r.data
const fileName = fn ?? `${Date.now()}.requestdump.txt`
if (fs.existsSync(fileName)) {
fs.rmSync(fileName)
}
fs.writeFileSync(fileName, dump)
if (!notg && tg) {
await tgNotify(config.botToken, config.target, username, "errorlog", fileName)
}
return fileName
}
/**
*
* @param {axios.AxiosResponse<any, any>} r
*/
function updateCoolkies(r) {
if (r.headers["set-cookie"]) {
for (let i of r.headers["set-cookie"]) {
const entries = i.split(";")
for (let e of entries) {
const [k, v] = e.split("=")
if (!["path", "expires", "max-age"].includes(k.toLowerCase())) {
globalCookieStore.set(k, v)
}
}
}
}
}
function exportCookies() {
let cookie = ""
for (let i of globalCookieStore.keys()) {
cookie += `${i}=${globalCookieStore.get(i)};`
}
if (cookie.length > 0) {
cookie = cookie.substring(0, cookie.length - 2)
}
return { "Cookie": cookie }
}
function addDays(date, days) {
const result = new Date(date);
result.setDate(result.getDate() + days);
return result;
}
Date.prototype.isLeapYear = function() {
const year = this.getFullYear();
if((year & 3) != 0) return false;
return ((year % 100) != 0 || (year % 400) == 0);
};
// Get Day of Year
Date.prototype.getDOY = function() {
const dayCount = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
const mn = this.getMonth();
const dn = this.getDate();
let dayOfYear = dayCount[mn] + dn;
if(mn > 1 && this.isLeapYear()) dayOfYear++;
return dayOfYear;
};
program
.name("MyWeeklyPlanner Automation Script")
.description("A tool for setting up plans in CLI")
.version("1.1")
program
.argument("<username>", "Username")
.option("-g --genConfig", "Generate config for <user>")
.option("-l --listTeachers", "List all available teachers")
.option("-t --tg", "Send a notify in telegram")
.option("-q --quiet", "Mute output")
.option("-o --override", "Force override existing entries")
.action(async (username, { tg, listTeachers, genConfig, quiet, override }) => {
if (quiet) {
console["log"] = () => void 0
}
if (!fs.existsSync(`${username}.config.json`)) {
console.log("Please generate config first")
process.exit(-1)
}
if (tg && (!config.botToken || !config.target)) {
console.log("Error! Failed to remind via tg! Because no bot token or target was found!")
process.exit(-1)
}
if (genConfig) {
if (fs.existsSync(`${username}.config.json`)) {
console.log("File exists!")
process.exit(-1)
}
fs.writeFileSync(`${username}.config.json`, JSON.stringify({
baseURL: "example.myweeklyplanner.net",
username,
password: "",
botToken: "Optional",
target: "", // ChannelUsername or UserID
schedule: [
null, // sun
{ // mon
selectionCandidate: {
room: "Required | Mon",
teacher: "OptionalTeacherName"
},
plan: ""
},
{
selectionCandidate: {
room: "Required | Tue",
teacher: "OptionalTeacherName"
},
plan: ""
},
null, // no fit on wed
{
selectionCandidate: {
room: "Required | Thu",
teacher: "OptionalTeacherName"
},
plan: ""
},
{
selectionCandidate: {
room: "Required | Fri",
teacher: "OptionalTeacherName"
},
plan: ""
},
null, // sat
],
}, undefined, 4))
process.exit(0)
}
/**
* @type { {
* baseURL: string,
* username: string,
* password: string,
* botToken: string | undefined | null,
* target: string | number | undefined | null,
* schedule: {
* selectionCandidate: {
* room: string | undefined | null,
* teacher: string | undefined | null,
* },
* plan: string
* }[]
* } }
*/
const config = JSON.parse(fs.readFileSync(`${username}.config.json`).toString())
const http = await init(config.baseURL)
const credentials = await login(http, config.username, config.password)
const notAssignedDates = [
...(await fetchAvailableDates(http, credentials, 'current')),
...(await fetchAvailableDates(http, credentials, 'nextweek'))
]
const today = new Date()
const weekBegin = addDays(today, -today.getDay())
for (let i = 0; i < 14; i ++) {
const week = Math.floor(i / 7)
const data = config.schedule[i - week * 7]
if (!data) {
console.log(`Skipping ${i}(DOW) becuase no data is present!`)
continue
}
const goalDate = addDays(weekBegin, i)
const standardDateStr = date.format(goalDate, "YYYY-MM-DD")
if (goalDate.getDOY() < today.getDOY()) {
console.log(`Skipping ${standardDateStr} because it was gone ~ Yes Forever ~ Can't be controlled like a river never stop`)
continue
}
if (!override && !notAssignedDates.includes(standardDateStr)) {
console.log(`Skipping ${standardDateStr} because you or sombody else already done the job. (hint: use --override)`)
continue
}
try {
const table = await fetchDataTable(http, credentials, goalDate)
if (listTeachers) {
console.log(`At ${goalDate}`)
for (const j of table) {
console.log(` ${j.name}(ID = ${j.staffID}) at ${j.room}`)
}
break
}
if (!data.selectionCandidate || (!data.selectionCandidate.room && !data.selectionCandidate.teacher)) {
console.log("Error! No candicate is provided!")
process.exit(-1)
}
let staff = null
if (data.selectionCandidate.room) {
for (const j of table) {
if (j.room == data.selectionCandidate.room) {
staff = j.staffID
}
}
} else if (data.selectionCandidate.teacher) {
for (const j of table) {
if (j.name.includes(data.selectionCandidate.name)) {
staff = j.staffID
}
}
}
if (!staff) {
console.log("Error! No result was found with candidate: " + JSON.stringify(data.selectionCandidate) + " at: " + standardDateStr)
continue
}
if (!await checkCapacity(http, staff, goalDate)) {
console.log(`Skipping ${standardDateStr} because its full`)
continue
}
const { err } = await savePlan(http, staff, credentials, data.plan, goalDate)
if (err) {
console.log(`Skipping ${standardDateStr} because server rejected with reason: ${err}`)
continue
}
console.log("Successfully setup plan on " + standardDateStr)
} catch(err) {
console.error(err)
}
}
await logout(http)
if (tg) {
await tgNotify(config.botToken, config.target, config.username, "log", "Success")
}
console.log("Logged out successfully! Renew job done!")
})
program.parse()