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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
| const fs = require('fs'); const path = require('path'); const url = require('url');
const request = require('request'); const xmlParser = require('xml-parser'); const md5 = require('md5');
const config = { username: 'toimc', repo: "toimc.github.io", token: 'xxxxxx', sitemap: path.join(__dirname, './public/sitemap.xml'), cache: true, gitalkCacheFile: path.join(__dirname, './gitalk-init-cache.json'), gitalkErrorFile: path.join(__dirname, './gitalk-init-error.json'), };
const api = 'https://api.github.com/repos/' + config.username + '/' + config.repo + '/issues';
const sitemapXmlReader = (file) => { try { const data = fs.readFileSync(file, 'utf8'); const sitemap = xmlParser(data); let ret = []; sitemap.root.children.forEach(function (url) { const loc = url.children.find(function (item) { return item.name === 'loc'; }); if (!loc) { return false; } const title = url.children.find(function (item) { return item.name === 'title'; }); const desc = url.children.find(function (item) { return item.name === 'desc'; }); const date = url.children.find(function (item) { return item.name === 'date'; }); ret.push({ url: loc.content, title: title.content, desc: desc.content, date: date.content, }); }); return ret; } catch (e) { return []; } };
const getGitalkId = ({ url: u, date }) => { const link = url.parse(u); if (!link || !link.pathname) { return false; } if (!date) { return false; } return md5(link.pathname); };
const getIsInitByRequest = (id) => { const options = { headers: { 'Authorization': 'token ' + config.token, 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36', 'Accept': 'application/json' }, url: api + '?labels=' + id + ',Gitalk', method: 'GET' }; return new Promise((resolve) => { request(options, function (err, response, body) { if (err) { return resolve([err, false]); } if (response.statusCode != 200) { return resolve([response, false]); } const res = JSON.parse(body); if (res.length > 0) { return resolve([false, true]); } return resolve([false, false]); }); }); };
const getIsInitByCache = (() => { let gitalkCache = false; try { gitalkCache = require(config.gitalkCacheFile); } catch (e) {} return function (id) { if (!gitalkCache) { return false; } if (gitalkCache.find(({ id: itemId }) => (itemId === id))) { return true; } return false; }; })();
const idIsInit = async (id) => { if (!config.cache) { return await getIsInitByRequest(id); } if (getIsInitByCache(id) === false) { return await getIsInitByRequest(id); } return [false, true]; };
const gitalkInit = ({ url, id, title, desc }) => { const reqBody = { 'title': title, 'labels': [id, 'Gitalk'], 'body': url + '\r\n\r\n' + desc };
const options = { headers: { 'Authorization': 'token ' + config.token, 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36', 'Accept': 'application/json', 'Content-Type': 'application/json;charset=UTF-8' }, url: api, body: JSON.stringify(reqBody), method: 'POST' }; return new Promise((resolve) => { request(options, function (err, response, body) { if (err) { return resolve([err, false]); } if (response.statusCode != 201) { return resolve([response, false]); } return resolve([false, true]); }); }); };
const write = async (fileName, content, flag = 'w+') => { return new Promise((resolve) => { fs.open(fileName, flag, function (err, fd) { if (err) { resolve([err, false]); return; } fs.writeFile(fd, content, function (err) { if (err) { resolve([err, false]); return; } fs.close(fd, (err) => { if (err) { resolve([err, false]); return; } }); resolve([false, true]); }); }); }); };
const init = async () => { const urls = sitemapXmlReader(config.sitemap); const errorData = []; const initializedData = []; const successData = []; for (const item of urls) { const { url, date, title, desc } = item; const id = getGitalkId({ url, date }); if (!id) { console.log(`id: 生成失败 [ ${id} ] `); errorData.push({ ...item, info: 'id 生成失败', }); continue; } const [err, res] = await idIsInit(id); if (err) { console.log(`Error: 查询评论异常 [ ${title} ] , 信息:`, err || '无'); errorData.push({ ...item, info: '查询评论异常', }); continue; } if (res === true) { initializedData.push({ id, url, title, }); continue; } console.log(`Gitalk 初始化开始... [ ${title} ] `); const [e, r] = await gitalkInit({ id, url, title, desc }); if (e || !r) { console.log(`Error: Gitalk 初始化异常 [ ${title} ] , 信息:`, e || '无'); errorData.push({ ...item, info: '初始化异常', }); continue; } successData.push({ id, url, title, }); console.log(`Gitalk 初始化成功! [ ${title} ] - ${id}`); continue; }
console.log(''); console.log('--------- 运行结果 ---------'); console.log('');
if (errorData.length !== 0) { console.log(`报错数据: ${errorData.length} 条。参考文件 ${config.gitalkErrorFile}。`); await write(config.gitalkErrorFile, JSON.stringify(errorData, null, 2)); }
console.log(`本次成功: ${successData.length} 条。`);
if (config.cache) { console.log(`写入缓存: ${(initializedData.length + successData.length)} 条,已初始化 ${initializedData.length} 条,本次成功: ${successData.length} 条。参考文件 ${config.gitalkCacheFile}。`); await write(config.gitalkCacheFile, JSON.stringify(initializedData.concat(successData), null, 2)); } else { console.log(`已初始化: ${initializedData.length} 条。`); } };
init();
|