fix(route/apple): fix paid podcast retrieval (#19937)

This commit is contained in:
Tony
2025-08-31 06:00:31 +08:00
committed by GitHub
parent f5d709b66a
commit e9038b7c6e
+58 -25
View File
@@ -1,7 +1,9 @@
import { Route } from '@/types';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import cache from '@/utils/cache';
import { config } from '@/config';
export const route: Route = {
path: '/podcast/:id/:region?',
@@ -21,7 +23,7 @@ export const route: Route = {
},
radar: [
{
source: ['podcasts.apple.com/:region/podcast/:id'],
source: ['podcasts.apple.com/:region/podcast/:showName/:id', 'podcasts.apple.com/:region/podcast/:id'],
},
],
name: '播客',
@@ -32,42 +34,73 @@ export const route: Route = {
async function handler(ctx) {
const { id, region } = ctx.req.param();
const link = `https://podcasts.apple.com/${region || `cn`}/podcast/${id}`;
const response = await got({
method: 'get',
url: link,
});
const numericId = id.match(/id(\d+)/)?.[1];
const baseUrl = 'https://podcasts.apple.com';
const link = `${baseUrl}/${region || `cn`}/podcast/${id}`;
const $ = load(response.data);
const response = await ofetch(link);
const $ = load(response);
const schemaShow = JSON.parse($(String.raw`#schema\:show`).text());
const serializedServerData = JSON.parse($('#serialized-server-data').text());
const seoEpisodes = schemaShow.workExample;
const originEpisodes = serializedServerData[0].data.shelves.find((item) => item.contentType === 'episode').items;
const header = serializedServerData[0].data.shelves.find((item) => item.contentType === 'showHeaderRegular').items[0];
const episodes = originEpisodes.map((item) => {
const bearerToken = await cache.tryGet(
'apple:podcast:bearer',
async () => {
const moduleAddress = new URL($('head script[type="module"]').attr('src'), baseUrl).href;
const modulesResponse = await ofetch(moduleAddress, {
parseResponse: (txt) => txt,
});
const bearerToken = modulesResponse.match(/="(eyJhbGci.*?)",/)[1];
return bearerToken as string;
},
config.cache.contentExpire,
false
);
const episodeReponse = await ofetch(`https://amp-api.podcasts.apple.com/v1/catalog/us/podcasts/${numericId}/episodes`, {
query: {
'extend[podcast-channels]': 'editorialArtwork,subscriptionArtwork,subscriptionOffers',
include: 'channel',
limit: 25,
with: 'entitlements',
l: 'en-US',
},
headers: {
Authorization: `Bearer ${bearerToken}`,
Origin: baseUrl,
},
});
const episodes = episodeReponse.data.map(({ attributes: item }) => {
// Try to keep line breaks in the description
const matchedSeoEpisode = seoEpisodes.find((seoEpisode) => seoEpisode.name === item.title) || null;
const episodeDescription = (matchedSeoEpisode ? matchedSeoEpisode.description : item.summary).replaceAll('\n', '<br>');
const offer = item.offers[0];
return {
title: item.title,
enclosure_url: item.playAction.episodeOffer.streamUrl,
enclosure_type: 'audio/mp4',
itunes_duration: item.duration,
link: item.playAction.episodeOffer.storeUrl,
pubDate: parseDate(item.releaseDate),
description: episodeDescription,
title: item.name,
enclosure_url: item.assetUrl || offer.hlsUrl,
enclosure_type: item.assetUrl ? 'audio/mp4' : 'application/vnd.apple.mpegurl',
itunes_duration: (item.durationInMilliseconds || offer.durationInMilliseconds) / 1000,
link: item.url,
pubDate: parseDate(item.releaseDateTime),
description: item.description.standard.replaceAll('\n', '<br>'),
author: item.artistName,
itunes_item_image: item.artwork.url.replace(/\{w\}x\{h\}(?:\{c\}|bb)\.\{f\}/, '3000x3000bb.webp'),
category: item.genreNames,
};
});
const channel = episodeReponse.data.find((d) => d.type === 'podcast-episodes').relationships.channel.data.find((d) => d.type === 'podcast-channels').attributes;
return {
title: header.title,
link: header.contextAction.podcastOffer.storeUrl,
title: channel.name,
link: channel.url,
itunes_author: header.contextAction.podcastOffer.author,
item: episodes,
description: header.description.replaceAll('\n', ' '),
description: (header.description || channel.description.standard).replaceAll('\n', ' '),
image: (channel.logoArtwork || channel.subscriptionArtwork).url.replace(/\{w\}x\{h\}(?:\{c\}|bb)\.\{f\}/, '3000x3000bb.webp'),
itunes_category: header.metadata.find((d) => Object.hasOwn(d, 'category')).category?.title || header.metadata.find((d) => Object.hasOwn(d, 'category')).category,
};
}