diff --git a/docs/programming.md b/docs/programming.md index bc2f876ad..f552c9bdf 100644 --- a/docs/programming.md +++ b/docs/programming.md @@ -1013,6 +1013,10 @@ GitHub 官方也提供了一些 RSS: +### 用户博客 + + + ## 码农俱乐部 ### 话题 diff --git a/lib/v2/luogu/maintainer.js b/lib/v2/luogu/maintainer.js index 8a7d98e90..0f8f11a1e 100644 --- a/lib/v2/luogu/maintainer.js +++ b/lib/v2/luogu/maintainer.js @@ -2,4 +2,5 @@ module.exports = { '/contest': ['prnake'], '/daily/:id?': ['LogicJake ', 'prnake ', 'nczitzk'], '/user/feed/:uid': ['solstice23'], + '/user/blog/:username': ['ftiasch'], }; diff --git a/lib/v2/luogu/radar.js b/lib/v2/luogu/radar.js index 7b8172918..3fdc9673c 100644 --- a/lib/v2/luogu/radar.js +++ b/lib/v2/luogu/radar.js @@ -20,6 +20,12 @@ module.exports = { source: ['/user/:uid'], target: '/luogu/user/feed/:uid', }, + { + title: '用户博客', + docs: 'https://docs.rsshub.app/programming.html#luo-gu', + source: ['/blog/:username'], + target: '/luogu/user/blog/:username', + }, ], }, }; diff --git a/lib/v2/luogu/router.js b/lib/v2/luogu/router.js index eb1e9da77..9d5f1c53e 100644 --- a/lib/v2/luogu/router.js +++ b/lib/v2/luogu/router.js @@ -2,4 +2,5 @@ module.exports = (router) => { router.get('/contest', require('./contest')); router.get('/daily/:id?', require('./daily')); router.get('/user/feed/:uid', require('./userFeed')); + router.get('/user/blog/:username', require('./userBlog')); }; diff --git a/lib/v2/luogu/userBlog.js b/lib/v2/luogu/userBlog.js new file mode 100644 index 000000000..8bd6b939b --- /dev/null +++ b/lib/v2/luogu/userBlog.js @@ -0,0 +1,54 @@ +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, + }; +};