fix(route/thepaper): add cookie (#20615)

* fix(route/thepaper): add cookie

* fix(route/thepaper): migrate from got to ofetch for API requests
This commit is contained in:
Tony 2025-12-02 15:51:17 +08:00 committed by GitHub
parent 15c703cafd
commit e108a0772b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 41 additions and 27 deletions

View File

@ -1,7 +1,7 @@
import { load } from 'cheerio';
import type { Route } from '@/types';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import utils from './utils';
@ -40,23 +40,26 @@ export const route: Route = {
async function handler(ctx) {
const id = ctx.req.param('id');
const channel_url = `https://m.thepaper.cn/channel/${id}`;
const channel_url_resp = await got(channel_url);
const channel_url_data = JSON.parse(load(channel_url_resp.data)('#__NEXT_DATA__').html());
const channelUrl = `https://m.thepaper.cn/channel/${id}`;
const channelUrlResp = await ofetch(channelUrl);
const $ = load(channelUrlResp.data);
const nextData = $('#__NEXT_DATA__').text();
const channelUrlData = JSON.parse(nextData);
const resp = await got.post('https://api.thepaper.cn/contentapi/nodeCont/getByChannelId', {
json: {
const resp = await ofetch('https://api.thepaper.cn/contentapi/nodeCont/getByChannelId', {
method: 'POST',
body: {
channelId: id,
},
});
const list = resp.data.data.list;
const list = resp.data.list;
const items = await Promise.all(list.map((item) => utils.ProcessItem(item, ctx)));
return {
title: `澎湃新闻频道 - ${utils.ChannelIdToName(id, channel_url_data)}`,
link: channel_url,
title: `澎湃新闻频道 - ${utils.ChannelIdToName(id, channelUrlData)}`,
link: channelUrl,
item: items,
itunes_author: '澎湃新闻',
image: utils.ExtractLogo(channel_url_resp),
image: utils.ExtractLogo(channelUrlResp),
};
}

View File

@ -1,7 +1,7 @@
import { load } from 'cheerio';
import type { Route } from '@/types';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import utils from './utils';
@ -30,8 +30,14 @@ export const route: Route = {
};
async function handler(ctx) {
const response = await got('https://m.thepaper.cn');
const data = JSON.parse(load(response.data)('#__NEXT_DATA__').html());
const response = await ofetch('https://m.thepaper.cn', {
headers: {
Cookie: 'blackAndWhiteMode=0; redTops=0;',
},
});
const $ = load(response);
const nextData = $('#__NEXT_DATA__').text();
const data = JSON.parse(nextData);
const list = [...data.props.pageProps.data.list, ...data.props.pageProps.topData.recommendImg];
const items = await Promise.all(list.map((item) => utils.ProcessItem(item, ctx)));

View File

@ -1,7 +1,7 @@
import { load } from 'cheerio';
import type { Route } from '@/types';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import utils from './utils';
@ -124,24 +124,27 @@ export const route: Route = {
async function handler(ctx) {
const id = ctx.req.param('id');
const list_url = `https://m.thepaper.cn/list/${id}`;
const list_url_resp = await got(list_url);
const list_url_data = JSON.parse(load(list_url_resp.data)('#__NEXT_DATA__').html());
const listUrl = `https://m.thepaper.cn/list/${id}`;
const listUrlResp = await ofetch(listUrl);
const $ = load(listUrlResp);
const nextData = $('#__NEXT_DATA__').text();
const listUrlData = JSON.parse(nextData);
const resp = await got.post('https://api.thepaper.cn/contentapi/nodeCont/getByNodeIdPortal', {
json: {
const resp = await ofetch('https://api.thepaper.cn/contentapi/nodeCont/getByNodeIdPortal', {
method: 'POST',
body: {
nodeId: id,
},
});
const pagePropsData = resp.data.data;
const pagePropsData = resp.data;
const list = pagePropsData.list;
const items = await Promise.all(list.map((item) => utils.ProcessItem(item, ctx)));
return {
title: `澎湃新闻栏目 - ${utils.ListIdToName(id, list_url_data)}`,
link: list_url,
title: `澎湃新闻栏目 - ${utils.ListIdToName(id, listUrlData)}`,
link: listUrl,
item: items,
itunes_author: '澎湃新闻',
image: pagePropsData.nodeInfo?.pic ?? utils.ExtractLogo(list_url_resp),
image: pagePropsData.nodeInfo?.pic ?? utils.ExtractLogo(listUrlResp),
};
}

View File

@ -3,7 +3,7 @@ import path from 'node:path';
import { load } from 'cheerio';
import cache from '@/utils/cache';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { parseDate, parseRelativeDate } from '@/utils/parse-date';
import { art } from '@/utils/render';
@ -28,8 +28,10 @@ export default {
}
const itemUrl = `https://m.thepaper.cn/${item.cornerLabelDesc && item.cornerLabelDesc === '短剧' ? 'series' : 'detail'}/${item.contId}`;
return cache.tryGet(`${itemUrl}${useOldMode ? ':old' : ''}`, async () => {
const res = await got(itemUrl);
const data = JSON.parse(load(res.data)('#__NEXT_DATA__').html());
const res = await ofetch(itemUrl);
const $ = load(res);
const nextData = $('#__NEXT_DATA__').text();
const data = JSON.parse(nextData);
const detailData = data.props.pageProps.detailData;
const contentDetail = detailData.contentDetail || detailData.liveDetail || detailData.specialDetail?.specialInfo;
if (!contentDetail) {
@ -88,5 +90,5 @@ export default {
},
ChannelIdToName: (nodeId, next_data) => next_data.props.appProps.menu.channelList.find((c) => c.nodeId.toString() === nodeId.toString()).name,
ListIdToName: (listId, next_data) => next_data.props.appProps.menu.channelList.flatMap((c) => c.childNodeList || []).find((l) => l.nodeId.toString() === listId.toString())?.name,
ExtractLogo: (response) => 'https://m.thepaper.cn' + load(response.data)('img.imageCover').attr('src'),
ExtractLogo: (response) => 'https://m.thepaper.cn' + load(response)('img.imageCover').attr('src'),
};