From fb689567390db7bc9b45b757e7ea7094a5870962 Mon Sep 17 00:00:00 2001 From: Xiaoxu Guo Date: Mon, 2 Jan 2023 19:49:24 +0800 Subject: [PATCH] fix: Luogu blog uid (#11537) --- docs/programming.md | 2 +- lib/v2/luogu/radar.js | 4 ++-- lib/v2/luogu/router.js | 2 +- lib/v2/luogu/userBlog.js | 28 +++++++++++----------------- 4 files changed, 15 insertions(+), 21 deletions(-) diff --git a/docs/programming.md b/docs/programming.md index f552c9bdf..c0bb6329a 100644 --- a/docs/programming.md +++ b/docs/programming.md @@ -1015,7 +1015,7 @@ GitHub 官方也提供了一些 RSS: ### 用户博客 - + ## 码农俱乐部 diff --git a/lib/v2/luogu/radar.js b/lib/v2/luogu/radar.js index 3fdc9673c..a3f60d70e 100644 --- a/lib/v2/luogu/radar.js +++ b/lib/v2/luogu/radar.js @@ -23,8 +23,8 @@ module.exports = { { title: '用户博客', docs: 'https://docs.rsshub.app/programming.html#luo-gu', - source: ['/blog/:username'], - target: '/luogu/user/blog/:username', + source: ['/blog/:name'], + target: '/luogu/user/blog/:name', }, ], }, diff --git a/lib/v2/luogu/router.js b/lib/v2/luogu/router.js index 9d5f1c53e..dd21f9118 100644 --- a/lib/v2/luogu/router.js +++ b/lib/v2/luogu/router.js @@ -2,5 +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')); + router.get('/user/blog/:name', require('./userBlog')); }; diff --git a/lib/v2/luogu/userBlog.js b/lib/v2/luogu/userBlog.js index 8bd6b939b..b488d4a31 100644 --- a/lib/v2/luogu/userBlog.js +++ b/lib/v2/luogu/userBlog.js @@ -2,29 +2,23 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); module.exports = async (ctx) => { - const username = ctx.params.username; + const name = ctx.params.name; - // 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; - }); + const blogBaseUrl = `https://www.luogu.com.cn/blog/${name}/`; - // 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); + // 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 title = $('title').text(); - const link = $('.am-alert > a').attr('href'); + const uid = $("meta[name='blog-uid']").attr('content'); + const name = $("meta[name='blog-name']").attr('content'); return { - title, - link, + uid, + title: `${name} - 洛谷博客`, }; }); - const posts = (await got(`https://www.luogu.com.cn/api/blog/lists?uid=${uid}`).json()).data.result.map((r) => ({ + 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), @@ -34,7 +28,7 @@ module.exports = async (ctx) => { const item = await Promise.all( posts.map((post) => ctx.cache.tryGet(post.link, async () => { - const rsp = await got.get(post.link); + const rsp = await got(post.link); const $ = cheerio.load(rsp.data); return { title: post.title,