From bdc1b91a0b76733aa22ba302ec8c07085f5faeb1 Mon Sep 17 00:00:00 2001 From: Tony Date: Thu, 7 Mar 2024 13:01:52 +0800 Subject: [PATCH] fix(route): t66y (#14700) --- lib/router.js | 4 +- lib/routes-deprecated/t66y/index.js | 145 --------------------------- lib/routes-deprecated/t66y/post.js | 149 ---------------------------- lib/routes/t66y/index.ts | 55 ++++++++++ lib/routes/t66y/maintainer.ts | 4 + lib/routes/t66y/post.ts | 75 ++++++++++++++ lib/routes/t66y/radar.ts | 25 +++++ lib/routes/t66y/router.ts | 4 + lib/routes/t66y/utils.ts | 57 +++++++++++ 9 files changed, 222 insertions(+), 296 deletions(-) delete mode 100644 lib/routes-deprecated/t66y/index.js delete mode 100644 lib/routes-deprecated/t66y/post.js create mode 100644 lib/routes/t66y/index.ts create mode 100644 lib/routes/t66y/maintainer.ts create mode 100644 lib/routes/t66y/post.ts create mode 100644 lib/routes/t66y/radar.ts create mode 100644 lib/routes/t66y/router.ts create mode 100644 lib/routes/t66y/utils.ts diff --git a/lib/router.js b/lib/router.js index 39742737c..d25d903cd 100644 --- a/lib/router.js +++ b/lib/router.js @@ -83,8 +83,8 @@ router.get('/mihoyo/bh3/:type', lazyloadRouteHandler('./routes/mihoyo/bh3')); router.get('/mihoyo/bh2/:type', lazyloadRouteHandler('./routes/mihoyo/bh2')); // 草榴社区 -router.get('/t66y/post/:tid', lazyloadRouteHandler('./routes/t66y/post')); -router.get('/t66y/:id/:type?', lazyloadRouteHandler('./routes/t66y/index')); +// router.get('/t66y/post/:tid', lazyloadRouteHandler('./routes/t66y/post')); +// router.get('/t66y/:id/:type?', lazyloadRouteHandler('./routes/t66y/index')); // 色中色 router.get('/sexinsex/:id/:type?', lazyloadRouteHandler('./routes/sexinsex/index')); diff --git a/lib/routes-deprecated/t66y/index.js b/lib/routes-deprecated/t66y/index.js deleted file mode 100644 index c8d4fa467..000000000 --- a/lib/routes-deprecated/t66y/index.js +++ /dev/null @@ -1,145 +0,0 @@ -const cheerio = require('cheerio'); -const got = require('@/utils/got'); -const url = require('url'); - -const base = 'http://www.t66y.com'; -const section = 'thread0806.php?fid='; -const got_ins = got.extend({ - headers: { - Referer: base, - }, -}); - -function killViidii(originUrl) { - const decodeStr = /.*\?http/g; - const decodeSig = /______/g; - const jsSuffix = '&z'; - const htmlSuffix = '&z'; - const returnSuffix = 'return false'; - return originUrl.includes('viidii') ? originUrl.replaceAll(decodeStr, 'http').replaceAll(decodeSig, '.').replace(jsSuffix, '').replace(htmlSuffix, '').replace(returnSuffix, '') : originUrl; -} - -const sourceTimezoneOffset = -8; -const filterReg = /read\.php/; - -module.exports = async (ctx) => { - const typefilter = ctx.params.type ? `&type=${ctx.params.type}` : ''; - const res = await got_ins.get(url.resolve(base, `${section}${ctx.params.id}${typefilter}&search=today`)); - const data = res.data; - const $ = cheerio.load(data); - const typeTitle = ctx.params.type ? `[${$('.t .fn b').text()}]` : ''; - let list = $('#ajaxtable > tbody:nth-child(2)'); - list = $('.tr3', list).not('.tr2.tac').nextAll().slice(0, 25).get(); - - const parseContent = (htmlString) => { - let $ = cheerio.load(htmlString); - let time = $('#main > div:nth-child(4) > table > tbody > tr:nth-child(2) > th > div').text(); - const regex = /\d{4}-\d{2}-\d{2} \d{2}:\d{2}/; - const regRes = regex.exec(time); - time = regRes === null ? new Date() : new Date(regRes[0]); - time.setTime(time.getTime() + (sourceTimezoneOffset - time.getTimezoneOffset() / 60) * 60 * 60 * 1000); - - const author = $('#main > div:nth-child(4) > table > tbody > tr.tr1.do_not_catch > th:nth-child(1) > b').text(); - const content = $('#main > div:nth-child(4) > table > tbody > tr.tr1.do_not_catch > th:nth-child(2) > table > tbody > tr > td > div.tpc_content.do_not_catch').html(); - - // Change the image tag to display image in rss reader - try { - $ = cheerio.load(content, { decodeEntities: false }); // 修复无发进行filter与filterout的问题 - } catch { - return null; - } - - // Handle video - const video = $('a:nth-of-type(2)'); - if (video) { - const videoScript = video.attr('onclick'); - const regVideo = /https?:\/\/.*'/; - const videoRes = regVideo.exec(videoScript); - if (videoRes && videoRes.length !== 0) { - let link = videoRes[0]; - link = link.slice(0, -1); - $('iframe').attr('src', link); - } - } - // Handle img tag - let images = $('img'); - for (const image of images) { - $(image).replaceWith(``); - } - // Handle input tag - images = $('input'); - for (const image of images) { - $(image).replaceWith(``); - } - - // Handle links - const links = $('a[href*="viidii"]'); - for (const link of links) { - $(link).attr('href', killViidii($(link).attr('href'))); - } - - return { - author, - description: $('body').html(), - pubDate: time.toUTCString(), - }; - }; - - const out = await Promise.all( - list.map(async (item) => { - const $ = cheerio.load(item); - let title = $('.tal h3 a'); - const path = title.attr('href'); - - // Filter duplicated or null entries - if (!path || path.match(filterReg) !== null) { - return ''; - } - const link = url.resolve(base, path); - - // Check cache - const cache = await ctx.cache.get(link); - if (cache) { - return JSON.parse(cache); - } - - const tnode = $('.tal').contents().get(0); - const catalog = tnode.type === 'text' ? tnode.data.trim() : ''; - - title = cheerio.load(title.html())('font').text() === '' ? title.text() : cheerio.load(title.html())('font').text(); - - if (!title) { - return ''; - } - - const single = { - title: `${catalog} ${title}`, - link, - guid: path, - }; - - try { - const response = await got_ins.get(url.resolve(base, path)); - const result = parseContent(response.data); - - if (!result) { - return ''; - } - - single.author = result.author; - single.description = result.description; - single.pubDate = result.pubDate; - } catch { - return ''; - } - ctx.cache.set(link, JSON.stringify(single)); - return single; - }) - ); - - ctx.state.data = { - title: typeTitle + $('title').text(), - link: url.resolve(base, `${section}${ctx.params.id}${typefilter}`), - item: out.filter((item) => item !== ''), - }; -}; diff --git a/lib/routes-deprecated/t66y/post.js b/lib/routes-deprecated/t66y/post.js deleted file mode 100644 index f2593f128..000000000 --- a/lib/routes-deprecated/t66y/post.js +++ /dev/null @@ -1,149 +0,0 @@ -const cheerio = require('cheerio'); -const got = require('@/utils/got'); -const iconv = require('iconv-lite'); -const url = require('url'); - -const base = 'http://www.t66y.com'; -const got_ins = got.extend({ - headers: { - Referer: base, - }, - responseType: 'buffer', -}); - -function killViidii(originUrl) { - const decodeStr = /.*\?http/g; - const decodeSig = /______/g; - const jsSuffix = '&z'; - const htmlSuffix = '&z'; - const returnSuffix = 'return false'; - return originUrl.includes('viidii') ? originUrl.replaceAll(decodeStr, 'http').replaceAll(decodeSig, '.').replace(jsSuffix, '').replace(htmlSuffix, '').replace(returnSuffix, '') : originUrl; -} - -const sourceTimezoneOffset = -8; - -function parseContent(htmlString) { - let $ = cheerio.load(htmlString, { decodeEntities: false }); - let time = $('.tipad').text(); - const regex = /\d{4}-\d{2}-\d{2} \d{2}:\d{2}/; - const regRes = regex.exec(time); - time = regRes === null ? new Date() : new Date(regRes[0]); - time.setTime(time.getTime() + (sourceTimezoneOffset - time.getTimezoneOffset() / 60) * 60 * 60 * 1000); - - const author = $('th.r_two > b').text(); - const content = $('.tpc_content').html(); - - // Change the image tag to display image in rss reader - try { - $ = cheerio.load(content, { decodeEntities: false }); // 修复无发进行filter与filterout的问题 - } catch { - return null; - } - - // Handle video - const video = $('a:nth-of-type(2)'); - if (video) { - const videoScript = video.attr('onclick'); - const regVideo = /https?:\/\/.*'/; - const videoRes = regVideo.exec(videoScript); - if (videoRes && videoRes.length !== 0) { - let link = videoRes[0]; - link = link.slice(0, -1); - $('iframe').attr('src', link); - } - } - // Handle img tag - let images = $('img'); - for (const image of images) { - $(image).replaceWith(``); - } - // Handle input tag - images = $('input'); - for (const image of images) { - $(image).replaceWith(``); - } - - // Handle links - const links = $('a[href*="viidii"]'); - for (const link of links) { - $(link).attr('href', killViidii($(link).attr('href'))); - } - - return { - author, - description: $('body').html(), - pubDate: time.toUTCString(), - }; -} - -module.exports = async (ctx) => { - const tid = ctx.params.tid; - let response = await got_ins.get(url.resolve(base, `/read.php?tid=${tid}`)); - let html = iconv.decode(response.data, 'gbk'); - // 跟踪重定向 - let $ = cheerio.load(html); - const redirect = $('a:last-child').attr('href'); - response = await got_ins.get(url.resolve(base, redirect)); - html = iconv.decode(response.data, 'gbk'); - $ = cheerio.load(html, { decodeEntities: false }); - // 获取楼主id、末页 - const uid = /uid=(\d+)/.exec($('.t.t2').first().find('.tiptop>a:first-child').attr('href'))[1]; - const lasturl = $('.pages:first-child>a:last-child').attr('href'); - const lastpage = lasturl ? /page=(\d+)/.exec(lasturl)[1] : 1; - // 从缓存中获取上次读取的页吗 - let page = 0; - const cachePage = await ctx.cache.tryGet(`/t66y/post/${tid}`, () => ({ page })); - page = cachePage.page + 1; - if (page > lastpage) { - page = lastpage; - } - - // 记录重试次数 - let retry = 0; - // 请求帖子 - const load = async (page) => { - const link = url.resolve(base, `/read.php?tid=${tid}&page=${page}`); - if (page > 1) { - // 只有第二页开始需要重新读取 - html = await ctx.cache.tryGet(link, async () => { - const response = await got_ins.get(link, { - headers: { - Referer: url.resolve(base, redirect), - }, - }); - return iconv.decode(response.data, 'gbk'); - }); - $ = cheerio.load(html, { decodeEntities: false }); - } - - const title = /本頁主題: (.+)\n?/.exec($('#main div.t .h:first-child').text())[1]; - const items = $('.t.t2') - .get() - .filter((item) => { - const matchs = /uid=(\d+)/.exec($('.tiptop>a:first-child', item).attr('href')); - return matchs.length === 2 && uid === matchs[1]; - }) - .map((item, index) => { - const single = { - title: `P${page}#${index + 1} ${title}`, - link: `${link}#${index + 1}`, - guid: `${link}#${index + 1}`, - }; - const result = parseContent($(item).html()); - return Object.assign(single, result); - }); - - // 记录读取的最后页码 - ctx.cache.set(`/t66y/post/${tid}`, JSON.stringify({ page })); - - // 如果没有读到内容,则读取下一页,最多读取3页 - if (items.length === 0 && retry < 3) { - retry++; // 记录重试次数 - return load(page + 1); - } else { - return { title, link: url.resolve(base, `/read.php?tid=${tid}`), item: items }; - } - }; - - ctx.state.data = await load(page); -}; diff --git a/lib/routes/t66y/index.ts b/lib/routes/t66y/index.ts new file mode 100644 index 000000000..e4384cc6c --- /dev/null +++ b/lib/routes/t66y/index.ts @@ -0,0 +1,55 @@ +import * as cheerio from 'cheerio'; +import got from '@/utils/got'; +import cache from '@/utils/cache'; +import { parseDate } from '@/utils/parse-date'; +import { baseUrl, parseContent } from './utils'; + +export default async (ctx) => { + const { id, type } = ctx.req.param(); + + const url = new URL(`thread0806.php?fid=${id}&search=today`, baseUrl); + type && url.searchParams.set('type', type); + + const { data: res } = await got(url); + const $ = cheerio.load(res); + const list = $('#ajaxtable > tbody:nth-child(2) .tr3') + .not('.tr2.tac') + .toArray() + .map((item) => { + const element = $(item); + + const tal = element.find('.tal'); + const catalog = tal + .contents() + .filter((_, node) => node.type === 'text') + .text() + .trim(); + const a = tal.find('h3 a'); + const td3 = element.find('td:nth-child(3)'); + + return { + title: `${catalog} ${a.text()}`, + link: `${baseUrl}/${a.attr('href')}`, + author: td3.find('a').text(), + pubDate: parseDate(String(td3.find('span[data-timestamp]').data('timestamp')).slice(0, -1), 'X'), + }; + }); + + const out = await Promise.all( + list.map((item) => + cache.tryGet(item.link, async () => { + const { data: response } = await got(item.link); + + item.description = parseContent(response); + + return item; + }) + ) + ); + + ctx.set('data', { + title: (type ? `[${$('.t .fn b').text()}]` : '') + $('head title').text(), + link: url.href, + item: out, + }); +}; diff --git a/lib/routes/t66y/maintainer.ts b/lib/routes/t66y/maintainer.ts new file mode 100644 index 000000000..0522da08d --- /dev/null +++ b/lib/routes/t66y/maintainer.ts @@ -0,0 +1,4 @@ +export default { + '/post/:tid': ['cnzgray'], + '/:id/:type?': ['zhboner'], +}; diff --git a/lib/routes/t66y/post.ts b/lib/routes/t66y/post.ts new file mode 100644 index 000000000..87cb3916e --- /dev/null +++ b/lib/routes/t66y/post.ts @@ -0,0 +1,75 @@ +import * as cheerio from 'cheerio'; +import got from '@/utils/got'; +import cache from '@/utils/cache'; +import { parseDate } from '@/utils/parse-date'; +import { baseUrl, parseContent } from './utils'; + +function parseItems(tid: string, $: cheerio.CheerioAPI) { + return $('.tr1:nth-child(1)') + .toArray() + .map((item) => { + const $item = $(item); + + const content = parseContent($item.html()); + const footer = $item.next(); + const pid = footer.find('.tipad a[title]').attr('id')?.slice(2); + + return { + title: content?.split('
')[0], + description: content, + author: $item + .find('b') + .contents() + .filter((_, ele) => ele.type === 'text') + .text() + .trim(), + pubDate: parseDate(String(footer.find('span[data-timestamp]').data('timestamp')), 'X'), + link: `${baseUrl}/read.php?tid=${tid}${pid ? `&pid=${pid}` : ''}`, + }; + }); +} + +export default async (ctx) => { + const tid = ctx.req.param('tid') as string; + const { data: response } = await got(`${baseUrl}/read.php?tid=${tid}`); + // 跟踪重定向 + let $ = cheerio.load(response); + const redirect = $('a:last-child').attr('href'); + if (!redirect) { + throw new Error('Cannot get the redirect link'); + } + + const { data: redirectedResponse, url: link } = await got(new URL(redirect, baseUrl).href); + $ = cheerio.load(redirectedResponse); + + const firstPage = parseItems(tid, $); + + const pageSize = $('.w70 input').eq(0).attr('value')?.split('/')[1]; + let pageUrls: string[] = []; + if (pageSize) { + const length = Number.parseInt(pageSize); + pageUrls = Array.from({ length }, (_, i) => `${baseUrl}/read.php?tid=${tid}&page=${i + 1}`).slice(1); + } + + // 请求帖子 + const nextPages = pageSize + ? await Promise.all( + pageUrls.map((url) => + cache.tryGet(url, async () => { + const { data: res } = await got(url); + const $ = cheerio.load(res); + + return parseItems(tid, $); + }) + ) + ) + : []; + + const items = [...firstPage, ...nextPages.flat()]; + + ctx.set('data', { + title: $('head title').text(), + link, + item: items, + }); +}; diff --git a/lib/routes/t66y/radar.ts b/lib/routes/t66y/radar.ts new file mode 100644 index 000000000..99a3d0842 --- /dev/null +++ b/lib/routes/t66y/radar.ts @@ -0,0 +1,25 @@ +export default { + 't66y.com': { + _name: '草榴社區', + '.': [ + { + title: '分区帖子', + docs: 'https://docs.rsshub.app/routes/multimedia#cao-liu-she-qu', + source: ['/thread0806.php'], + target: (param, url) => { + const { searchParams } = new URL(url); + return `/t66y/${searchParams.get('fid')}${searchParams.has('type') ? `/${searchParams.get('type')}` : ''}`; + }, + }, + { + title: '帖子跟踪', + docs: 'https://docs.rsshub.app/routes/multimedia#cao-liu-she-qu', + source: ['/htm_data/:YYMM/DD/:tid', '/read.php'], + target: (param, url) => { + const link = new URL(url); + return link.pathname === '/read.php' ? `/t66y/post/${link.searchParams.get('tid')}` : `/t66y/post/${param.tid}`; + }, + }, + ], + }, +}; diff --git a/lib/routes/t66y/router.ts b/lib/routes/t66y/router.ts new file mode 100644 index 000000000..0d3e3b558 --- /dev/null +++ b/lib/routes/t66y/router.ts @@ -0,0 +1,4 @@ +export default (router) => { + router.get('/post/:tid', './post'); + router.get('/:id/:type?', './index'); +}; diff --git a/lib/routes/t66y/utils.ts b/lib/routes/t66y/utils.ts new file mode 100644 index 000000000..36f7e825e --- /dev/null +++ b/lib/routes/t66y/utils.ts @@ -0,0 +1,57 @@ +import * as cheerio from 'cheerio'; + +export const baseUrl = 'https://www.t66y.com'; + +const killRedircdn = (originUrl) => { + const decodeStr = /.*\?http/g; + const decodeSig = /______/g; + const htmlSuffix = '&z'; + return originUrl.replaceAll(decodeStr, 'http').replaceAll(decodeSig, '.').replace(htmlSuffix, ''); +}; + +export const parseContent = (htmlString) => { + const $ = cheerio.load(htmlString); + + const content = $('div.tpc_content').eq(0); + content.find('.t_like').remove(); + + // Handle video + // const video = $('a:nth-of-type(2)'); + // if (video) { + // const videoScript = video.attr('onclick'); + // const regVideo = /https?:\/\/.*'/; + // const videoRes = regVideo.exec(videoScript); + // if (videoRes && videoRes.length !== 0) { + // let link = videoRes[0]; + // link = link.slice(0, -1); + // $('iframe').attr('src', link); + // } + // } + // Handle img tag + content.find('img').each((_, ele) => { + const $ele = $(ele); + const essData = $ele.attr('ess-data'); + if (essData) { + $ele.attr('src', essData); + } + $ele.removeAttr('ess-data'); + $ele.removeAttr('iyl-data'); + }); + + // Handle input tag + // images = $('input'); + // for (const image of images) { + // $(image).replaceWith(``); + // } + + // Handle links + content.find('a').each((_, ele) => { + const $ele = $(ele); + const href = $ele.attr('href'); + if (href?.includes('redircdn')) { + $ele.attr('href', killRedircdn(href)); + } + }); + + return content.html(); +};