feat: t66y增加跟踪指定帖子的功能 (#1140)

This commit is contained in:
cnzgray
2018-11-14 14:26:50 +08:00
committed by DIYgod
parent b9255836b2
commit 8ae35c8dc8
3 changed files with 179 additions and 0 deletions
+12
View File
@@ -867,6 +867,18 @@ GitHub 官方也提供了一些 RSS:
</route>
<route name="帖子跟踪" author="cnzgray" example="/t66y/post/3286088" path="/t66y/post/:tid" :paramsDesc="['帖子 id, 可在帖子 URL 中找到']">
::: tip 提示
帖子 id 查找办法:
打开想跟踪的帖子,比如:http://t66y.com/htm_data/20/1811/3286088.html。其中`3286088`就是帖子id。
:::
</route>
### sexinsex
<route name="分区帖子" author="cnzgray" example="/sexinsex/230/634" path="/sexinsex/:id/:type?" :paramsDesc="['分区 id, 可在分区页 URL 中找到', '类型 id, 可在分区类型过滤后的 URL 中找到']">
+1
View File
@@ -368,6 +368,7 @@ router.get('/cctv/:category', require('./routes/cctv/category'));
router.get('/caixin/:column/:category', require('./routes/caixin/category'));
// 草榴社区
router.get('/t66y/post/:tid', require('./routes/t66y/post'));
router.get('/t66y/:id/:type?', require('./routes/t66y/index'));
// 色中色
+166
View File
@@ -0,0 +1,166 @@
const cheerio = require('cheerio');
const axios = require('../../utils/axios');
const iconv = require('iconv-lite');
const url = require('url');
const base = 'http://www.t66y.com';
const axios_ins = axios.create({
headers: {
Referer: base,
},
responseType: 'arraybuffer',
});
function killViidii(originUrl) {
const decodeStr = /.*\?http/g;
const decodeSig = /______/g;
const jsSuffix = '&amp;z';
const htmlSuffix = '&z';
const returnSuffix = 'return false';
if (originUrl.indexOf('viidii') !== -1) {
return originUrl
.replace(decodeStr, 'http')
.replace(decodeSig, '.')
.replace(jsSuffix, '')
.replace(htmlSuffix, '')
.replace(returnSuffix, '');
} else {
return 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 content = $('.tpc_content').html();
// Change the image tag to display image in rss reader
try {
$ = cheerio.load(content, { decodeEntities: false }); // 修复无发进行filter与filterout的问题
} catch (error) {
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, link.length - 1);
$('iframe').attr('src', link);
}
}
// Handle img tag
let images = $('img');
for (let k = 0; k < images.length; k++) {
$(images[k]).replaceWith(`<img src="${$(images[k]).attr('data-src')}" referrerpolicy="no-referrer" />`);
}
// Handle input tag
images = $('input');
for (let k = 0; k < images.length; k++) {
$(images[k]).replaceWith(`<img src="${$(images[k]).attr('data-src')}" referrerpolicy="no-referrer" />`);
}
// Handle links
const links = $('a[href*="viidii"]');
for (let k = 0; k < links.length; k++) {
$(links[k]).attr('href', killViidii($(links[k]).attr('href')));
}
return {
description: $('body').html(),
pubDate: time.toUTCString(),
};
}
module.exports = async (ctx) => {
const tid = ctx.params.tid;
let response = await axios_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 axios_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 = JSON.parse(await ctx.cache.tryGet(`/t66y/post/${tid}`, () => JSON.stringify({ page }), 3 * 60 * 60));
page = cachePage.page + 1;
if (page > lastpage) {
page = lastpage;
}
// 请求帖子
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 axios_ins.get(link, {
headers: {
Referer: url.resolve(base, redirect),
},
});
return iconv.decode(response.data, 'gbk');
},
3 * 60 * 60
);
$ = 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 * 60 * 60);
if (items.length === 0) {
// 如果没有读到内容,则读取下一页。
return load(page + 1);
} else {
return {
title: title,
link: url.resolve(base, `/read.php?tid=${tid}`),
item: items,
};
}
};
ctx.state.data = await load(page);
};