Skip to content

Commit

Permalink
feat(route): also support podcast id in xiaoyuzhou
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudoyu committed Oct 11, 2024
1 parent a4daf28 commit b6961b9
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions lib/routes/xiaoyuzhou/podcast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const route: Route = {
categories: ['multimedia', 'popular'],
view: ViewType.Audios,
example: '/xiaoyuzhou/podcast/6021f949a789fca4eff4492c',
parameters: { id: '播客id,可以在小宇宙播客的 URL 中找到' },
parameters: { id: '播客 id 或单集 id,可以在小宇宙播客的 URL 中找到' },
features: {
requireConfig: false,
requirePuppeteer: false,
Expand All @@ -19,25 +19,53 @@ export const route: Route = {
},
radar: [
{
source: ['xiaoyuzhoufm.com/podcast/:id'],
source: ['xiaoyuzhoufm.com/podcast/:id', 'xiaoyuzhoufm.com/episode/:id'],
},
],
name: '播客',
maintainers: ['hondajojo', 'jtsang4'],
maintainers: ['hondajojo', 'jtsang4', 'pseudoyu'],
handler,
url: 'xiaoyuzhoufm.com/',
};

async function handler(ctx) {
const link = `https://www.xiaoyuzhoufm.com/podcast/${ctx.req.param('id')}`;
const response = await got({
const id = ctx.req.param('id');
let link = `https://www.xiaoyuzhoufm.com/podcast/${id}`;
let response = await got({
method: 'get',
url: link,
});

const $ = load(response.data);
let $ = load(response.data);
let page_data = JSON.parse($('#__NEXT_DATA__')[0].children[0].data);

const page_data = JSON.parse($('#__NEXT_DATA__')[0].children[0].data);
if (!page_data.props.pageProps.podcast?.episodes) {
// If episodes are not found, it might be an episode page
// Try to get the podcast id from the episode page
link = `https://www.xiaoyuzhoufm.com/episode/${id}`;
response = await got({
method: 'get',
url: link,
});

$ = load(response.data);
const podcastLink = $('.jsx-605929003.podcast-title .jsx-605929003.name').attr('href');
if (podcastLink) {
const podcastId = podcastLink.split('/').pop();
link = `https://www.xiaoyuzhoufm.com/podcast/${podcastId}`;
response = await got({
method: 'get',
url: link,
});

$ = load(response.data);
page_data = JSON.parse($('#__NEXT_DATA__')[0].children[0].data);
}
}

if (!page_data.props.pageProps.podcast?.episodes) {
throw new Error('Failed to fetch podcast episodes');
}

const episodes = page_data.props.pageProps.podcast.episodes.map((item) => ({
title: item.title,
Expand Down

0 comments on commit b6961b9

Please sign in to comment.