diff --git a/lib/routes/nga/post.js b/lib/routes/nga/post.js index 8221a5779..cf180b1e5 100644 --- a/lib/routes/nga/post.js +++ b/lib/routes/nga/post.js @@ -3,45 +3,69 @@ const cheerio = require('cheerio'); const iconv = require('iconv-lite'); module.exports = async (ctx) => { - const { tid } = ctx.params; - const link = `https://nga.178.com/read.php?tid=${tid}&rand=${Math.random() * 1000}`; - const timestamp = Math.floor(Date.now() / 1000); - const response = await axios({ - method: 'get', - url: link, - responseType: 'arraybuffer', - headers: { - Cookie: `guestJs=${timestamp};`, - }, - }); + const getPageUrl = (tid, page = 1, hash = '') => `https://nga.178.com/read.php?tid=${tid}&page=${page}&rand=${Math.random() * 1000}#${hash}`; + const getPage = async (tid, pageId = 1) => { + const link = getPageUrl(tid, pageId); + const timestamp = Math.floor(Date.now() / 1000); + const response = await axios({ + method: 'get', + url: link, + responseType: 'arraybuffer', + headers: { + Cookie: `guestJs=${timestamp};`, + }, + }); - const htmlString = iconv.decode(response.data, 'gbk'); - const $ = cheerio.load(htmlString); + const htmlString = iconv.decode(response.data, 'gbk'); + return cheerio.load(htmlString); + }; + + const getLastPageId = async (tid) => { + const $ = await getPage(tid); + const nav = $('#pagebtop'); + const match = nav.html().match(/\{0:'\/read.php\?tid=(\d+)',1:(\d+).*?\}/); + return match ? match[2] : 1; + }; + + const eraseTag = (str) => str.replace(/\[(\w+)\].+?\[\/\1\]/g, ''); + const eraseQuote = (str) => str.replace(/\[quote\](.+?)\[\/quote\]/, (m, b) => b).replace(/\[b\].*?\[uid=\d+\](.+?)\[\/uid\].+?\[\/b\]/, (m, user) => `@${user} `); + const pipeImg = (str) => str.replace(/\[img\](.+?)\[\/img\]/g, (m, src) => ``); + + const tid = ctx.params.tid; + const pageId = await getLastPageId(tid); + + const $ = await getPage(tid, pageId); const title = $('title').text() || ''; - const description = $('#postcontentandsubject0').html() || ''; - let pubDate = new Date($('#postdate0').text()).toUTCString(); - if ($('#alertc0').length > 0) { - const matches = htmlString.match(/<\/span>