pp-robot-module 0.2.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -0
- package/dist/MR/Ai.d.ts +8 -0
- package/dist/MR/Ai.js +24 -0
- package/dist/MR/Clipboard.d.ts +20 -0
- package/dist/MR/Clipboard.js +60 -0
- package/dist/MR/Csv.d.ts +20 -0
- package/dist/MR/Csv.js +56 -0
- package/dist/MR/Date.d.ts +60 -0
- package/dist/MR/Date.js +94 -0
- package/dist/MR/DbClient.d.ts +97 -0
- package/dist/MR/DbClient.js +269 -0
- package/dist/MR/Dialog.d.ts +0 -0
- package/dist/MR/Dialog.js +40 -0
- package/dist/MR/Excel.d.ts +110 -0
- package/dist/MR/Excel.js +316 -0
- package/dist/MR/File.d.ts +162 -0
- package/dist/MR/File.js +399 -0
- package/dist/MR/Hash.d.ts +12 -0
- package/dist/MR/Hash.js +21 -0
- package/dist/MR/Html.d.ts +38 -0
- package/dist/MR/Html.js +163 -0
- package/dist/MR/Http.d.ts +33 -0
- package/dist/MR/Http.js +121 -0
- package/dist/MR/Imap.d.ts +15 -0
- package/dist/MR/Imap.js +52 -0
- package/dist/MR/Ini.d.ts +31 -0
- package/dist/MR/Ini.js +55 -0
- package/dist/MR/Keyboard.d.ts +59 -0
- package/dist/MR/Keyboard.js +515 -0
- package/dist/MR/Log.d.ts +7 -0
- package/dist/MR/Log.js +52 -0
- package/dist/MR/Mouse.d.ts +51 -0
- package/dist/MR/Mouse.js +152 -0
- package/dist/MR/Net.d.ts +17 -0
- package/dist/MR/Net.js +74 -0
- package/dist/MR/Pdf.d.ts +113 -0
- package/dist/MR/Pdf.js +197 -0
- package/dist/MR/Process.d.ts +46 -0
- package/dist/MR/Process.js +106 -0
- package/dist/MR/QRcode.d.ts +13 -0
- package/dist/MR/QRcode.js +37 -0
- package/dist/MR/RedisClient.d.ts +40 -0
- package/dist/MR/RedisClient.js +183 -0
- package/dist/MR/Regex.d.ts +16 -0
- package/dist/MR/Regex.js +20 -0
- package/dist/MR/Robot.d.ts +19 -0
- package/dist/MR/Robot.js +31 -0
- package/dist/MR/Screen.d.ts +20 -0
- package/dist/MR/Screen.js +79 -0
- package/dist/MR/Smtp.d.ts +21 -0
- package/dist/MR/Smtp.js +83 -0
- package/dist/MR/String.d.ts +104 -0
- package/dist/MR/String.js +166 -0
- package/dist/MR/System.d.ts +31 -0
- package/dist/MR/System.js +201 -0
- package/dist/MR/Validator.d.ts +30 -0
- package/dist/MR/Validator.js +104 -0
- package/dist/MR/WebBrowser.d.ts +210 -0
- package/dist/MR/WebBrowser.js +484 -0
- package/dist/MR/WebBrowser2.d.ts +0 -0
- package/dist/MR/WebBrowser2.js +504 -0
- package/dist/MR/WebElement.d.ts +123 -0
- package/dist/MR/WebElement.js +172 -0
- package/dist/MR/WebKeyboard.d.ts +21 -0
- package/dist/MR/WebKeyboard.js +48 -0
- package/dist/MR/WebMouse.d.ts +46 -0
- package/dist/MR/WebMouse.js +79 -0
- package/dist/MR/Window.d.ts +8 -0
- package/dist/MR/Window.js +34 -0
- package/dist/MR/Word.d.ts +56 -0
- package/dist/MR/Word.js +220 -0
- package/dist/MR/Zip.d.ts +25 -0
- package/dist/MR/Zip.js +89 -0
- package/dist/bin/inputting-method.exe +0 -0
- package/dist/bin/keyboard-password.exe +0 -0
- package/dist/bin/lock-window.exe +0 -0
- package/dist/bin/printer-pdf.exe +0 -0
- package/dist/dotenv.d.ts +0 -0
- package/dist/dotenv.js +2 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.js +63 -0
- package/dist/util.d.ts +17 -0
- package/dist/util.js +41 -0
- package/package.json +120 -0
package/dist/MR/File.js
ADDED
@@ -0,0 +1,399 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
+
});
|
10
|
+
};
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
12
|
+
exports.MR = void 0;
|
13
|
+
const path = require("path");
|
14
|
+
const fs = require("fs");
|
15
|
+
const Log_1 = require("./Log");
|
16
|
+
const fse = require('fs-extra');
|
17
|
+
const iconvLite = require('iconv-lite');
|
18
|
+
var SortType;
|
19
|
+
(function (SortType) {
|
20
|
+
SortType["Name"] = "name";
|
21
|
+
SortType["Birthtime"] = "birthtime";
|
22
|
+
SortType["Ctime"] = "ctime";
|
23
|
+
SortType["Mtime"] = "mtime";
|
24
|
+
SortType["Size"] = "size";
|
25
|
+
})(SortType || (SortType = {}));
|
26
|
+
var OrderBy;
|
27
|
+
(function (OrderBy) {
|
28
|
+
OrderBy[OrderBy["ASC"] = 0] = "ASC";
|
29
|
+
OrderBy[OrderBy["DESC"] = 1] = "DESC";
|
30
|
+
})(OrderBy || (OrderBy = {}));
|
31
|
+
var MR;
|
32
|
+
(function (MR) {
|
33
|
+
class File {
|
34
|
+
constructor() { }
|
35
|
+
// 目标文件夹不存在的话, 会自动创建
|
36
|
+
static copy(params) {
|
37
|
+
return __awaiter(this, void 0, void 0, function* () {
|
38
|
+
Log_1.default.debug("File.copy", params);
|
39
|
+
try {
|
40
|
+
return yield fse.copy(params._srcFile, params._destFile);
|
41
|
+
}
|
42
|
+
catch (err) {
|
43
|
+
console.error(err);
|
44
|
+
return false;
|
45
|
+
}
|
46
|
+
});
|
47
|
+
}
|
48
|
+
// 如果文件夹不存在, 会创建空文件夹
|
49
|
+
// 文件夹中含有其他文件或文件夹时会清空所有
|
50
|
+
// 创建时返回文件夹绝对路径
|
51
|
+
static emptyDir(params) {
|
52
|
+
return __awaiter(this, void 0, void 0, function* () {
|
53
|
+
Log_1.default.debug("File.emptyDir", params);
|
54
|
+
try {
|
55
|
+
yield fse.emptyDir(params._dir);
|
56
|
+
return true;
|
57
|
+
}
|
58
|
+
catch (err) {
|
59
|
+
return false;
|
60
|
+
}
|
61
|
+
});
|
62
|
+
}
|
63
|
+
// 文件不存在则创建空文件, 存在时不做修改
|
64
|
+
// 永远返回undefined
|
65
|
+
static ensureFile(params) {
|
66
|
+
return __awaiter(this, void 0, void 0, function* () {
|
67
|
+
Log_1.default.debug("File.ensureFile", params);
|
68
|
+
try {
|
69
|
+
return yield fse.ensureFile(params._filename);
|
70
|
+
}
|
71
|
+
catch (err) {
|
72
|
+
return false;
|
73
|
+
}
|
74
|
+
});
|
75
|
+
}
|
76
|
+
// 文件夹如果存在, 不做修改, 返回null
|
77
|
+
// 不存在时创建, 并返回文件夹绝对路径
|
78
|
+
static ensureDir(params) {
|
79
|
+
return __awaiter(this, void 0, void 0, function* () {
|
80
|
+
Log_1.default.debug("File.ensureDir", params);
|
81
|
+
try {
|
82
|
+
yield fse.ensureDir(params._dir, params._options);
|
83
|
+
console.log('success!');
|
84
|
+
return true;
|
85
|
+
}
|
86
|
+
catch (err) {
|
87
|
+
console.error(err);
|
88
|
+
return false;
|
89
|
+
}
|
90
|
+
});
|
91
|
+
}
|
92
|
+
// 创建返回绝对路径
|
93
|
+
// 已经存在返回null
|
94
|
+
static mkdirp(params) {
|
95
|
+
return __awaiter(this, void 0, void 0, function* () {
|
96
|
+
Log_1.default.debug("File.mkdirp", params);
|
97
|
+
try {
|
98
|
+
return yield fse.mkdirp(params._dir, params._options);
|
99
|
+
}
|
100
|
+
catch (err) {
|
101
|
+
console.error(err);
|
102
|
+
return false;
|
103
|
+
}
|
104
|
+
});
|
105
|
+
}
|
106
|
+
// 移动文件夹或文件, 源必须存在
|
107
|
+
// 无返回值
|
108
|
+
static move(params) {
|
109
|
+
return __awaiter(this, void 0, void 0, function* () {
|
110
|
+
Log_1.default.debug("File.move", params);
|
111
|
+
try {
|
112
|
+
yield fse.move(params._srcFile, params._destFile, { overwrite: true });
|
113
|
+
console.log('success!');
|
114
|
+
}
|
115
|
+
catch (err) {
|
116
|
+
console.error(err);
|
117
|
+
}
|
118
|
+
});
|
119
|
+
}
|
120
|
+
// 文件夹或文件路径是否存在
|
121
|
+
static pathExists(params) {
|
122
|
+
return __awaiter(this, void 0, void 0, function* () {
|
123
|
+
Log_1.default.debug("File.pathExists", params);
|
124
|
+
const exists = yield fse.pathExists(params._filename);
|
125
|
+
return exists;
|
126
|
+
});
|
127
|
+
}
|
128
|
+
// 写
|
129
|
+
static outputFile(params) {
|
130
|
+
return __awaiter(this, void 0, void 0, function* () {
|
131
|
+
Log_1.default.debug("File.outputFile", [params._filename, params._encoding]);
|
132
|
+
try {
|
133
|
+
let _opt = {};
|
134
|
+
if (params._encoding)
|
135
|
+
_opt = { encoding: params._encoding };
|
136
|
+
return yield fse.outputFile(params._filename, params._data, _opt);
|
137
|
+
}
|
138
|
+
catch (err) {
|
139
|
+
console.error(err);
|
140
|
+
return false;
|
141
|
+
}
|
142
|
+
});
|
143
|
+
}
|
144
|
+
static readFile(params) {
|
145
|
+
return __awaiter(this, void 0, void 0, function* () {
|
146
|
+
Log_1.default.debug("File.readFile", [params._filename, params._encoding]);
|
147
|
+
try {
|
148
|
+
let data = yield fse.readFile(params._filename);
|
149
|
+
if (params._encoding) {
|
150
|
+
data = iconvLite.decode(data, params._encoding);
|
151
|
+
}
|
152
|
+
return data;
|
153
|
+
}
|
154
|
+
catch (err) {
|
155
|
+
console.error(err);
|
156
|
+
}
|
157
|
+
});
|
158
|
+
}
|
159
|
+
// 写json
|
160
|
+
static outputJson(params) {
|
161
|
+
return __awaiter(this, void 0, void 0, function* () {
|
162
|
+
Log_1.default.debug("File.outputJson", params._filename);
|
163
|
+
try {
|
164
|
+
let _opt = {};
|
165
|
+
if (params._encoding)
|
166
|
+
_opt = { encoding: params._encoding };
|
167
|
+
yield fse.outputJson(params._filename, params._data, _opt);
|
168
|
+
}
|
169
|
+
catch (err) {
|
170
|
+
console.error(err);
|
171
|
+
}
|
172
|
+
});
|
173
|
+
}
|
174
|
+
//读 json
|
175
|
+
static readJson(params) {
|
176
|
+
return __awaiter(this, void 0, void 0, function* () {
|
177
|
+
Log_1.default.debug("File.readJson", params);
|
178
|
+
try {
|
179
|
+
let data = yield fse.readJson(params._filename);
|
180
|
+
if (params._encoding) {
|
181
|
+
data = iconvLite.decode(data, params._encoding);
|
182
|
+
}
|
183
|
+
return data;
|
184
|
+
}
|
185
|
+
catch (err) {
|
186
|
+
console.error(err);
|
187
|
+
}
|
188
|
+
});
|
189
|
+
}
|
190
|
+
// 无返回值, 移除文件夹或文件, 递归式
|
191
|
+
static remove(params) {
|
192
|
+
return __awaiter(this, void 0, void 0, function* () {
|
193
|
+
Log_1.default.debug("File.remove", params);
|
194
|
+
try {
|
195
|
+
if (File.pathExists({ _filename: params._filename }))
|
196
|
+
return yield fse.remove(params._filename);
|
197
|
+
}
|
198
|
+
catch (err) {
|
199
|
+
console.error(err);
|
200
|
+
}
|
201
|
+
});
|
202
|
+
}
|
203
|
+
static detail(params) {
|
204
|
+
return __awaiter(this, void 0, void 0, function* () {
|
205
|
+
Log_1.default.debug("File.detail", params);
|
206
|
+
try {
|
207
|
+
const stats = yield fs.promises.stat(params._filename);
|
208
|
+
const fileInfo = path.parse(params._filename);
|
209
|
+
const fileDetails = {
|
210
|
+
isFile: stats.isFile(),
|
211
|
+
isDirectory: stats.isDirectory(),
|
212
|
+
sizeForamt: this.formatFileSize(stats.size),
|
213
|
+
size: stats.size,
|
214
|
+
birthtime: stats.birthtime,
|
215
|
+
modifytime: stats.mtime,
|
216
|
+
filename: fileInfo.name,
|
217
|
+
extname: fileInfo.ext,
|
218
|
+
dirname: fileInfo.dir,
|
219
|
+
fullpath: path.resolve(params._filename),
|
220
|
+
};
|
221
|
+
return fileDetails;
|
222
|
+
}
|
223
|
+
catch (err) {
|
224
|
+
console.error(err);
|
225
|
+
throw err;
|
226
|
+
}
|
227
|
+
});
|
228
|
+
}
|
229
|
+
static folderSize(params) {
|
230
|
+
return __awaiter(this, void 0, void 0, function* () {
|
231
|
+
Log_1.default.debug("File.folderSize", params);
|
232
|
+
try {
|
233
|
+
const folderSize = this.getFolderSize(params._dir);
|
234
|
+
return folderSize;
|
235
|
+
}
|
236
|
+
catch (err) {
|
237
|
+
console.error(err);
|
238
|
+
throw err;
|
239
|
+
}
|
240
|
+
});
|
241
|
+
}
|
242
|
+
static getFolderSize(folderPath) {
|
243
|
+
let totalSize = 0;
|
244
|
+
function calculateSize(filePath) {
|
245
|
+
const stats = fs.statSync(filePath);
|
246
|
+
if (stats.isDirectory()) {
|
247
|
+
const subFiles = fs.readdirSync(filePath);
|
248
|
+
subFiles.forEach((subFile) => {
|
249
|
+
calculateSize(path.join(filePath, subFile));
|
250
|
+
});
|
251
|
+
}
|
252
|
+
else if (stats.isFile()) {
|
253
|
+
totalSize += stats.size;
|
254
|
+
}
|
255
|
+
}
|
256
|
+
calculateSize(folderPath);
|
257
|
+
// 将文件夹大小转换为人类可读的格式
|
258
|
+
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
259
|
+
let size = totalSize;
|
260
|
+
let unitIndex = 0;
|
261
|
+
while (size >= 1024 && unitIndex < units.length - 1) {
|
262
|
+
size /= 1024;
|
263
|
+
unitIndex++;
|
264
|
+
}
|
265
|
+
const formattedSize = size.toFixed(2) + ' ' + units[unitIndex];
|
266
|
+
return formattedSize;
|
267
|
+
}
|
268
|
+
static getLastedFile(params) {
|
269
|
+
return __awaiter(this, void 0, void 0, function* () {
|
270
|
+
Log_1.default.debug("File.getLastedFile", params);
|
271
|
+
try {
|
272
|
+
const newestFile = this.getNewestFileInDirectory(params._dir);
|
273
|
+
return newestFile;
|
274
|
+
}
|
275
|
+
catch (err) {
|
276
|
+
console.error(err);
|
277
|
+
throw err;
|
278
|
+
}
|
279
|
+
});
|
280
|
+
}
|
281
|
+
static getNewestFileInDirectory(directoryPath) {
|
282
|
+
const files = fs.readdirSync(directoryPath);
|
283
|
+
let newestFile = null;
|
284
|
+
let newestDate = null;
|
285
|
+
files.forEach((file) => {
|
286
|
+
const filePath = path.join(directoryPath, file);
|
287
|
+
const stats = fs.statSync(filePath);
|
288
|
+
if (stats.isFile()) {
|
289
|
+
const fileDate = stats.mtime;
|
290
|
+
if (!newestDate || fileDate > newestDate) {
|
291
|
+
newestDate = fileDate;
|
292
|
+
newestFile = file;
|
293
|
+
}
|
294
|
+
}
|
295
|
+
});
|
296
|
+
return newestFile;
|
297
|
+
}
|
298
|
+
/**
|
299
|
+
* Change the name or location of a file or directory.
|
300
|
+
* @param params
|
301
|
+
*/
|
302
|
+
static rename(params) {
|
303
|
+
Log_1.default.debug("File.rename", params);
|
304
|
+
if (File.pathExists({ _filename: params._oldFile }))
|
305
|
+
return fs.renameSync(params._oldFile, params._newFile);
|
306
|
+
}
|
307
|
+
/**
|
308
|
+
* Write data to a file, replacing the file if it already exists.
|
309
|
+
* @param params
|
310
|
+
*/
|
311
|
+
static write(params) {
|
312
|
+
Log_1.default.debug("File.write", params._filename);
|
313
|
+
fs.writeFileSync(params._filename, params._data);
|
314
|
+
}
|
315
|
+
/**
|
316
|
+
* Create a directory.
|
317
|
+
* @param params
|
318
|
+
*/
|
319
|
+
static makeDir(params) {
|
320
|
+
Log_1.default.debug("File.makeDir", params);
|
321
|
+
return fs.mkdirSync(params._dir);
|
322
|
+
}
|
323
|
+
/**
|
324
|
+
* Get file status.
|
325
|
+
* @param params
|
326
|
+
*/
|
327
|
+
static getStats(params) {
|
328
|
+
Log_1.default.debug("File.getStats", params);
|
329
|
+
return fs.statSync(params._filename);
|
330
|
+
}
|
331
|
+
/**
|
332
|
+
* Lists files or directories in a directory.
|
333
|
+
* @param params
|
334
|
+
*/
|
335
|
+
static list(params) {
|
336
|
+
Log_1.default.debug("File.list", params);
|
337
|
+
const fileList = fs.readdirSync(params._dir);
|
338
|
+
if (params && params.sortType) {
|
339
|
+
Log_1.default.debug("File.list->sort", { params });
|
340
|
+
if (params.sortType === SortType.Name) {
|
341
|
+
fileList.sort(new Intl.Collator(undefined, { numeric: true, sensitivity: "base" })
|
342
|
+
.compare);
|
343
|
+
if (params.orderBy === OrderBy.DESC) {
|
344
|
+
fileList.reverse();
|
345
|
+
}
|
346
|
+
}
|
347
|
+
else {
|
348
|
+
return fileList
|
349
|
+
.map(filename => {
|
350
|
+
return {
|
351
|
+
filename,
|
352
|
+
value: this.getStats({ _filename: filename })[params.sortType]
|
353
|
+
};
|
354
|
+
})
|
355
|
+
.sort((a, b) => params.orderBy === OrderBy.DESC
|
356
|
+
? b.value - a.value
|
357
|
+
: a.value - b.value)
|
358
|
+
.map(v => v.filename);
|
359
|
+
}
|
360
|
+
}
|
361
|
+
return fileList;
|
362
|
+
}
|
363
|
+
/**
|
364
|
+
* Lists only files in a directory.
|
365
|
+
* @param params
|
366
|
+
*/
|
367
|
+
static listFiles(params) {
|
368
|
+
const dirname = (params && params._dir) || "./";
|
369
|
+
Log_1.default.debug("File.listFiles", { dirname });
|
370
|
+
return File.list(params).filter((filename) => File.isFile({ _filename: path.join(dirname, filename) }));
|
371
|
+
}
|
372
|
+
/**
|
373
|
+
* Tests whether the given path is a file.
|
374
|
+
* @param params
|
375
|
+
*/
|
376
|
+
static isFile(params) {
|
377
|
+
Log_1.default.debug("File.isFile", params);
|
378
|
+
return File.pathExists({ _filename: params._filename }) && fs.lstatSync(params._filename).isFile();
|
379
|
+
}
|
380
|
+
static isDir(params) {
|
381
|
+
Log_1.default.debug("File.isDir", params);
|
382
|
+
return File.pathExists({ _filename: params._filename }) && fs.lstatSync(params._filename).isDirectory();
|
383
|
+
}
|
384
|
+
}
|
385
|
+
File.SortType = SortType;
|
386
|
+
File.OrderBy = OrderBy;
|
387
|
+
// 转换文件大小为可读格式(例如:KB、MB、GB)
|
388
|
+
File.formatFileSize = (bytes) => {
|
389
|
+
if (bytes === 0) {
|
390
|
+
return '0 Bytes';
|
391
|
+
}
|
392
|
+
const k = 1024;
|
393
|
+
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
|
394
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
395
|
+
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
|
396
|
+
};
|
397
|
+
MR.File = File;
|
398
|
+
})(MR = exports.MR || (exports.MR = {}));
|
399
|
+
exports.default = MR.File;
|
package/dist/MR/Hash.js
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.MR = void 0;
|
4
|
+
const forge = require("node-forge");
|
5
|
+
var MR;
|
6
|
+
(function (MR) {
|
7
|
+
class Hash {
|
8
|
+
constructor() { } // eslint-disable-line no-useless-constructor, no-empty-function
|
9
|
+
/**
|
10
|
+
* Calculate the md5 hash of a string.
|
11
|
+
* @param msg
|
12
|
+
*/
|
13
|
+
static md5(msg) {
|
14
|
+
const md = forge.md.md5.create();
|
15
|
+
md.update(msg);
|
16
|
+
return md.digest().toHex();
|
17
|
+
}
|
18
|
+
}
|
19
|
+
MR.Hash = Hash;
|
20
|
+
})(MR = exports.MR || (exports.MR = {}));
|
21
|
+
exports.default = MR.Hash;
|
@@ -0,0 +1,38 @@
|
|
1
|
+
export declare namespace MR {
|
2
|
+
class Html {
|
3
|
+
private constructor();
|
4
|
+
static extract(params: {
|
5
|
+
_html: string;
|
6
|
+
_json?: any;
|
7
|
+
}): Promise<any>;
|
8
|
+
static image(params: {
|
9
|
+
_html: string;
|
10
|
+
}): Promise<any[]>;
|
11
|
+
static link(params: {
|
12
|
+
_html: string;
|
13
|
+
}): Promise<any[]>;
|
14
|
+
static text(params: {
|
15
|
+
_html: string;
|
16
|
+
}): Promise<any>;
|
17
|
+
static element(params: {
|
18
|
+
_html: string;
|
19
|
+
_selector: string;
|
20
|
+
}): any;
|
21
|
+
static table(params: {
|
22
|
+
_html: string;
|
23
|
+
_selector?: string;
|
24
|
+
}): any;
|
25
|
+
static struct(params: {
|
26
|
+
_html: string;
|
27
|
+
_selector: string;
|
28
|
+
}): any[];
|
29
|
+
static structOne(params: {
|
30
|
+
_html: string;
|
31
|
+
_selector: string;
|
32
|
+
}): any[];
|
33
|
+
private static stringifyValue;
|
34
|
+
private static flattenObjectValues;
|
35
|
+
}
|
36
|
+
}
|
37
|
+
declare const _default: typeof MR.Html;
|
38
|
+
export default _default;
|
package/dist/MR/Html.js
ADDED
@@ -0,0 +1,163 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
+
});
|
10
|
+
};
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
12
|
+
exports.MR = void 0;
|
13
|
+
const Log_1 = require("./Log");
|
14
|
+
const htmlparser2 = require('htmlparser2');
|
15
|
+
const cheerio = require('cheerio');
|
16
|
+
const tabletojson = require('tabletojson').Tabletojson;
|
17
|
+
//const tableify = require('tableify');
|
18
|
+
var MR;
|
19
|
+
(function (MR) {
|
20
|
+
class Html {
|
21
|
+
constructor() { }
|
22
|
+
//TODO
|
23
|
+
static extract(params) {
|
24
|
+
return __awaiter(this, void 0, void 0, function* () {
|
25
|
+
Log_1.default.debug("Html.extract", params);
|
26
|
+
const handler = new htmlparser2.DomHandler();
|
27
|
+
const parser = new htmlparser2.Parser(handler);
|
28
|
+
parser.write(params._html);
|
29
|
+
parser.end();
|
30
|
+
const $ = cheerio.load(params._html);
|
31
|
+
const data = $.extract({
|
32
|
+
red: '.red',
|
33
|
+
});
|
34
|
+
return data;
|
35
|
+
});
|
36
|
+
}
|
37
|
+
static image(params) {
|
38
|
+
return __awaiter(this, void 0, void 0, function* () {
|
39
|
+
Log_1.default.debug("Html.image", params);
|
40
|
+
const result = [];
|
41
|
+
const parser = new htmlparser2.Parser({
|
42
|
+
onopentag: (tagName, attrs) => {
|
43
|
+
if (tagName === 'img' && attrs.src) {
|
44
|
+
const src = attrs.src;
|
45
|
+
result.push(src);
|
46
|
+
}
|
47
|
+
}
|
48
|
+
});
|
49
|
+
parser.write(params._html);
|
50
|
+
parser.end();
|
51
|
+
return result;
|
52
|
+
});
|
53
|
+
}
|
54
|
+
static link(params) {
|
55
|
+
return __awaiter(this, void 0, void 0, function* () {
|
56
|
+
Log_1.default.debug("Html.a", params);
|
57
|
+
const result = [];
|
58
|
+
const parser = new htmlparser2.Parser({
|
59
|
+
onopentag: (tagName, attrs) => {
|
60
|
+
if (tagName === 'a' && attrs.href) {
|
61
|
+
const src = attrs.href;
|
62
|
+
result.push(src);
|
63
|
+
}
|
64
|
+
}
|
65
|
+
});
|
66
|
+
parser.write(params._html);
|
67
|
+
parser.end();
|
68
|
+
return result;
|
69
|
+
});
|
70
|
+
}
|
71
|
+
static text(params) {
|
72
|
+
return __awaiter(this, void 0, void 0, function* () {
|
73
|
+
Log_1.default.debug("Html.text", params);
|
74
|
+
const handler = new htmlparser2.DomHandler();
|
75
|
+
const parser = new htmlparser2.Parser(handler);
|
76
|
+
parser.write(params._html);
|
77
|
+
parser.end();
|
78
|
+
return htmlparser2.DomUtils.textContent(handler.root.childNodes);
|
79
|
+
});
|
80
|
+
}
|
81
|
+
// 通过返回值可以进行 ele.text(); ele.attr('class'); ele.html();
|
82
|
+
static element(params) {
|
83
|
+
Log_1.default.debug("Html.element", params);
|
84
|
+
const $ = cheerio.load(params._html);
|
85
|
+
const ele = $(params._selector);
|
86
|
+
return ele;
|
87
|
+
}
|
88
|
+
static table(params) {
|
89
|
+
Log_1.default.debug("Html.table", params);
|
90
|
+
const $ = cheerio.load(params._html);
|
91
|
+
let tableHtml = undefined;
|
92
|
+
if (params._selector) {
|
93
|
+
tableHtml = $(params._selector).html();
|
94
|
+
}
|
95
|
+
else {
|
96
|
+
tableHtml = $('table').html();
|
97
|
+
}
|
98
|
+
let json = undefined;
|
99
|
+
if (tableHtml) {
|
100
|
+
json = tabletojson.convert(`<table>${tableHtml}</table>`, { useFirstRowForHeadings: true });
|
101
|
+
}
|
102
|
+
return json;
|
103
|
+
}
|
104
|
+
static struct(params) {
|
105
|
+
const $ = cheerio.load(params._html);
|
106
|
+
const lis = $(params._selector);
|
107
|
+
const result = [];
|
108
|
+
lis.each((i, li) => {
|
109
|
+
const oneResult = this.structOne({ _html: $.html(li), _selector: params._selector });
|
110
|
+
result.push(oneResult);
|
111
|
+
});
|
112
|
+
return result;
|
113
|
+
}
|
114
|
+
static structOne(params) {
|
115
|
+
Log_1.default.debug("Html.struct", params);
|
116
|
+
const $ = cheerio.load(params._html);
|
117
|
+
const result = [];
|
118
|
+
$(params._selector).each((i, el) => {
|
119
|
+
const item = {};
|
120
|
+
$(el).find('*').each((j, subEl) => {
|
121
|
+
if (subEl.attribs && Object.keys(subEl.attribs).length > 0) {
|
122
|
+
const attrs = Object.fromEntries(Object.entries(subEl.attribs).filter(([key]) => !key.startsWith('class')));
|
123
|
+
if (Object.keys(attrs).length > 0) {
|
124
|
+
item[j] = attrs;
|
125
|
+
}
|
126
|
+
item[String(j) + '_text'] = $(subEl).text();
|
127
|
+
}
|
128
|
+
else {
|
129
|
+
item[j] = $(subEl).text().trim();
|
130
|
+
}
|
131
|
+
});
|
132
|
+
result.push(item);
|
133
|
+
});
|
134
|
+
let uniqueValues = [];
|
135
|
+
if (result.length > 0) {
|
136
|
+
const flatValues = result.flatMap(item => Object.values(item).flatMap(value => this.flattenObjectValues(value)));
|
137
|
+
if (flatValues && flatValues.length > 0) {
|
138
|
+
uniqueValues = Array.from(new Set(flatValues));
|
139
|
+
}
|
140
|
+
}
|
141
|
+
//console.log(uniqueValues);
|
142
|
+
return uniqueValues;
|
143
|
+
}
|
144
|
+
static stringifyValue(value) {
|
145
|
+
if (typeof value === 'string') {
|
146
|
+
return value;
|
147
|
+
}
|
148
|
+
else {
|
149
|
+
return JSON.stringify(value);
|
150
|
+
}
|
151
|
+
}
|
152
|
+
static flattenObjectValues(value) {
|
153
|
+
if (typeof value === 'object' && value !== null) {
|
154
|
+
return Object.values(value).flatMap(this.flattenObjectValues);
|
155
|
+
}
|
156
|
+
else {
|
157
|
+
return [String(value)];
|
158
|
+
}
|
159
|
+
}
|
160
|
+
}
|
161
|
+
MR.Html = Html;
|
162
|
+
})(MR = exports.MR || (exports.MR = {}));
|
163
|
+
exports.default = MR.Html;
|
@@ -0,0 +1,33 @@
|
|
1
|
+
export declare namespace MR {
|
2
|
+
class Http {
|
3
|
+
private constructor();
|
4
|
+
private static addContentType;
|
5
|
+
static getUserAgent(): any;
|
6
|
+
private static addUserAgent;
|
7
|
+
static get(params: {
|
8
|
+
_url: string;
|
9
|
+
_headers?: {};
|
10
|
+
}): Promise<string>;
|
11
|
+
static post(params: {
|
12
|
+
_url: string;
|
13
|
+
_body?: {};
|
14
|
+
_headers?: {};
|
15
|
+
}): Promise<string>;
|
16
|
+
static form(params: {
|
17
|
+
_url: string;
|
18
|
+
_method?: 'post' | 'get';
|
19
|
+
_body?: {};
|
20
|
+
_headers?: {};
|
21
|
+
}): Promise<string>;
|
22
|
+
static parseCookies(params: {
|
23
|
+
_response: any;
|
24
|
+
}): Promise<any>;
|
25
|
+
static download(params: {
|
26
|
+
_url: string;
|
27
|
+
_filename: string;
|
28
|
+
_headers?: {};
|
29
|
+
}): Promise<void>;
|
30
|
+
}
|
31
|
+
}
|
32
|
+
declare const _default: typeof MR.Http;
|
33
|
+
export default _default;
|