-
Notifications
You must be signed in to change notification settings - Fork 119
/
baiduLinkSubmit.js
142 lines (133 loc) · 3.43 KB
/
baiduLinkSubmit.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
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
const http = require('http');
const fs = require('fs');
const readline = require('readline');
const https = require('https');
if (!process.env.BAIDU_LINK_SUBMIT_TOKEN) {
throw new Error('no baidu link submit token configured');
}
/**
* get old site.txt from official site
*/
const getOldSiteData = () => {
return new Promise((resolve, reject) => {
https
.get('https://seata.apache.org/site.txt', (res) => {
res.setEncoding('utf-8');
if (res.statusCode === 200) {
res.on('data', (data) => {
resolve(data.split('\n'));
});
} else {
res.on('data', function (chunk) {
reject(
`get old site.txt from official site error,statusCode: ${res.statusCode},body:${chunk}`
);
});
}
})
.on('error', (e) => {
reject(e);
});
});
};
/**
* get the new generated local site.txt
*/
const getNewData = () => {
return new Promise((resolve, reject) => {
const data = [];
try {
const rl = readline.createInterface({
input: fs.createReadStream('sitemaps/site.txt'),
crlfDelay: Infinity,
});
rl.on('line', (line) => {
data.push(line);
}).on('close', () => {
resolve(data);
});
} catch (err) {
reject(err);
}
});
};
/**
* compute added data
*/
const computeAddedData = (oldData, newData) => {
const addedData = [];
newData.forEach((element) => {
if (!oldData.includes(element)) {
addedData.push(element);
}
});
return addedData;
};
const submit = (data) => {
return new Promise((resolve, reject) => {
var options = {
host: 'data.zz.baidu.com',
port: 80,
path:
'/urls?site=https://seata.apache.org&token=' +
process.env.BAIDU_LINK_SUBMIT_TOKEN,
method: 'POST',
headers: {
'Content-Type': 'text/plain',
},
};
var req = http.request(options, function (res) {
if (res.statusCode == 200) {
res.on('data', function (chunk) {
resolve(chunk);
});
} else {
res.on('data', function (chunk) {
reject(
`baidu link submit error,statusCode: ${res.statusCode},body:${chunk}`
);
});
}
});
req.on('error', function (e) {
reject('baidu link submit request error: ' + e.message);
});
// write data to request body
data.forEach((item) => {
req.write(item + '\n');
});
req.end();
});
};
getOldSiteData()
.then((oldData) => {
getNewData()
.then(async (newData) => {
try {
const addedData = computeAddedData(oldData, newData);
// baidu limits up to 2,000 entries at a time
while (addedData.length > 2000) {
// delete and intercept
const data = addedData.splice(0, 2000);
const result = await submit(data);
console.log(result);
if (result.remain < addedData.length) {
throw new Error('baidu link submit over quota');
}
}
// submission of the remaining data of less than 2,000 entries
if (addedData.length > 0) {
const result = await submit(addedData);
console.log(result);
}
} catch (err) {
console.error(err);
}
})
.catch((err) => {
console.error(err);
});
})
.catch((err) => {
console.error(err);
});