-
Notifications
You must be signed in to change notification settings - Fork 9.9k
feat(route): add iVoox podcast feed #22072
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 7 commits
c769de1
16a0996
5cdbcff
66252f0
ac27e2d
9c3a298
673ba90
f33a13c
c8f7c2c
f8b33cb
b4b2508
91508cc
0a3d19d
4eb1429
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import type { Namespace } from '@/types'; | ||
|
|
||
| export const namespace: Namespace = { | ||
| name: 'iVoox', | ||
| url: 'www.ivoox.com', | ||
| categories: ['multimedia'], | ||
| lang: 'es', | ||
| }; |
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,205 @@ | ||||
| import { load } from 'cheerio'; | ||||
| import { decodeHTML } from 'entities'; | ||||
|
|
||||
| import InvalidParameterError from '@/errors/types/invalid-parameter'; | ||||
| import type { Data, DataItem, Route } from '@/types'; | ||||
| import { ViewType } from '@/types'; | ||||
| import cache from '@/utils/cache'; | ||||
| import ofetch from '@/utils/ofetch'; | ||||
| import { parseDate } from '@/utils/parse-date'; | ||||
|
|
||||
| const rootUrl = 'https://www.ivoox.com'; | ||||
| const itunesAuthorSelector = String.raw`itunes\:author`; | ||||
| const itunesCategorySelector = String.raw`itunes\:category`; | ||||
| const itunesDurationSelector = String.raw`itunes\:duration`; | ||||
| const itunesExplicitSelector = String.raw`itunes\:explicit`; | ||||
| const itunesImageSelector = String.raw`itunes\:image`; | ||||
|
|
||||
| export const route: Route = { | ||||
| path: '/podcast/:id', | ||||
| categories: ['multimedia'], | ||||
| example: '/ivoox/podcast/11178419', | ||||
| parameters: { | ||||
| id: 'Podcast ID, can be found in the iVoox podcast URL after `_sq_f`, for example `11178419` or `f11178419`', | ||||
| }, | ||||
| features: { | ||||
| requireConfig: false, | ||||
| requirePuppeteer: false, | ||||
| antiCrawler: false, | ||||
| supportRadar: true, | ||||
| supportBT: false, | ||||
| supportPodcast: true, | ||||
| supportScihub: false, | ||||
| }, | ||||
| radar: [ | ||||
| { | ||||
| source: ['www.ivoox.com/podcast-*_sq_f:id_1.html', 'www.ivoox.com/en/podcast-*_sq_f:id_1.html', 'www.ivoox.com/*_sq_f:id_1.html', 'www.ivoox.com/en/*_sq_f:id_1.html'], | ||||
| target: (params) => `/podcast/${params.id}`, | ||||
| }, | ||||
| ], | ||||
| name: 'Podcast', | ||||
| url: 'www.ivoox.com', | ||||
| maintainers: ['guillevc'], | ||||
| handler, | ||||
| description: 'Transforms an iVoox podcast page into an RSS feed that exposes the full episode audio enclosures instead of the short clip feed.', | ||||
| view: ViewType.Audios, | ||||
| }; | ||||
|
|
||||
| async function handler(ctx): Promise<Data> { | ||||
| const id = normalizePodcastId(ctx.req.param('id')); | ||||
|
guillevc marked this conversation as resolved.
Outdated
|
||||
| const limit = Number.parseInt(ctx.req.query('limit') ?? '10', 10); | ||||
| const feedUrl = `${rootUrl}/feed_fg_f${id}_filtro_1.xml`; | ||||
| const response = await ofetch(feedUrl, { | ||||
|
TonyRL marked this conversation as resolved.
Outdated
|
||||
| parseResponse: (txt) => txt, | ||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why specify the default option https://github.com/unjs/ofetch/blob/dfbe3ca4ef8a22fc023fca5a5ef530e525f5e523/src/fetch.ts#L230 again? Does the request fail if you don't specify the default option again?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tried removing it... ofetch returns a blob for
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right. The feed returns |
||||
| }); | ||||
|
|
||||
| const $ = load(response, { xmlMode: true }); | ||||
| const channel = $('channel').first(); | ||||
|
guillevc marked this conversation as resolved.
Outdated
|
||||
| if (!channel.length) { | ||||
| throw new Error(`Invalid iVoox podcast feed for ID ${id}`); | ||||
| } | ||||
|
|
||||
| const items = ( | ||||
| await Promise.all( | ||||
| channel | ||||
| .children('item') | ||||
| .toArray() | ||||
| .slice(0, Number.isNaN(limit) ? 10 : limit) | ||||
| .map(async (element): Promise<DataItem | undefined> => { | ||||
| const itemElement = $(element); | ||||
| const enclosure = itemElement.children('enclosure').first(); | ||||
|
guillevc marked this conversation as resolved.
Outdated
|
||||
| const enclosureUrl = enclosure.attr('url'); | ||||
| const itemId = normalizeEpisodeId(childText(itemElement, 'guid') || childText(itemElement, 'link')); | ||||
|
guillevc marked this conversation as resolved.
Outdated
guillevc marked this conversation as resolved.
Outdated
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since https://github.com/DIYgod/RSSHub/pull/22072/changes#r3342448784 is marked as resolved by you, could you explain how is it resolved?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for not explaining. The intermediate variable was inlined in commit
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you show me where is commit
TonyRL marked this conversation as resolved.
Outdated
|
||||
|
|
||||
| if (!enclosureUrl) { | ||||
| return; | ||||
| } | ||||
|
|
||||
| const title = childText(itemElement, 'title'); | ||||
| const image = childAttr(itemElement, itunesImageSelector, 'href'); | ||||
| const length = parseOptionalInteger(enclosure.attr('length')); | ||||
| const resolvedEnclosureUrl = itemId ? await resolveEpisodeAudioUrl(itemId, enclosureUrl, childText(itemElement, 'link')) : enclosureUrl; | ||||
|
TonyRL marked this conversation as resolved.
|
||||
|
|
||||
| return { | ||||
| title, | ||||
| description: childText(itemElement, 'description') || undefined, | ||||
| link: childText(itemElement, 'link') || undefined, | ||||
|
TonyRL marked this conversation as resolved.
|
||||
| pubDate: parseOptionalDate(childText(itemElement, 'pubDate')), | ||||
|
guillevc marked this conversation as resolved.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since https://github.com/DIYgod/RSSHub/pull/22072/changes#r3343163538 is marked as resolved by you, could you explain how is it resolved?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inlined in the same commit |
||||
| guid: childText(itemElement, 'guid') || undefined, | ||||
|
TonyRL marked this conversation as resolved.
|
||||
| enclosure_url: resolvedEnclosureUrl, | ||||
| enclosure_type: enclosure.attr('type') || mediaTypeFromUrl(resolvedEnclosureUrl), | ||||
| enclosure_title: title, | ||||
| ...(length === undefined ? {} : { enclosure_length: length }), | ||||
| itunes_duration: childText(itemElement, itunesDurationSelector) || undefined, | ||||
|
guillevc marked this conversation as resolved.
Outdated
|
||||
| itunes_item_image: image, | ||||
| }; | ||||
| }) | ||||
| ) | ||||
| ).filter((current): current is DataItem => current !== undefined); | ||||
|
|
||||
| return { | ||||
| title: childText(channel, 'title'), | ||||
| description: childText(channel, 'description') || undefined, | ||||
| link: childText(channel, 'link') || rootUrl, | ||||
|
guillevc marked this conversation as resolved.
TonyRL marked this conversation as resolved.
|
||||
| item: items, | ||||
| image: childText(channel.children('image').first(), 'url') || childAttr(channel, itunesImageSelector, 'href'), | ||||
| itunes_image: childAttr(channel, itunesImageSelector, 'href') || childText(channel.children('image').first(), 'url') || undefined, | ||||
| language: normalizeLanguage(childText(channel, 'language')), | ||||
|
guillevc marked this conversation as resolved.
Outdated
|
||||
| feedLink: feedUrl, | ||||
| itunes_author: childText(channel, itunesAuthorSelector) || undefined, | ||||
|
guillevc marked this conversation as resolved.
Outdated
|
||||
| itunes_category: childAttr(channel, itunesCategorySelector, 'text'), | ||||
| itunes_explicit: childText(channel, itunesExplicitSelector) || undefined, | ||||
|
guillevc marked this conversation as resolved.
Outdated
|
||||
| }; | ||||
| } | ||||
|
|
||||
| function normalizePodcastId(id: string): string { | ||||
| const match = /^f?(\d+)$/i.exec(id); | ||||
| if (!match) { | ||||
| throw new InvalidParameterError(`Invalid iVoox podcast ID: ${id}`); | ||||
| } | ||||
|
|
||||
| return match[1]; | ||||
| } | ||||
|
|
||||
| function normalizeEpisodeId(value: string): string | undefined { | ||||
| const match = /(?:_rf_|\/)(\d+)(?:_\d+)?(?:\.html)?/i.exec(value) ?? /(\d+)/.exec(value); | ||||
| return match?.[1]; | ||||
| } | ||||
|
|
||||
| function resolveEpisodeAudioUrl(audioId: string, fallbackUrl: string, referer: string): Promise<string> { | ||||
| return cache.tryGet(`ivoox:audio-url:${audioId}`, async () => { | ||||
| try { | ||||
| const response = await ofetch(`https://vcore-web.ivoox.com/v1/public/audios/${audioId}/download-url`); | ||||
| const downloadUrl = response?.data?.downloadUrl; | ||||
| if (typeof downloadUrl === 'string' && downloadUrl) { | ||||
| const audioResponse = await ofetch.raw(new URL(downloadUrl, rootUrl).href, { | ||||
| headers: { | ||||
| Referer: referer, | ||||
| }, | ||||
| redirect: 'manual', | ||||
| method: 'GET', | ||||
| }); | ||||
|
|
||||
| if (audioResponse.status >= 300 && audioResponse.status < 400) { | ||||
| const location = audioResponse.headers.get('location'); | ||||
| if (location) { | ||||
| return new URL(location, rootUrl).href; | ||||
| } | ||||
| } | ||||
|
|
||||
| if (audioResponse.url) { | ||||
| return audioResponse.url; | ||||
| } | ||||
|
|
||||
| return new URL(downloadUrl, rootUrl).href; | ||||
| } | ||||
| } catch { | ||||
| // Fall back to the original feed enclosure when iVoox does not return a direct URL. | ||||
| } | ||||
|
|
||||
| return fallbackUrl; | ||||
| }); | ||||
| } | ||||
|
|
||||
| function childText(element, selector: string): string { | ||||
| return decodeHTML(element.children(selector).first().text()); | ||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why use decodeHTML with xmlMode RSSHub/lib/routes/ivoox/podcast.ts Line 56 in 673ba90
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||
| } | ||||
|
|
||||
| function childAttr(element, selector: string, attribute: string): string | undefined { | ||||
| return element.children(selector).first().attr(attribute); | ||||
| } | ||||
|
|
||||
| function parseOptionalDate(value: string): Date | undefined { | ||||
| return value ? parseDate(value) : undefined; | ||||
| } | ||||
|
|
||||
| function parseOptionalInteger(value: string | undefined): number | undefined { | ||||
| if (!value) { | ||||
| return; | ||||
| } | ||||
|
|
||||
| const number = Number.parseInt(value, 10); | ||||
| return Number.isNaN(number) ? undefined : number; | ||||
| } | ||||
|
|
||||
| function normalizeLanguage(value: string): Data['language'] | undefined { | ||||
| const language = value.toLowerCase(); | ||||
| return language ? ((language === 'es-es' ? 'es' : language) as Data['language']) : undefined; | ||||
| } | ||||
|
|
||||
| function mediaTypeFromUrl(url: string): string { | ||||
| const pathname = new URL(url).pathname.toLowerCase(); | ||||
| if (pathname.endsWith('.m4a')) { | ||||
| return 'audio/mp4'; | ||||
| } | ||||
|
|
||||
| if (pathname.endsWith('.ogg') || pathname.endsWith('.opus')) { | ||||
| return 'audio/ogg'; | ||||
| } | ||||
|
|
||||
| if (pathname.endsWith('.aac')) { | ||||
| return 'audio/aac'; | ||||
| } | ||||
|
|
||||
| return 'audio/mpeg'; | ||||
| } | ||||

Uh oh!
There was an error while loading. Please reload this page.