feat: add slug support for sspai author (#4116)

This commit is contained in:
hoilc
2020-02-29 20:13:09 +08:00
committed by GitHub
parent 7328f51840
commit 12e21218bb
2 changed files with 21 additions and 9 deletions
+1 -1
View File
@@ -725,7 +725,7 @@ Supported sub-sites:
### 作者
<Route author="SunShinenny" example="/sspai/author/796518" path="/sspai/author/:id" :paramsDesc="['作者 id,可在作者主页URL中找到']"/>
<Route author="SunShinenny hoilc" example="/sspai/author/796518" path="/sspai/author/:id" :paramsDesc="['作者 slug 或 id,slug 可在作者主页URL中找到,id 不易查找,仅作兼容']"/>
### 作者动态
+20 -8
View File
@@ -1,16 +1,32 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
async function getUserId(slug) {
const response = await got({
method: 'get',
url: `https://sspai.com/api/v1/user/slug/info/get?slug=${slug}`,
headers: {
Referer: `https://sspai.com/u/${slug}/posts`,
},
});
if (response.data.error !== 0) {
throw 'User Not Found';
}
return response.data.data.id;
}
module.exports = async (ctx) => {
const id = ctx.params.id;
const id = ctx.params.id.match(/^\d+$/) ? ctx.params.id : await getUserId(ctx.params.id);
const api_url = `https://sspai.com/api/v1/articles?offset=0&limit=20&author_ids=${id}&include_total=false`;
const resp = await got({
method: 'get',
url: api_url,
});
const data = resp.data.list;
let author_nickname = '';
let author_id = 0;
const author_slug = data[0].author.slug;
const author_nickname = data[0].author.nickname;
const items = await Promise.all(
data.map(async (item) => {
const link = `https://sspai.com/post/${item.id}`;
@@ -28,10 +44,6 @@ module.exports = async (ctx) => {
ctx.cache.set(key, description);
}
// 将作者名称和id赋值给nickname和id
author_nickname = item.author.nickname;
author_id = item.author.id;
return {
title: item.title.trim(),
description: description,
@@ -44,7 +56,7 @@ module.exports = async (ctx) => {
ctx.state.data = {
title: `${author_nickname} - 少数派作者`,
link: `https://sspai.com/user/${author_id}/posts`,
link: `https://sspai.com/u/${author_slug}/posts`,
description: `${author_nickname} 更新推送 `,
item: items,
};