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', // GitHub repository 所有者,可以是个人或者组织。对应Gitalk配置中的owner repo: "toimc.github.io", // 储存评论issue的github仓库名,仅需要仓库名字即可。对应 Gitalk配置中的repo token: 'xxxxxx', // 前面申请的 personal access token sitemap: path.join(__dirname, './public/sitemap.xml'), // 自己站点的 sitemap 文件地址 cache: true, // 是否启用缓存,启用缓存会将已经初始化的数据写入配置的 gitalkCacheFile 文件,下一次直接通过缓存文件判断 gitalkCacheFile: path.join(__dirname, './gitalk-init-cache.json'), // 用于保存 gitalk 已经初始化的 id 列表 gitalkErrorFile: path.join(__dirname, './gitalk-init-error.json'), // 用于保存 gitalk 初始化报错的数据 };
const api = 'https://api.github.com/repos/' + config.username + '/' + config.repo + '/issues';
/** * 读取 sitemap 文件 * 远程 sitemap 文件获取可参考 https://www.npmjs.com/package/sitemapper */ 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 []; } };
// 获取 gitalk 使用的 id const getGitalkId = ({ url: u, date }) => { const link = url.parse(u); // 链接不存在,不需要初始化 if (!link || !link.pathname) { return false; } if (!date) { return false; } return md5(link.pathname); };
/** * 通过以请求判断是否已经初始化 * @param {string} gitalk 初始化的id * @return {[boolean, boolean]} 第一个值表示是否出错,第二个值 false 表示没初始化, true 表示已经初始化 */ 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]); }); }); };
/** * 通过缓存判断是否已经初始化 * @param {string} gitalk 初始化的id * @return {boolean} false 表示没初始化, true 表示已经初始化 */ 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; }; })();
// 根据缓存,判断链接是否已经初始化 // 第一个值表示是否出错,第二个值 false 表示没初始化, true 表示已经初始化 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 }) => { //创建issue 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]); }); }); };
/** * 写入内容 * @param {string} fileName 文件名 * @param {string} content 内容 */ 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) { // console.log(`--- Gitalk 已经初始化 --- [ ${title} ] `); 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();
|