fix: Luogu blog uid (#11537)

This commit is contained in:
Xiaoxu Guo 2023-01-02 19:49:24 +08:00 committed by GitHub
parent 5c7aff76a3
commit fb68956739
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 21 deletions

View File

@ -1015,7 +1015,7 @@ GitHub 官方也提供了一些 RSS:
### 用户博客
<Route author="ftiasch" example="/luogu/user/blog/ftiasch" path="/luogu/user/blog/:username" :paramsDesc="['用户名']" radar="1" rssbud="1"/>
<Route author="ftiasch" example="/luogu/user/blog/ftiasch" path="/luogu/user/blog/:name" :paramsDesc="['博客名称']" radar="1" rssbud="1"/>
## 码农俱乐部

View File

@ -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',
},
],
},

View File

@ -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'));
};

View File

@ -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,