feat: 增加洛谷用户动态 RSS (#5611)

This commit is contained in:
solstice23 2020-09-06 03:56:47 +08:00 committed by GitHub
parent 7a89d32faf
commit 44bd219892
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 0 deletions

View File

@ -570,6 +570,10 @@ GitHub 官方也提供了一些 RSS:
<Route author="prnake" example="/luogu/contest" path="/luogu/contest"/>
### 用户动态
<Route author="solstice23" example="/luogu/user/feed/1" path="/luogu/user/feed/:uid" :paramsDesc="['用户 UID']"/>
## 码农俱乐部
### 话题

View File

@ -1399,6 +1399,7 @@ router.get('/bjp/apod', require('./routes/bjp/apod'));
// 洛谷
router.get('/luogu/daily/:id?', require('./routes/luogu/daily'));
router.get('/luogu/contest', require('./routes/luogu/contest'));
router.get('/luogu/user/feed/:uid', require('./routes/luogu/userFeed'));
// 决胜网
router.get('/juesheng', require('./routes/juesheng'));

View File

@ -0,0 +1,38 @@
const got = require('@/utils/got');
module.exports = async (ctx) => {
const getUsernameFromUID = async (uid) => {
const key = 'luogu-username-from-uid-' + uid;
let name = await ctx.cache.get(key);
if (!name) {
const nameResponse = await got({
method: 'get',
url: `https://www.luogu.com.cn/user/${uid}?_contentOnly=1`,
});
name = nameResponse.data.currentData.user.name;
ctx.cache.set(key, name);
}
return name;
};
const uid = ctx.params.uid;
const name = await getUsernameFromUID(uid);
const response = await got({
method: 'get',
url: `https://www.luogu.com.cn/api/feed/list?user=${uid}`,
});
const data = response.data.feeds.result;
ctx.state.data = {
title: `${name} 的洛谷动态`,
link: `https://www.luogu.com.cn/user/${uid}#activity`,
allowEmpty: true,
item: data.map((item) => ({
title: item.content,
description: item.content,
pubDate: new Date(item.time * 1000).toUTCString(),
link: `https://www.luogu.com.cn/user/${uid}#activity`,
guid: item.id,
})),
};
};