Files
RSSHub/lib/v2/luogu/userBlog.js
T
Xiaoxu Guo 14503131bc feat(route): add Luogu user blog (#11527)
* feat(route): add Luogu user blog

* used html as description instead

* Do not set pubDate if there isn't any.

* used Luogu API
2023-01-02 02:17:04 +08:00

55 lines
1.7 KiB
JavaScript

const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const username = ctx.params.username;
// Convert from `username` to `uid`
const userApiUrl = `https://www.luogu.com.cn/api/user/search?keyword=${username}`;
const uid = await ctx.cache.tryGet(userApiUrl, async () => {
const rsp = await got(userApiUrl).json();
return rsp.users[0].uid;
});
// Get the title and link from the sitemap
const sitemapUrl = `https://www.luogu.com.cn/blog/${username}/_sitemap`;
const { title: blogTitle, link: blogBaseUrl } = await ctx.cache.tryGet(sitemapUrl, async () => {
const rsp = await got(sitemapUrl);
const $ = cheerio.load(rsp.data);
const title = $('title').text();
const link = $('.am-alert > a').attr('href');
return {
title,
link,
};
});
const posts = (await got(`https://www.luogu.com.cn/api/blog/lists?uid=${uid}`).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.get(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,
};
};