-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.js
60 lines (47 loc) · 1.76 KB
/
main.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
import puppeteer from 'puppeteer';
import { exit } from 'process';
import fs from 'fs';
import path from 'path';
import { autoLogin } from './src/login.js';
import { getAllBooks } from './src/toc.js';
import { exportMarkDownFiles } from './src/export.js';
// import { printDirectoryTree } from './src/toc.js';
let color = {
byNum: (mess, fgNum) => {
mess = mess || '';
fgNum = fgNum === undefined ? 31 : fgNum;
return '\u001b[' + fgNum + 'm' + mess + '\u001b[39m';
},
black: (mess) => color.byNum(mess, 30),
red: (mess) => color.byNum(mess, 31),
green: (mess) => color.byNum(mess, 32),
yellow: (mess) => color.byNum(mess, 33),
blue: (mess) => color.byNum(mess, 34),
magenta: (mess) => color.byNum(mess, 35),
cyan: (mess) => color.byNum(mess, 36),
white: (mess) => color.byNum(mess, 37)
};
async function run() {
if (!process.env.EXPORT_PATH) {
const outputDir = path.join(process.cwd(), 'output');
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
process.env.EXPORT_PATH = outputDir;
console.log(`The environment variable EXPORT_PATH is not set, so the default ${outputDir} is used as the export path.`)
}
// const page = await BrowserPage.getInstance();
const browser = await puppeteer.launch({ headless: true }); // true:not show browser
const page = await browser.newPage();
// 检查是否存在 cookie 文件
await autoLogin(page)
console.log(color.green("Login successfully!"))
console.log()
console.log("Get book stacks ...")
const books = await getAllBooks(page)
// console.log(books)
console.log("Start export all books ...")
await exportMarkDownFiles(page, books)
browser.close()
};
run();