Skip to content

Commit

Permalink
[new route] 获取cnbeta topic feed (DIYgod#8015)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
cczhong11 and actions-user authored Nov 27, 2021
1 parent 1501116 commit 265a690
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/new-media.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ pageClass: routes

<Route author="kt286 HaitianLiu" example="/cnbeta" path="/cnbeta"/>

### 主题

<Route author="cczhong11" example="/cnbeta/topic/453" path="/cnbeta/topic/:topic_id"/>

## Common App

### Blog
Expand Down
1 change: 1 addition & 0 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
75 changes: 75 additions & 0 deletions lib/routes/cnbeta/topic.js
Original file line number Diff line number Diff line change
@@ -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() + '<br>' + $('.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}`,
};
};

0 comments on commit 265a690

Please sign in to comment.