Skip to content

Commit

Permalink
fix: V2EX Route Exclude Duplicate Posts (DIYgod#13144)
Browse files Browse the repository at this point in the history
* update: V2EX Route Exclude Duplicate Posts

* use ctx.query.limit

* refactor: migrate to v2

---------

Co-authored-by: pull[bot] <39814207+pull[bot]@users.noreply.github.com>
  • Loading branch information
0xleizhang and pull[bot] authored Aug 29, 2023
1 parent 966e812 commit 9335585
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 64 deletions.
6 changes: 3 additions & 3 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ router.get('/huya/live/:id', lazyloadRouteHandler('./routes/huya/live'));
router.get('/showroom/room/:id', lazyloadRouteHandler('./routes/showroom/room'));

// v2ex
router.get('/v2ex/topics/:type', lazyloadRouteHandler('./routes/v2ex/topics'));
router.get('/v2ex/post/:postid', lazyloadRouteHandler('./routes/v2ex/post'));
router.get('/v2ex/tab/:tabid', lazyloadRouteHandler('./routes/v2ex/tab'));
// router.get('/v2ex/topics/:type', lazyloadRouteHandler('./routes/v2ex/topics'));
// router.get('/v2ex/post/:postid', lazyloadRouteHandler('./routes/v2ex/post'));
// router.get('/v2ex/tab/:tabid', lazyloadRouteHandler('./routes/v2ex/tab'));

// f-droid
router.get('/fdroid/apprelease/:app', lazyloadRouteHandler('./routes/fdroid/apprelease'));
Expand Down
39 changes: 0 additions & 39 deletions lib/routes/v2ex/post.js

This file was deleted.

5 changes: 5 additions & 0 deletions lib/v2/v2ex/maintainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
'/post/:postid': ['kt286'],
'/tab/:tabid': ['liyefox'],
'/topics/:type': ['WhiteWorld'],
};
35 changes: 35 additions & 0 deletions lib/v2/v2ex/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const got = require('@/utils/got');
const { parseDate } = require('@/utils/parse-date');

module.exports = async (ctx) => {
const { postid } = ctx.params;
const pageUrl = `https://www.v2ex.com/t/${postid}`;

const { data: topicResponse } = await got('https://www.v2ex.com/api/topics/show.json', {
searchParams: {
id: postid,
},
});

const { data: replies } = await got('https://www.v2ex.com/api/replies/show.json', {
searchParams: {
topic_id: postid,
},
});

const topic = topicResponse[0];

ctx.state.data = {
title: `V2EX-${topic.title}`,
link: pageUrl,
description: topic.content,
item: replies.map((item, index) => ({
title: `#${index + 1} ${item.content}`,
description: item.content_rendered,
link: `${pageUrl}#r_${item.id}`,
author: item.member.username,
pubDate: parseDate(item.created, 'X'),
})),
allowEmpty: true,
};
};
35 changes: 35 additions & 0 deletions lib/v2/v2ex/radar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module.exports = {
'v2ex.com': {
_name: 'V2EX',
'.': [
{
title: '最热 / 最新主题',
docs: 'https://docs.rsshub.app/routes/v2ex',
source: ['/'],
target: (_, url) => {
const { searchParams } = new URL(url);
if (searchParams.get('tab') === 'all' || searchParams.get('tab') === 'hot') {
return `/v2ex/topics/${searchParams.get('tab')?.replace('all', 'latest')}`;
}
},
},
{
title: '帖子',
docs: 'https://docs.rsshub.app/routes/v2ex',
source: ['/t/:postid'],
target: '/v2ex/post/:postid',
},
{
title: '标签',
docs: 'https://docs.rsshub.app/routes/v2ex',
source: ['/'],
target: (_, url) => {
const { searchParams } = new URL(url);
if (searchParams.get('tab') && searchParams.get('tab') !== 'all' && searchParams.get('tab') !== 'hot') {
return `/v2ex/tab/${searchParams.get('tab')}`;
}
},
},
],
},
};
5 changes: 5 additions & 0 deletions lib/v2/v2ex/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = (router) => {
router.get('/post/:postid', require('./post'));
router.get('/tab/:tabid', require('./tab'));
router.get('/topics/:type', require('./topics'));
};
38 changes: 17 additions & 21 deletions lib/routes/v2ex/tab.js → lib/v2/v2ex/tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,21 @@ module.exports = async (ctx) => {

const $ = cheerio.load(response.data);
const links = $('span.item_title > a')
.map((i, link) => `${host}${$(link).attr('href')}`)
.slice(0, 10)
.get();
.toArray()
.slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 10)
.map((link) => `${host}${$(link).attr('href').replace(/#.*$/, '')}`);

const items = await Promise.all(
links.map(async (pageUrl) => {
const cacheKey = `v2ex-${pageUrl}`;
const cacheValue = await ctx.cache.get(cacheKey);
let post = {};
if (cacheValue) {
post = cacheValue;
} else {
links.map((link) =>
ctx.cache.tryGet(`v2ex-${link}`, async () => {
const response = await got({
method: 'get',
url: pageUrl,
url: link,
});

const $ = cheerio.load(response.data);
const list = $('[id^="r_"]').get();
const reply_content = list
const list = $('[id^="r_"]').toArray();
const replyContent = list
.map((item) => {
const post = $(item);
const content = post.find('.reply_content').html();
Expand All @@ -39,18 +36,17 @@ module.exports = async (ctx) => {
return `<p><div>#${no}: <i>${author}</i></div><div>${content}</div></p>`;
})
.join('');
post = {

return {
title: $('.header h1').text(),
link: pageUrl,
guid: pageUrl,
description: $('div.topic_content').html() + `<div>${reply_content}</div>`,
link,
description: `${$('div.topic_content').html()}<div>${replyContent}</div>`,
author: $('div.header > small > a').text(),
};
ctx.cache.set(cacheKey, post);
}
return Promise.resolve(post);
})
})
)
);

ctx.state.data = {
title: `V2EX-${tabid}`,
link: pageUrl,
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/v2ex/topics.js → lib/v2/v2ex/topics.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const got = require('@/utils/got');
const { parseDate } = require('@/utils/parse-date');

module.exports = async (ctx) => {
const type = ctx.params.type;
const { type } = ctx.params;

const { data } = await got(`https://www.v2ex.com/api/topics/${type}.json`);

Expand Down

0 comments on commit 9335585

Please sign in to comment.