Files
RSSHub/lib/v2/luogu/userBlog.js
2023-01-02 11:49:24 +00:00

49 lines
1.5 KiB
JavaScript

const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const name = ctx.params.name;
const blogBaseUrl = `https://www.luogu.com.cn/blog/${name}/`;
// Fetch the uid & title
const { uid: blogUid, title: blogTitle } = await ctx.cache.tryGet(blogBaseUrl, async () => {
const rsp = await got(blogBaseUrl);
const $ = cheerio.load(rsp.data);
const uid = $("meta[name='blog-uid']").attr('content');
const name = $("meta[name='blog-name']").attr('content');
return {
uid,
title: `${name} - 洛谷博客`,
};
});
const posts = (await got(`https://www.luogu.com.cn/api/blog/lists?uid=${blogUid}`).json()).data.result.map((r) => ({
title: r.title,
link: `${blogBaseUrl}${r.identifier}`,
pubDate: new Date(r.postTime * 1000),
}));
// Get the full text
const item = await Promise.all(
posts.map((post) =>
ctx.cache.tryGet(post.link, async () => {
const rsp = await got(post.link);
const $ = cheerio.load(rsp.data);
return {
title: post.title,
link: post.link,
pubDate: post.pubDate,
description: $('#article-content').html(),
};
})
)
);
ctx.state.data = {
title: blogTitle,
link: blogBaseUrl,
item,
};
};