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
/
Copy pathengine_youdao.ts
196 lines (185 loc) · 6.69 KB
/
engine_youdao.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
import Engine from './engine';
import { Book, Word } from './data';
import _ = require('lodash');
import Config from './config';
const fetch = require('node-fetch');
const fs = require('fs');
const queryString = require('query-string');
const parser = require('fast-xml-parser');
const moment = require('moment');
const utils = require('./utils');
const { logger } = utils;
const Maimemo = require('./engine_maimemo');
const crypto = require('crypto');
const md5 = crypto.createHash('md5');
class Youdao extends Engine {
lookup(word: any): Promise<Word> {
throw new Error('Method not implemented.');
}
config: any;
constructor() {
super('有道', Config.youdao.cookie);
this.config = Config.youdao;
this.config.to_maimemo_bookid = _.defaultTo(
this.config.to_maimemo_bookid,
Config.maimemo.bookid,
);
// this.reqConfig = _.merge({}, this.reqConfig, {
// headers: {
// 'User-Agent': 'youdao 2.3.4 rv:161 (Macintosh; Mac OS X 10.14.2; zh_CN)',
// },
// });
// this.books = {};
// // 有道单词表验证时间戳
// this.maxServerTimestamp = 0;
// this.maxRemServerTimestamp = 0;
}
async fetchBookNames(): Promise<Array<Book>> {
let rep = await this.req.get(`http://dict.youdao.com/wordbook/webapi/books`);
return rep.data.data.map((v) => {
let bk = new Book();
bk.name = v.bookName;
bk.id = v.bookId;
return bk;
});
}
async fetchBookWords(book: Book): Promise<boolean> {
let offset = 0;
let limit = 100;
while (true) {
let rep = await this.req.get(
`http://dict.youdao.com/wordbook/webapi/words?limit=100&offset=${offset}&bookId=${book.id}`,
);
let items = rep.data.data.itemList;
_.map(items, (v) => {
let word = new Word();
word.name = v.word;
book.words[word.name] = word;
});
offset += limit;
// break;
if (items.length < limit) {
break;
}
}
return true;
}
// login() {
// if (_.size(this.config.username) > 0) {
// // 初始化cookie
// return this.req.get('http://fanyi.youdao.com/?keyfrom=dict2.index').then(() => {
// return this.req
// .get(
// `https://dict.youdao.com/login/acc/login?app=client&product=DICT&tp=urstoken&cf=7&show=true&format=json&` +
// `username=${this.config.username}&password=${md5
// .update(this.config.password)
// .digest('hex')}&um=true`,
// )
// .then((res) => {
// super.login();
// logger.info(this.name, '用户登录', res.username);
// });
// });
// }
// }
// getWords(page = 0, all = false) {
// let books = {};
// // 获取有道单词本
// //TODO 超过2000的时候怎么办?
// return this.req
// .post(
// 'http://dict.youdao.com/wordbook/api?keyfrom=mac.main&id=80:E6:50:00:E4:EE&model=MacBookPro11,2&deviceid=C02N62Q0G3QC&mid=Mac%20OS%20X%2010.13.6&requestNum=2000',
// {
// form: {
// version: 2,
// data: `<?xml version="1.0" encoding="utf-8" ?><request><type>words</type><operation>update</operation><maxLocalTimestamp>0</maxLocalTimestamp><maxRemLocalTimestamp>${new Date().getTime()}</maxRemLocalTimestamp></request>`,
// },
// },
// )
// .then((res) => {
// let jsonObj = parser.parse(res);
// // console.log(jsonObj)
// // 将单词分类到单词本
// jsonObj.response.actionlist.action.forEach((action) => {
// let book = books[action.tags];
// if (book == null) {
// book = new Book();
// book.name = action.tags;
// books[action.tags] = book;
// }
// book.addWord(action.word);
// });
// _.map(books, (v) => {
// logger.info(this.name, '单词本:', v.name, '单词量:', _.size(v.words));
// });
// // let myBook = books[Config.youdao.unchecktag]
// this.maxServerTimestamp = jsonObj.response.maxServerTimestamp;
// this.maxRemServerTimestamp = jsonObj.response.maxRemServerTimestamp;
// // buildMaimemo()
// return books;
// });
// }
/**
* 查询有道单词
* @param wordEn
* @returns {*}
*/
// getYoudaoWord(wordEn) {
// return this.req.get(
// `http://dict.youdao.com/jsonapi?q=${encodeURIComponent(
// wordEn,
// )}&doctype=json&keyfrom=mac.main&id=92EAC020C265501643DB359450E87E0E&vendor=cidian.youdao.com&appVer=2.3.3&client=macdict&jsonversion=2`,
// {
// json: true,
// },
// );
// }
// moveToTag(word, tag) {
// logger.info(this.name, '移动单词', word.name, '到', tag);
// let t = new Date().getTime();
// return this.req
// .post(
// `http://dict.youdao.com/wordbook/api?keyfrom=mac.main&id=80:E6:50:00:E4:EE&model=MacBookPro11,2&deviceid=C02N62Q0G3QC&mid=Mac%20OS%20X%2010.13.6&requestNum=2000`,
// {
// form: {
// data: `<?xml version="1.0" encoding="utf-8" ?><request><type>words</type><operation>commit</operation><maxLocalTimestamp>${
// this.maxServerTimestamp
// }</maxLocalTimestamp><maxRemLocalTimestamp>${
// youdaoBookInfo.maxRemServerTimestamp
// }</maxRemLocalTimestamp>
// <actionlist><action type="add"><word><![CDATA[${word.name}]]></word><wordInfo>
// <phonetic><![CDATA[]]></phonetic><trans><![CDATA[${word.getCn()}]]></trans>
// <tags><![CDATA[;${tag}]]></tags><addtime>${Math.round(t / 1000)}</addtime>
// <flag>1</flag></wordInfo></action></actionlist></request>`,
// version: 2,
// },
// },
// )
// .then((res) => {
// let jsonObj = parser.parse(res);
// this.maxServerTimestamp = jsonObj.response.maxServerTimestamp;
// this.maxRemServerTimestamp = jsonObj.response.maxRemServerTimestamp;
// // debugger
// });
// }
// async work() {
// this.books = await this.getWords();
// let res = await Maimemo.buildMaimemo(
// this.books[this.config.unchecktag],
// this.config.to_maimemo_bookid,
// );
// for (let i = 0; i < res.newwords_notin_maimemo.length; i++) {
// // 先要获取释义,再移动
// let word = res.newwords_notin_maimemo[i];
// await this.getYoudaoWord(word.name).then((res) => {
// word.youdaoData = res;
// // console.log(word.toMaimemo())
// });
// // 移动单词
// if (this.config.noresulttag) {
// await this.moveToTag(word, this.config.noresulttag);
// }
// }
// }
}
export default Youdao;