fix(route): add pubDate for 知乎日报 (#7660)
Co-authored-by: auto-bot-ty <auto-bot-ty@git@com>
This commit is contained in:
parent
978dc93f08
commit
5284b7e18d
|
|
@ -1,34 +1,36 @@
|
|||
const got = require('@/utils/got');
|
||||
const utils = require('./utils');
|
||||
const { parseDate } = require('@/utils/parse-date');
|
||||
|
||||
// 参考:https://github.com/izzyleung/ZhihuDailyPurify/wiki/%E7%9F%A5%E4%B9%8E%E6%97%A5%E6%8A%A5-API-%E5%88%86%E6%9E%90
|
||||
// 文章给出了v4版 api的信息,包含全文api
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const api = 'https://news-at.zhihu.com/api/4/news';
|
||||
const listRes = await got({
|
||||
method: 'get',
|
||||
url: 'https://news-at.zhihu.com/api/4/news/latest',
|
||||
url: `${api}/latest`,
|
||||
headers: {
|
||||
...utils.header,
|
||||
Referer: 'https://news-at.zhihu.com/api/4/news/latest',
|
||||
Referer: `${api}/latest`,
|
||||
},
|
||||
});
|
||||
// 根据api的说明,过滤掉极个别站外链接
|
||||
const storyList = listRes.data.stories.filter((el) => el.type === 0);
|
||||
const resultItem = await Promise.all(
|
||||
storyList.map(async (story) => {
|
||||
const url = 'https://news-at.zhihu.com/api/4/news/' + story.id;
|
||||
const item = {
|
||||
title: story.title,
|
||||
description: '',
|
||||
link: 'https://daily.zhihu.com/story/' + story.id,
|
||||
};
|
||||
const key = 'daily' + story.id;
|
||||
const value = await ctx.cache.get(key);
|
||||
const date = listRes.data.date;
|
||||
|
||||
if (value) {
|
||||
item.description = value;
|
||||
} else {
|
||||
const articleList = storyList.map((item) => ({
|
||||
title: item.title,
|
||||
pubDate: parseDate(date, 'YYYYMMDD'),
|
||||
link: item.url,
|
||||
guid: item.url,
|
||||
storyId: item.id,
|
||||
}));
|
||||
|
||||
const items = await Promise.all(
|
||||
articleList.map(async (item) => {
|
||||
const url = `${api}/${item.storyId}`;
|
||||
const description = await ctx.cache.tryGet(item.link, async () => {
|
||||
const storyDetail = await got({
|
||||
method: 'get',
|
||||
url: url,
|
||||
|
|
@ -36,10 +38,9 @@ module.exports = async (ctx) => {
|
|||
Referer: url,
|
||||
},
|
||||
});
|
||||
item.description = utils.ProcessImage(storyDetail.data.body.replace(/<div class="meta">([\s\S]*?)<\/div>/g, '<strong>$1</strong>').replace(/<\/?h2.*?>/g, ''));
|
||||
ctx.cache.set(key, item.description);
|
||||
}
|
||||
|
||||
return utils.ProcessImage(storyDetail.data.body.replace(/<div class="meta">([\s\S]*?)<\/div>/g, '<strong>$1</strong>').replace(/<\/?h2.*?>/g, ''));
|
||||
});
|
||||
item.description = description;
|
||||
return Promise.resolve(item);
|
||||
})
|
||||
);
|
||||
|
|
@ -48,6 +49,6 @@ module.exports = async (ctx) => {
|
|||
title: '知乎日报',
|
||||
link: 'https://daily.zhihu.com',
|
||||
description: '每天3次,每次7分钟',
|
||||
item: resultItem,
|
||||
item: items,
|
||||
};
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue