forked from DIYgod/RSSHub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[new route] 获取cnbeta topic feed (DIYgod#8015)
Co-authored-by: GitHub Action <[email protected]>
- Loading branch information
1 parent
1501116
commit 265a690
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`, | ||
}; | ||
}; |