Skip to content

Commit

Permalink
feat(route): luogu (#17804)
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyRL authored Dec 4, 2024
1 parent 8b765e6 commit 0a5e907
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/routes/luogu/daily.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const route: Route = {
},
],
name: '日报',
maintainers: ['LogicJake ', 'prnake ', 'nczitzk'],
maintainers: ['LogicJake', 'prnake', 'nczitzk'],
handler,
url: 'luogu.com.cn/discuss/47327',
};
Expand Down
75 changes: 75 additions & 0 deletions lib/routes/luogu/user-article.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import ofetch from '@/utils/ofetch';
import * as cheerio from 'cheerio';
import { getUserInfoFromUID } from './utils';
import { parseDate } from '@/utils/parse-date';

export const route: Route = {
path: '/user/article/:uid',
categories: ['programming'],
example: '/luogu/user/article/1',
parameters: { name: '用户 UID' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['luogu.com/user/:uid'],
},
{
source: ['luogu.com.cn/user/:uid'],
},
],
name: '用户文章',
maintainers: ['TonyRL'],
handler,
};

const baseUrl = 'https://www.luogu.com';

async function handler(ctx) {
const { uid } = ctx.req.param();

const userInfo = await getUserInfoFromUID(uid);
const response = await ofetch(`${baseUrl}/api/article/find`, {
query: {
user: uid,
page: 1,
ascending: false,
},
});

const posts = response.articles.result.map((item) => ({
title: item.title,
link: `${baseUrl}/article/${item.lid}`,
author: item.author.name,
pubDate: parseDate(item.time, 'X'),
description: item.content,
}));

const item = await Promise.all(
posts.map((item) =>
cache.tryGet(item.link, async () => {
const response = await ofetch(item.link);
const $ = cheerio.load(response);
item.description = $('div#rendered-preview').html();

return item;
})
)
);

return {
title: `${userInfo.name} 的个人中心 - 洛谷 | 计算机科学教育新生态`,
description: userInfo.description,
link: `${baseUrl}/user/${uid}#article`,
image: userInfo.avatar,
item,
};
}
5 changes: 4 additions & 1 deletion lib/routes/luogu/user-blog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ export const route: Route = {
supportScihub: false,
},
radar: [
{
source: ['luogu.com/blog/:name'],
},
{
source: ['luogu.com.cn/blog/:name'],
},
],
name: '用户博客',
maintainers: [],
maintainers: ['ftiasch'],
handler,
};

Expand Down
19 changes: 9 additions & 10 deletions lib/routes/luogu/user-feed.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';
import MarkdownIt from 'markdown-it';
import { getUserInfoFromUID } from './utils';
const md = MarkdownIt();

export const route: Route = {
Expand All @@ -19,6 +19,9 @@ export const route: Route = {
supportScihub: false,
},
radar: [
{
source: ['luogu.com/user/:uid'],
},
{
source: ['luogu.com.cn/user/:uid'],
},
Expand All @@ -29,27 +32,23 @@ export const route: Route = {
};

async function handler(ctx) {
const getUsernameFromUID = (uid) =>
cache.tryGet('luogu:username:' + uid, async () => {
const { data } = await got(`https://www.luogu.com.cn/user/${uid}?_contentOnly=1`);
return data.currentData.user.name;
});

const uid = ctx.req.param('uid');
const name = await getUsernameFromUID(uid);
const userInfo = await getUserInfoFromUID(uid);
const { data: response } = await got(`https://www.luogu.com.cn/api/feed/list?user=${uid}`);

const data = response.feeds.result;

return {
title: `${name} 的洛谷动态`,
title: `${userInfo.name} 的洛谷动态`,
description: userInfo.description,
image: userInfo.avatar,
link: `https://www.luogu.com.cn/user/${uid}#activity`,
allowEmpty: true,
item: data.map((item) => ({
title: item.content,
description: md.render(item.content),
pubDate: parseDate(item.time, 'X'),
author: name,
author: userInfo.name,
link: `https://www.luogu.com.cn/user/${uid}#activity`,
guid: item.id,
})),
Expand Down
17 changes: 17 additions & 0 deletions lib/routes/luogu/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import cache from '@/utils/cache';
import ofetch from '@/utils/got';

export const getUserInfoFromUID = (uid) =>
cache.tryGet('luogu:username:' + uid, async () => {
const data = await ofetch(`https://www.luogu.com/user/${uid}`, {
query: {
_contentOnly: 1,
},
});

return {
name: data.data.currentData.user.name,
description: data.data.currentData.user.slogan,
avatar: data.data.currentData.user.avatar,
};
}) as Promise<{ name: string; description: string; avatar: string }>;

0 comments on commit 0a5e907

Please sign in to comment.