-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappplus.js
More file actions
496 lines (400 loc) · 13.3 KB
/
appplus.js
File metadata and controls
496 lines (400 loc) · 13.3 KB
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
const program = require('commander')
const fs = require('fs')
const csv = require('csv');
const stringify = require('csv-stringify')
const csvParseSync = require('csv-parse/lib/sync')
const {
getCourseNameByCode
} = require('./src/util.js');
const { BotPlus } = require( './src/botplus')
const { PuppeteerDriver } = require( './src/puppeteer.js')
const enableVideoApi = true
const {
handleCreateDb,
handleAccountsCheckin,
handleCreateLog,
handleLearnCourse,
handleLearnCourses,
handleLearnModuleByCode,
handleGetCourseSumaries,
handleLearnModuleOfAccounts,
handleLearnModuleOfAccounts2,
handleReadScore,
getAccountsCourseCode,
handleLearnFinal,
handleGenSubject,
handleGenAccounts,
handleGenQuiz,
simpleLearn
} = require('./src/indexplus')
// example: node app.js -- createlog 4255 #毛泽东思想和中国特色社会主义理论体
const isNetwork = true
program
.version('2.0.1')
.option('-c, --config <configfile>', 'config file')
.option('-u, --username <username>', 'user name')
.option('-p, --password <password>', 'user password')
.option('-a, --account <accountfile>', 'account file')
.option('-b, --base <basepath>', 'base path')
.option('-m, --modulefile <modulefile>', 'module file')
.option('-s, --submitquiz <yes|no>', 'submit quiz yes or no')
.option('-f, --submitfinal <yes|no>', 'submit final yes or no')
.option('-t, --type <type>', 'module type') // 学习的类型
program.command('createlog <course>')
.description('handleCreateLog')
.action(function(course) {
console.log("handleCreateLog ", course, program.username, program.password)
handleCreateLog(course, program.username, program.password)
})
program.command('simplelearn')
.description('simple learn')
.action(async function( ) {
if (!isAvaible()) {
console.log("软件出现问题,请联系开发人员!")
return
}
let type = ( program.type || null)
let submitquiz = ( program.submitquiz || null)
let options = { type, submitquiz }
let accounts = await getAccounts()
await simpleLearn(accounts, options)
})
program.command('lcourse <course>')
.description('learn all module')
.action(async function(course) {
if (!isAvaible()) {
console.log("软件出现问题,请联系开发人员!")
return
}
let courseTitle = course
if (Number(course)) {
courseTitle = getCourseNameByCode(course)
}
let accounts = await getAccountsJsonByKey(program.account)
let username = null,
password = null
if (accounts.length > 0) {
username = accounts[0].username
password = accounts[0].password
}
console.log("handleLearnCourse ", course, username, password)
handleLearnCourse(courseTitle, username, password)
})
program.command('lmodule <course> <module>')
.description('learn by code module')
.action(async function(course, moduleCode) {
console.log("handleLearnModuleByCode ", moduleCode)
let accounts = []
if (program.account) {
accounts = await getAccountsCsvByKey(accountfile)
}
if (program.username) {
accounts.push({
username: program.username,
password: program.password
})
}
let options = {
type: program.type,
submitquiz: program.submitquiz,
}
handleLearnModuleByCode(course, moduleCode, program.username, program.password, options)
})
program.command('readscore <course>')
.description('readscore by course code')
.action(function(course) {
console.log("handleReadScore ", course)
handleReadScore(course, program.username, program.password)
})
// 根据网络数据,创建所有课程的数据文件
program.command('initdb [accountfile]')
.description('create all courses db')
.action(async function(accountfile) {
let accounts = await getAccounts(accountfile)
console.log("创建课程db", accountfile, accounts.length)
await handleCreateDb(accounts)
})
// 检查所有账户是否可以登录
// node app.js checkin account.json
program.command('checkin [accountfile]')
.description('checkin all accounts, support json, csv')
.action(async function(accountfile) {
let accounts = await getAccounts(accountfile)
console.log("检查账户登录...", accountfile)
await handleAccountsCheckin(accounts)
})
// 根据账号,课程名称,取得科目代码
// node app.js getcode account.json
program.command('getcode [accountfile]')
.description('checkin all accounts, support json, csv')
.action(async function(accountfile) {
// [{username, password, subject}]
let accounts = await getAccounts(accountfile)
console.log("检查账户登录...", accountfile, accounts.length)
await getAccountsCourseCode(accounts)
})
// 根据网络数据,建立学习进度数据文件
program.command('summary')
.description('summary all courses.')
.action(async function() {
if (!isAvaible()) {
console.log("软件出现问题,请联系开发人员!")
return
}
// 取得所有账户信息,取得每个账户的课程进度
let accounts = await getAccounts()
console.log("get all course summary", accounts.length)
if( accounts.length> 0 ){
let sumaries = await handleGetCourseSumaries(accounts )
// 保存文件
let basepath = program.base;
if( basepath ){
let filename = `${basepath}/summary-${(new Date).getTime()}.csv`
console.log( "before save to file:", filename )
//fs.writeFileSync(filename, JSON.stringify(sumaries))
const csv = stringify(sumaries, {}, function(err, records){
fs.writeFileSync(filename, records)
console.log( "after save to file:", filename )
})
}
}
})
// 根据网络数据,学习所有课程
program.command('learn')
.description('learn all courses.')
.action(async function( ) {
if (!isAvaible()) {
console.log("软件出现问题,请联系开发人员!")
return
}
let options = {
type: program.type,
submitquiz: program.submitquiz,
}
// 取得所有账户信息
// 为每个账户创建课程日志
let accounts = await getAccounts( )
console.log("accounts learn all courses", accounts.length)
await handleLearnCourses(accounts, options)
// 按顺序学习每门课程
})
program.command('lfinal')
.description('learn final exam ')
.action(async function(course,accountfile) {
if (!isAvaible()) {
console.log("软件出现问题,请联系开发人员!")
return
}
console.log('accountfile----:'+ accountfile);
let accounts = []
if (program.account || program.username) {
accounts = await getAccounts(program.account)
}
let courseTitle = course
if (Number(course)) {
courseTitle = getCourseNameByCode(course)
}
let options = {
submitfinal: program.submitfinal,
}
await handleLearnFinal(accounts, courseTitle, options )
})
// 生成课程数据文件
program.command('gensubject')
.description('生成课程数据文件')
.action(async function( ) {
if (!isAvaible()) {
console.log("软件出现问题,请联系开发人员!")
return
}
let accounts = []
if (program.account || program.username) {
accounts = await getAccounts(program.account)
}
// { username: '', password: '', subject: ''}
await handleGenSubject(accounts )
})
// 生成课程数据文件,把一个账户文件按照科目分别生成对应的账号文件,存入db/accounts/xxxx.csv
// genaccount --account=account.csv
program.command('genaccount')
.description('生成账号数据文件')
.action(async function( ) {
if (!isAvaible()) {
console.log("软件出现问题,请联系开发人员!")
return
}
let accounts = []
if (program.account || program.username) {
accounts = await getAccounts(program.account)
}
// { username: '', password: '', subject: '', code: ''}
await handleGenAccounts(accounts )
})
program.command( 'genquiz [byreview]')
.description('生成测验数据文件')
.action(async function( byreview) {
let accounts = await getAccounts( )
console.log( "byreview=", byreview)
byreview = 'byreview'
handleGenQuiz(accounts, byreview)
})
program.command( 'parsecsv <filename>')
.description('解析账户数据文件')
.action(async function( filename) {
await parseSubjcts(filename)
})
//
/////////////////////////////////////////////////////////////////////////////////////////////////
program.parse(process.argv)
if (program.username) console.log(`- ${program.username}`);
if (program.password) console.log(`- ${program.password}`);
async function loadConfig(){
//
}
async function testBotplus(){
let driver = new PuppeteerDriver
let bot = new BotPlus(driver)
await bot.login( '2021101201496', '19850114');
let subject = '国家开放大学学习指南'
await bot.prepareForLearn(subject);
await bot.profileCouse(subject)
}
async function getAccounts(accountfile=null) {
if ( program.account ){
accountfile = program.account
}
console.log( 'accountfile=', accountfile)
let accounts = []
if (/csv$/.test(accountfile)) {
accounts = await getAccountsCsvByKey(accountfile)
} else if (/json$/.test(accountfile)) {
accounts = await getAccountsJsonByKey(accountfile)
} else if( program.username && program.password){
accounts.push({ username: program.username, password: program.password })
}
// trim
accounts.forEach((acc)=>{
if( acc.subject ){
// 清除空格,字母小写便于比较
acc.subject = acc.subject.trim().toLowerCase()
}
})
return accounts
}
async function getModuleIds(course) {
let moduleids = []
let filename = `./db/subjects/${course}_${program.type}_module.json`
try {
let data = fs.readFileSync(filename, "utf-8")
if (data != null) {
moduleids = JSON.parse(data);
} else {
console.error(`无法读取数据文件 ${filename}`);
}
} catch (ex) {
console.error(`无法读取数据文件 ${filename}`, ex);
}
return moduleids
}
async function getAccountsJsonByKey(filename) {
console.log('==============getAccountsJsonByKey==============');
console.log('filename---:', filename);
let accounts = []
if (program.username) {
accounts.push({
username: program.username,
password: program.password
})
} else {
filename = filename || 'account.json'
if (filename) {
try {
let data = fs.readFileSync(filename, "utf-8")
if (data != null) {
let res = JSON.parse(data);
accounts = res.accounts || [];
} else {
console.error(`无法读取账户文件 ${filename}`);
}
} catch (ex) {
console.error(`无法读取账户文件 ${filename}`, ex);
}
}
}
return accounts
}
async function getAccountsCsvByKey(filename) {
let accounts = []
if (filename) {
try {
let data = fs.readFileSync(filename, "utf-8")
if (data != null) {
accounts = csvParseSync(data, {
columns: true,
skip_empty_lines: true
});
} else {
console.error(`无法读取账户文件 ${filename}`);
}
} catch (ex) {
console.error(`无法读取账户文件 ${filename}`, ex);
}
}
return accounts
}
// 解析csv文件,字段 realname,username,gender,idnum,birth,major,subjects
async function parseSubjcts(filename ){
if (filename) {
let data = fs.readFileSync(filename, "utf-8")
if (data != null) {
let accounts = csvParseSync(data, {
columns: true,
skip_empty_lines: true
});
let newAccounts = []
let uniqSubjectAccounts = []
let uniqSubjects = []
console.log( `accounts = ${accounts.length}`)
for( let i=0; i<accounts.length; i++){
let account = accounts[i]
let {realname, username, password, subjects} = account
let subjectArray = subjects.split( '、')
for( let j = 0; j<subjectArray.length; j++){
let subject = subjectArray[j]
console.log( `subjectArray${j}= `,subjectArray, subject)
if( subject.length == 0){
continue;
}
let account = {realname, username, password, subject}
if( !uniqSubjects.includes(subject)){
uniqSubjects.push( subject )
uniqSubjectAccounts.push( account )
}
newAccounts.push( account )
}
}
let newfilename = 'subjects2.csv'
const csv = stringify(newAccounts, { header: true, columns:['realname', 'username', 'password', 'subject']}, function(err, records){
fs.writeFileSync(newfilename, records)
console.log( "after save to file:", newfilename )
})
let newfilename2 = 'subjects1.csv'
const csv2 = stringify(uniqSubjectAccounts, {header: true, columns:['realname', 'username', 'password', 'subject']}, function(err, records){
fs.writeFileSync(newfilename2, records)
console.log( "after save to file:", newfilename2 )
})
} else {
console.error(`无法读取账户文件 ${filename}`);
}
}
}
// 软件是否可用
function isAvaible() {
let availabe = new Date('2020-12-30')
let now = new Date()
if (now < availabe) {
return true
} else {
return false
}
}