fix(route/xiaoyuzhou): fix find podcast name in eposide logic and use ofetch
This commit is contained in:
parent
001476b1fc
commit
c0237b45f4
|
|
@ -1,5 +1,5 @@
|
|||
import { Route, ViewType } from '@/types';
|
||||
import got from '@/utils/got';
|
||||
import ofetch from '@/utils/ofetch';
|
||||
import { load } from 'cheerio';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
|
||||
|
|
@ -30,43 +30,43 @@ export const route: Route = {
|
|||
|
||||
async function handler(ctx) {
|
||||
const id = ctx.req.param('id');
|
||||
let link = `https://www.xiaoyuzhoufm.com/podcast/${id}`;
|
||||
let response = await got({
|
||||
method: 'get',
|
||||
url: link,
|
||||
});
|
||||
let link;
|
||||
let response;
|
||||
let $;
|
||||
let page_data;
|
||||
|
||||
let $ = load(response.data);
|
||||
let page_data = JSON.parse($('#__NEXT_DATA__')[0].children[0].data);
|
||||
// First try podcast URL, if that fails try episode URL
|
||||
try {
|
||||
link = `https://www.xiaoyuzhoufm.com/podcast/${id}`;
|
||||
response = await ofetch(link);
|
||||
|
||||
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
|
||||
$ = load(response);
|
||||
const nextDataElement = $('#__NEXT_DATA__').get(0);
|
||||
page_data = JSON.parse(nextDataElement.children[0].data);
|
||||
|
||||
// If no episodes found, we should try episode URL
|
||||
if (!page_data.props.pageProps.podcast?.episodes) {
|
||||
throw new Error('No episodes found in podcast data');
|
||||
}
|
||||
} catch {
|
||||
// Try as episode instead
|
||||
link = `https://www.xiaoyuzhoufm.com/episode/${id}`;
|
||||
response = await got({
|
||||
method: 'get',
|
||||
url: link,
|
||||
});
|
||||
response = await ofetch(link);
|
||||
|
||||
$ = load(response);
|
||||
const podcastLink = $('a[href^="/podcast/"].name').attr('href');
|
||||
|
||||
$ = 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,
|
||||
});
|
||||
response = await ofetch(link);
|
||||
|
||||
$ = load(response.data);
|
||||
page_data = JSON.parse($('#__NEXT_DATA__')[0].children[0].data);
|
||||
$ = load(response);
|
||||
const nextDataElement = $('#__NEXT_DATA__').get(0);
|
||||
page_data = JSON.parse(nextDataElement.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,
|
||||
enclosure_url: item.enclosure.url,
|
||||
|
|
|
|||
Loading…
Reference in New Issue