mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-11 15:47:48 +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 axios = require('@/utils/axios');
|
||||||
|
const { ProcessFeed } = require('./utils');
|
||||||
|
|
||||||
module.exports = async (ctx) => {
|
module.exports = async (ctx) => {
|
||||||
const { id } = ctx.params;
|
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 { _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`)).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 Promise.all(
|
const items = await ProcessFeed(articles, `https://vocus.cc/${id}`, ctx.cache);
|
||||||
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}`,
|
|
||||||
}))
|
|
||||||
);
|
|
||||||
|
|
||||||
ctx.state.data = {
|
ctx.state.data = {
|
||||||
title: `${title} - 方格子`,
|
title: `${title} - 方格子`,
|
||||||
link: `https://vocus.cc/${id}/home`,
|
link,
|
||||||
description: abstract,
|
description: abstract,
|
||||||
item: items,
|
item: items,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
const axios = require('@/utils/axios');
|
const axios = require('@/utils/axios');
|
||||||
|
const { ProcessFeed } = require('./utils');
|
||||||
|
|
||||||
module.exports = async (ctx) => {
|
module.exports = async (ctx) => {
|
||||||
const id = ctx.params.id;
|
const id = ctx.params.id;
|
||||||
@@ -20,36 +21,7 @@ module.exports = async (ctx) => {
|
|||||||
},
|
},
|
||||||
})).data;
|
})).data;
|
||||||
|
|
||||||
const items = await Promise.all(
|
const items = await ProcessFeed(articles, link, ctx.cache);
|
||||||
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);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
ctx.state.data = {
|
ctx.state.data = {
|
||||||
title: `${fullname}的个人文章 - 方格子`,
|
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