-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
88 lines (77 loc) · 2.26 KB
/
index.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
85
86
87
88
// ==Headers==
// @Name: index
// @Description: index
// @Version: 1.0.47
// @Author: dodying
// @Created: 2021-04-19 20:16:30
// @Modified: 2021-04-19 20:51:11
// @Namespace: https://github.com/dodying/Nodejs
// @SupportURL: https://github.com/dodying/Nodejs/issues
// @Require: null
// ==/Headers==
// 设置
const fs = require('fs');
const config = require('./config');
// 导入原生模块
// 导入第三方模块
const req = require('../_lib/req');
req.config.init(config.req);
require('../_lib/log').hack();
// Function
let database;
const doExit = (exit = true) => {
if (typeof database === 'object' && database instanceof Object) {
fs.writeFileSync('./database.json', JSON.stringify(database, null, 2));
}
if (exit) process.exit();
};
const errorHandle = (res) => {
console.log(res.json);
};
// Main
const main = async () => {
if (!fs.existsSync('./data')) fs.mkdirSync('./data');
if (fs.existsSync('./database.json')) {
database = fs.readFileSync('database.json', 'utf-8');
database = JSON.parse(database);
} else {
database = {};
}
setInterval(() => {
doExit(false);
}, 1000 * 60 * 10);
const res = await req(new URL('/api/medical/getList?pageNo=1&pageSize=10', config.origin).href);
if (!res || !res.json || res.json.msg !== 'success') return errorHandle(res);
let page = 1;
while (Object.keys(database).length < res.json.totalCount) {
const res1 = await req(`/api/medical/getList?pageNo=${page}&pageSize=100`);
if (!res1 || !res1.json || res1.json.msg !== 'success') return errorHandle(res1);
for (const i of res1.json.list) {
if (i.id in database) continue;
const res2 = await req(`/api/medical/${i.id}`);
if (!res2 || !res2.json || res2.json.msg !== 'success') {
errorHandle(res2);
continue;
}
fs.writeFileSync(`./data/${i.id}.json`, res2.body);
database[i.id] = res2.json.medical.comName;
}
page++;
doExit(false);
}
};
try {
process.once('SIGINT', () => {
doExit(false);
console.log('SIGINT');
process.exit();
});
main().then(async () => {
doExit();
}, async (err) => {
console.error(err);
doExit();
});
} catch (error) {
doExit();
}