This repository has been archived by the owner on Dec 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
engine_langeasy.js
84 lines (79 loc) · 3.08 KB
/
engine_langeasy.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
const fetch = require('node-fetch')
const fs = require('fs');
const queryString = require('query-string');
const _ = require('lodash')
const parser = require('fast-xml-parser');
const { Book, Word } = require('./data')
const Config = require('./config')
const moment = require('moment');
const utils = require('./utils')
const { logger } = utils
const { Engine } = require('./engine')
const Maimemo = require('./engine_maimemo')
class Langeasy extends Engine {
constructor(props) {
super('朗易思听');
this.config = Config.langeasy
this.config.to_maimemo_bookid = _.defaultTo(this.config.to_maimemo_bookid, Config.maimemo.bookid)
}
login() {
if (_.size(this.config.username) > 0) {
return this.req.get('https://langeasy.com.cn/denglu.action').then(res => {
return this.req.post('https://langeasy.com.cn/login.action', {
form: {
name: this.config.username,
passwd: this.config.password,
redirectTo: 'https://langeasy.com.cn',
},
headers: {
'Upgrade-Insecure-Requests': 1,
'Referer': 'https://langeasy.com.cn/denglu.action',
'Origin': 'https://langeasy.com.cn',
}
}).then(res2 => {
logger.error('朗易思听', '登录失败')
}).catch(err => {
if (err.statusCode == 302) {
super.login()
} else {
logger.error('朗易思听', err)
}
})
})
// .then(res => {
// logger.info(res)
// })
}
}
getWords(page = 0, all = false) {
return this.req.get(`https://langeasy.com.cn/getUserNewWord.action?page=${page}&time=${new Date().getTime()}`, { json: true })
.then(res => {
let plist = []
if (page == 0) {
logger.info(this.name, '应获取单词数量', res.pageInfo.totalRecord)
if (all) {
// 自动获取所有单词
for (let i = 1; i < res.pageInfo.totalPage; i++) {
plist.push(this.getWords(i))
}
}
}
logger.info(this.name, `get words page=${page} len=${res.wordList.length}`)
plist.push(Promise.resolve(res.wordList))
return Promise.all(plist).then(presList => {
return [].concat.apply([], presList)
})
})
}
async work() {
let words = await this.getWords(0, this.debug != true)
logger.info(this.name, '总单词数量', words.length)
let book = new Book()
words.forEach(wd => {
book.addWord(wd.word)
})
Maimemo.buildMaimemo(book, this.config.to_maimemo_bookid)
// return book
}
}
module.exports = Langeasy