From ab5c1b9e567116fc259f845fd360f8cd122e435c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20H=C3=A5rek=20Andreassen?= Date: Sat, 30 Mar 2024 15:17:35 +0100 Subject: [PATCH 1/3] fix: Add error message when unable to generate feed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tim Hårek Andreassen --- lib/nrk.ts | 1 + routes/api/feeds/[seriesId].ts | 22 ++++++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/nrk.ts b/lib/nrk.ts index c0e121c..9b284ec 100644 --- a/lib/nrk.ts +++ b/lib/nrk.ts @@ -47,6 +47,7 @@ export const nrkRadio = { searchComponents["schemas"]["searchresult"] >(`https://psapi.nrk.no/radio/search/search?q=${query}`); if (status === OK && response) { + console.log("res", response.results); return response.results.series?.results; } else { throw `Something went wrong with ${query} - got status ${status}`; diff --git a/routes/api/feeds/[seriesId].ts b/routes/api/feeds/[seriesId].ts index 2057c20..52732a4 100644 --- a/routes/api/feeds/[seriesId].ts +++ b/routes/api/feeds/[seriesId].ts @@ -88,11 +88,21 @@ async function buildFeed(seriesId: string) { export const handler = async (_req: Request, ctx: FreshContext): Promise => { const seriesId = ctx.params.seriesId; - const feedContent = await buildFeed(seriesId); + try { + const feedContent = await buildFeed(seriesId); - return new Response(feedContent, { - headers: { - "Content-Type": "application/xml", - }, - }); + return new Response(feedContent, { + headers: { + "Content-Type": "application/xml", + }, + }); + } catch (error) { + console.error(error); + return new Response(JSON.stringify({ message: "Could not generate feed" }), { + status: 500, + headers: { + "Content-Type": "application/json", + }, + }); + } }; -- 2.40.1 From 82c4f8d22f72931360a45491463c92400a3c77b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20H=C3=A5rek=20Andreassen?= Date: Sat, 30 Mar 2024 15:31:04 +0100 Subject: [PATCH 2/3] fix: Get data for series as well as podcasts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tim Hårek Andreassen --- lib/nrk.ts | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/nrk.ts b/lib/nrk.ts index 9b284ec..182d87f 100644 --- a/lib/nrk.ts +++ b/lib/nrk.ts @@ -28,12 +28,19 @@ interface Manifest { async function withDownloadLink( episode: _OriginalEpisode, + type: catalogComponents["schemas"]["Type"], ): Promise { // getting stream link - const [playbackStatus, playbackResponse] = await get( + let [playbackStatus, playbackResponse] = await get( `https://psapi.nrk.no/playback/manifest/podcast/${episode.episodeId}`, ); + if (type === "series") { + [playbackStatus, playbackResponse] = await get( + `https://psapi.nrk.no/playback/manifest/program/${episode.episodeId}`, + ); + } + if (playbackStatus === OK && playbackResponse) { return { ...episode, url: playbackResponse.playable.assets[0].url }; } else { @@ -47,14 +54,13 @@ export const nrkRadio = { searchComponents["schemas"]["searchresult"] >(`https://psapi.nrk.no/radio/search/search?q=${query}`); if (status === OK && response) { - console.log("res", response.results); return response.results.series?.results; } else { throw `Something went wrong with ${query} - got status ${status}`; } }, getSerieData: async (seriesId: string) => { - const [ + let [ [episodeStatus, episodeResponse], [seriesStatus, serieResponse], ] = await Promise.all([ @@ -66,20 +72,33 @@ export const nrkRadio = { ), ]); + if (episodeStatus !== OK || seriesStatus !== OK) { + [ + [episodeStatus, episodeResponse], + [seriesStatus, serieResponse], + ] = await Promise.all([ + get( + `https://psapi.nrk.no/radio/catalog/series/${seriesId}/episodes`, + ), + get( + `https://psapi.nrk.no/radio/catalog/series/${seriesId}`, + ), + ]); + } + if ( episodeStatus === OK && seriesStatus === OK && serieResponse?.series && episodeResponse?._embedded.episodes?.length ) { const episodes = await Promise.all( - episodeResponse._embedded.episodes.map((episode) => withDownloadLink(episode)), + episodeResponse._embedded.episodes.map((episode) => withDownloadLink(episode, serieResponse.type)), ); return { ...serieResponse.series, episodes, }; - } else { - throw `Error getting episodes for ${seriesId}: EpisodeStatus: ${episodeStatus}. SerieStatus: ${seriesStatus}`; } + throw `Error getting episodes for ${seriesId}: EpisodeStatus: ${episodeStatus}. SerieStatus: ${seriesStatus}`; }, getEpisode: async (seriesId: string, episodeId: string): Promise => { const url = `https://psapi.nrk.no/radio/catalog/podcast/${seriesId}/episodes/${episodeId}`; -- 2.40.1 From d16829f45e550d9a1c7d43044c6d0c5be71a8f02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20H=C3=A5rek=20Andreassen?= Date: Sat, 30 Mar 2024 15:34:20 +0100 Subject: [PATCH 3/3] fix: Check if empty image MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Apparently this is allowed, although the OpenAPI specification disallows it. Signed-off-by: Tim Hårek Andreassen --- routes/api/feeds/[seriesId].ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/api/feeds/[seriesId].ts b/routes/api/feeds/[seriesId].ts index 52732a4..8729249 100644 --- a/routes/api/feeds/[seriesId].ts +++ b/routes/api/feeds/[seriesId].ts @@ -30,7 +30,7 @@ function toItemTag(seriesId: string, episode: OriginalEpisode) { async function buildFeed(seriesId: string) { const serie = await nrkRadio.getSerieData(seriesId); - const imageUrl = serie.squareImage.at(-1)?.url; + const imageUrl = serie.squareImage?.at(-1)?.url ?? ""; const linkValue = `https://radio.nrk.no/podkast/${serie.id}`; // Quickly adapted from https://raw.githubusercontent.com/olaven/paperpod/1cde9abd3174b26e126aa74fc5a3b63fd078c0fd/packages/converter/src/rss.ts -- 2.40.1