diff --git a/docs/new-media.md b/docs/new-media.md index 7194e0df24..d8dfc2ba12 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -168,6 +168,10 @@ pageClass: routes +### 主题 + + + ## Common App ### Blog diff --git a/lib/router.js b/lib/router.js index 3649e38eb9..0014ff4152 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1823,6 +1823,7 @@ router.get('/maoyan/upcoming', lazyloadRouteHandler('./routes/maoyan/upcoming')) // cnBeta router.get('/cnbeta', lazyloadRouteHandler('./routes/cnbeta/home')); +router.get('/cnbeta/topic/:topic_id', lazyloadRouteHandler('./routes/cnbeta/topic')); // 国家退伍士兵信息 router.get('/gov/veterans/:type', lazyloadRouteHandler('./routes/gov/veterans/china')); diff --git a/lib/routes/cnbeta/topic.js b/lib/routes/cnbeta/topic.js new file mode 100644 index 0000000000..9481b4ad80 --- /dev/null +++ b/lib/routes/cnbeta/topic.js @@ -0,0 +1,75 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const date = require('@/utils/date'); +module.exports = async (ctx) => { + const topicUrl = `https://www.cnbeta.com/topics/${ctx.params.topic_id}.htm`; + const response = await got({ + method: 'get', + url: topicUrl, + }); + const $ = cheerio.load(response.data); + + const topicInfo = $('div.cate-brief h2 b').text(); + const news_list = $('div.item') + .map((i, item) => { + const title = $(item).find('dl dt a').text(); + let href = $(item).find('dl dt a').attr('href'); + if (href === undefined) { + return null; + } + if (href.startsWith('//')) { + href = 'https:' + href; + } + const status = $(item).find('div.meta-data ul.status li')['0'].children[0].data; + const info = status.split(' '); + const author = info[0]; + const pubDate = info[1] + ' ' + info[2]; + return { title, href, author, pubDate }; + }) + .filter((x) => x) + .map((i, e) => e) + .get(); + + const ProcessFeed = (data) => { + const $ = cheerio.load(data); + + // 移除6.18广告 + $('.article-global').remove(); + $('.article-topic').remove(); + + // 提取内容 + return $('.article-summary p').html() + '
' + $('.article-content').html(); + }; + + const out = await Promise.all( + news_list.map(async (item) => { + const cache = await ctx.cache.get(item.href); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + + const response = await got({ + method: 'get', + url: item.href, + }); + + const description = ProcessFeed(response.data); + const single = { + title: item.title, + description, + pubDate: date(item.pubDate, +8), + link: item.href, + author: item.author, + }; + ctx.cache.set(item.href, JSON.stringify(single)); + return Promise.resolve(single); + }) + ); + + ctx.state.data = { + title: `cnBeta专题 - ${topicInfo}`, + link: topicUrl, + item: out ?? [], + description: `cnBeta专题 - ${topicInfo}`, + }; +};