const got = require('@/utils/got'); const cheerio = require('cheerio'); const iconv = require('iconv-lite'); const { parseDate } = require('@/utils/parse-date'); const timezone = require('@/utils/timezone'); const config = require('@/config').value; module.exports = async (ctx) => { const getPageUrl = (tid, authorId, page = 1, hash = '') => `https://nga.178.com/read.php?tid=${tid}&page=${page}${authorId ? `&authorid=${authorId}` : ''}&rand=${Math.random() * 1000}#${hash}`; const getPage = async (tid, authorId, pageId = 1) => { const link = getPageUrl(tid, authorId, pageId); const timestamp = Math.floor(Date.now() / 1000); let cookieString = `guestJs=${timestamp};`; if (config.nga.uid && config.nga.cid) { cookieString = `ngaPassportUid=${config.nga.uid}; ngaPassportCid=${config.nga.cid};`; } const response = await got(link, { responseType: 'buffer', headers: { Cookie: cookieString, }, }); const htmlString = iconv.decode(response.data, 'gbk'); return cheerio.load(htmlString); }; const getLastPageId = async (tid, authorId) => { const $ = await getPage(tid, authorId); const nav = $('#pagebtop'); const match = nav.html().match(/\{0:'\/read\.php\?tid=(\d+).*?',1:(\d+),.*?\}/); return match ? match[2] : 1; }; const deepReplace = (str, pattern, replace) => { // 对于可能存在嵌套的样式一路 replace 到最深处 while (str.match(pattern)) { str = str.replace(pattern, replace); } return str; }; const formatContent = (str) => { // 简单样式 str = deepReplace(str, /\[(b|u|i|del|code|sub|sup)\](.+?)\[\/\1\]/g, '<$1>$2$1>'); str = str .replace(/\[dice\](.+?)\[\/dice\]/g, 'ROLL : $1') .replace(/\[color=(.+?)\](.+?)\[\/color\]/g, '$2') .replace(/\[font=(.+?)\](.+?)\[\/font\]/g, '$2') .replace(/\[size=(.+?)\](.+?)\[\/size\]/g, '$2') .replace(/\[align=(.+?)\](.+?)\[\/align\]/g, '$2'); // 列表 str = deepReplace(str, /\[\*\](.+?)(?=\[\*\]|\[\/list\])/g, '
$1') .replace(/\[@(.+?)\]/g, '@$1') .replace(/\[uid=(\d+)\](.+?)\[\/uid\]/g, '@$2') .replace(/\[tid=(\d+)\](.+?)\[\/tid\]/g, '$2') .replace(/\[pid=(\d+),(\d+),(\d+)\](.+?)\[\/pid\]/g, (m, pid, tid, page, str) => { const url = `https://nga.178.com/read.php?tid=${tid}&page=${page}#pid${pid}Anchor`; return `${str}`; }); // 链接 str = str.replace(/\[url=(.+?)\](.+?)\[\/url\]/g, '$2'); // 分割线 str = str.replace(/\[h\](.+?)\[\/h\]/g, '