Skip to content

Commit

Permalink
feat(route): thepaper user (DIYgod#17666)
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyRL authored Nov 21, 2024
1 parent e286c77 commit 7416065
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/routes/thepaper/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import got from '@/utils/got';

export const route: Route = {
path: '/channel/:id',
categories: ['traditional-media'],
categories: ['new-media'],
example: '/thepaper/channel/25950',
parameters: { id: '频道 id,可在频道页 URL 中找到' },
features: {
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/thepaper/factpaper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import path from 'node:path';

export const route: Route = {
path: '/factpaper/:status?',
categories: ['traditional-media'],
categories: ['new-media'],
example: '/thepaper/factpaper',
parameters: { status: '状态 id,可选 `1` 即 有定论 或 `0` 即 核查中,默认为 `1`' },
features: {
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/thepaper/featured.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import got from '@/utils/got';

export const route: Route = {
path: '/featured',
categories: ['traditional-media'],
categories: ['new-media'],
example: '/thepaper/featured',
parameters: {},
features: {
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/thepaper/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import got from '@/utils/got';

export const route: Route = {
path: '/list/:id',
categories: ['traditional-media'],
categories: ['new-media'],
example: '/thepaper/list/25457',
parameters: { id: '栏目 id,可在栏目页 URL 中找到' },
features: {
Expand Down
174 changes: 174 additions & 0 deletions lib/routes/thepaper/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import { Route } from '@/types';
import * as cheerio from 'cheerio';
import ofetch from '@/utils/ofetch';
import cache from '@/utils/cache';
import { parseDate } from '@/utils/parse-date';

export const route: Route = {
path: '/user/:pphId',
categories: ['new-media'],
example: '/thepaper/user/4221423',
parameters: { pphId: '澎湃号 id,可在澎湃号页 URL 中找到' },
name: '澎湃号',
maintainers: ['TonyRL'],
handler,
};

interface AuthorInfo {
userId: number;
sname: string;
pic: string;
isAuth: string;
userType: string;
perDesc: string;
mobile: null;
isOrder: string;
sex: string;
area: string;
attentionNum: string;
fansNum: string;
praiseNum: null;
pph: boolean;
normalUser: boolean;
mobForwardType: number;
authInfo: string;
mail: string;
pphImpactNum: string;
wonderfulCommentCount: string;
location: string;
lastLoginDate: null;
}

interface PPHContentResponse {
code: number;
data: Data;
desc: string;
time: number;
}

interface Data {
hasNext: boolean;
startTime: number;
list: ListItem[];
nodeInfo: null;
tagInfo: null;
moreNodeInfo: null;
pageNum: null;
pageSize: null;
pages: null;
total: null;
prevPageNum: null;
nextPageNum: null;
excludeContIds: null;
contCont: null;
filterIds: null;
updateCount: null;
}

interface ListItem {
contId: string;
isOutForword: string;
isOutForward: string;
forwardType: string;
mobForwardType: number;
interactionNum: string;
praiseTimes: string;
pic: string;
imgCardMode: number;
smallPic: string;
sharePic: string;
pubTime: string;
pubTimeNew: string;
name: string;
closePraise: string;
authorInfo: AuthorInfo;
nodeId: number;
contType: number;
pubTimeLong: number;
specialNodeId: number;
cardMode: string;
dataObjId: number;
closeFrontComment: boolean;
isSupInteraction: boolean;
hideVideoFlag: boolean;
praiseStyle: number;
isSustainedFly: number;
softLocType: number;
closeComment: boolean;
voiceInfo: VoiceInfo;
softAdTypeStr: string;
}

interface VoiceInfo {
imgSrc: string;
isHaveVoice: string;
}

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

const mobileBuildId = (await cache.tryGet('thepaper:m:buildId', async () => {
const response = await ofetch('https://m.thepaper.cn');
const $ = cheerio.load(response);
const nextData = JSON.parse($('script#__NEXT_DATA__').text());
return nextData.buildId;
})) as string;

const userInfo = (await cache.tryGet(`thepaper:user:${pphId}`, async () => {
const response = await ofetch(`https://api.thepaper.cn/userservice/user/homePage/${pphId}`, {
headers: {
'Client-Type': '2',
Origin: 'https://m.thepaper.cn',
Referer: 'https://m.thepaper.cn/',
},
});
return response.userInfo;
})) as AuthorInfo;

const response = await ofetch<PPHContentResponse>('https://api.thepaper.cn/contentapi/cont/pph/user', {
method: 'POST',
body: {
pageSize: 10,
pageNum: 1,
contType: 0,
excludeContIds: [],
pphId,
startTime: 0,
},
});

const list = response.data.list.map((item) => ({
title: item.name,
link: `https://www.thepaper.cn/newsDetail_forward_${item.contId}`,
pubDate: parseDate(item.pubTimeLong),
author: item.authorInfo.sname,
contId: item.contId,
image: item.pic,
}));

const items = await Promise.all(
list.map((item) =>
cache.tryGet(item.link, async () => {
const response = await ofetch(`https://m.thepaper.cn/_next/data/${mobileBuildId}/detail/${item.contId}.json`, {
query: {
id: item.contId,
},
});

item.description = response.pageProps.detailData.contentDetail.content;
item.updated = parseDate(response.pageProps.detailData.contentDetail.updateTime);

return item;
})
)
);

return {
title: userInfo.sname,
description: userInfo.perDesc,
link: `https://www.thepaper.cn/user_${pphId}`,
item: items,
itunes_author: userInfo.sname,
image: userInfo.pic,
};
}

0 comments on commit 7416065

Please sign in to comment.