mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-12 08:10:32 +08:00
refactor: 重构方格子 vocus 全文缓存 (#2151)
* refactor: 重构方格子 vocus 全文缓存 * fix: return await Promise.all()
This commit is contained in:
@@ -1,23 +1,18 @@
|
||||
const axios = require('@/utils/axios');
|
||||
const { ProcessFeed } = require('./utils');
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const { id } = ctx.params;
|
||||
const link = `https://vocus.cc/${id}/home`;
|
||||
const headers = { Referer: link };
|
||||
|
||||
const { _id, title, abstract } = (await axios.get(`https://api.sosreader.com/api/publication/${id}`)).data;
|
||||
const { articles } = (await axios.get(`https://api.sosreader.com/api/articles?publicationId=${_id}&status=2&num=10&page=1`)).data;
|
||||
const items = await Promise.all(
|
||||
articles.map(async (item) => ({
|
||||
title: item.title,
|
||||
author: item.user.fullname,
|
||||
description: (await axios.get(`https://api.sosreader.com/api/article/${item._id}`)).data.article.content,
|
||||
pubDate: new Date(item.updatedAt).toUTCString(),
|
||||
link: `https://vocus.cc/${id}/${item._id}`,
|
||||
}))
|
||||
);
|
||||
const { _id, title, abstract } = (await axios.get(`https://api.sosreader.com/api/publication/${id}`, headers)).data;
|
||||
const { articles } = (await axios.get(`https://api.sosreader.com/api/articles?publicationId=${_id}&status=2&num=10&page=1`, headers)).data;
|
||||
const items = await ProcessFeed(articles, `https://vocus.cc/${id}`, ctx.cache);
|
||||
|
||||
ctx.state.data = {
|
||||
title: `${title} - 方格子`,
|
||||
link: `https://vocus.cc/${id}/home`,
|
||||
link,
|
||||
description: abstract,
|
||||
item: items,
|
||||
};
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
const axios = require('@/utils/axios');
|
||||
const { ProcessFeed } = require('./utils');
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const id = ctx.params.id;
|
||||
@@ -20,36 +21,7 @@ module.exports = async (ctx) => {
|
||||
},
|
||||
})).data;
|
||||
|
||||
const items = await Promise.all(
|
||||
articles.map(async (item) => {
|
||||
const itemUrl = `https://api.sosreader.com/api/article/${item._id}`;
|
||||
|
||||
const cache = await ctx.cache.get(itemUrl);
|
||||
if (cache) {
|
||||
return Promise.resolve(JSON.parse(cache));
|
||||
}
|
||||
|
||||
const response = await axios({
|
||||
method: 'get',
|
||||
url: itemUrl,
|
||||
headers: {
|
||||
Referer: link,
|
||||
},
|
||||
});
|
||||
|
||||
const description = response.data.article.content;
|
||||
|
||||
const single = {
|
||||
title: item.title,
|
||||
link: `https://vocus.cc/@${id}/${item._id}`,
|
||||
author: item.user.fullname,
|
||||
description: description,
|
||||
pubDate: new Date(item.updatedAt).toUTCString(),
|
||||
};
|
||||
ctx.cache.set(itemUrl, JSON.stringify(single));
|
||||
return Promise.resolve(single);
|
||||
})
|
||||
);
|
||||
const items = await ProcessFeed(articles, link, ctx.cache);
|
||||
|
||||
ctx.state.data = {
|
||||
title: `${fullname}的个人文章 - 方格子`,
|
||||
|
||||
29
lib/routes/vocus/utils.js
Normal file
29
lib/routes/vocus/utils.js
Normal file
@@ -0,0 +1,29 @@
|
||||
const axios = require('@/utils/axios');
|
||||
|
||||
const load = async (id, headers) => ({
|
||||
description: (await axios.get(`https://api.sosreader.com/api/article/${id}`, headers)).data.article.content,
|
||||
});
|
||||
|
||||
const ProcessFeed = async (list, host, caches) => {
|
||||
const headers = { Referer: host };
|
||||
|
||||
return await Promise.all(
|
||||
list.map(async (item) => {
|
||||
const itemUrl = `https://api.sosreader.com/api/article/${item._id}`;
|
||||
|
||||
const single = {
|
||||
title: item.title,
|
||||
author: item.user.fullname,
|
||||
pubDate: new Date(item.updatedAt).toUTCString(),
|
||||
link: `${host}/${item._id}`,
|
||||
};
|
||||
|
||||
const other = await caches.tryGet(itemUrl, single, async () => await load(itemUrl, headers));
|
||||
return Promise.resolve(Object.assign({}, single, other));
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
ProcessFeed,
|
||||
};
|
||||
Reference in New Issue
Block a user