fix: Generate feeds for series in addition to podcast (#19)

This commit is contained in:
Tim Hårek Andreassen 2024-04-02 16:27:00 +02:00 committed by GitHub
parent f4d27c06f2
commit 8381cddca6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 41 additions and 10 deletions

View File

@ -22,12 +22,19 @@ const nrkAPI = `https://psapi.nrk.no`;
async function withDownloadLink(
episode: PodcastEpisodesSingle,
type: catalogComponents["schemas"]["Type"],
): Promise<OriginalEpisode> {
// getting stream link
const [playbackStatus, playbackResponse] = await get<Manifest>(
let [playbackStatus, playbackResponse] = await get<Manifest>(
`${nrkAPI}/playback/manifest/podcast/${episode.episodeId}`,
);
if (type === "series") {
[playbackStatus, playbackResponse] = await get<Manifest>(
`${nrkAPI}/playback/manifest/program/${episode.episodeId}`,
);
}
if (playbackStatus === OK && playbackResponse) {
return { ...episode, url: playbackResponse.playable.assets[0].url };
} else {
@ -46,7 +53,7 @@ export const nrkRadio = {
throw `Something went wrong with ${query} - got status ${status}`;
},
getSerieData: async (seriesId: string) => {
const [
let [
[episodeStatus, episodeResponse],
[seriesStatus, serieResponse],
] = await Promise.all([
@ -58,12 +65,26 @@ export const nrkRadio = {
),
]);
if (episodeStatus !== OK || seriesStatus !== OK) {
[
[episodeStatus, episodeResponse],
[seriesStatus, serieResponse],
] = await Promise.all([
get<catalogComponents["schemas"]["EpisodesHalResource"]>(
`https://psapi.nrk.no/radio/catalog/series/${seriesId}/episodes`,
),
get<catalogComponents["schemas"]["SeriesHalResource"]>(
`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,

View File

@ -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
@ -88,6 +88,7 @@ async function buildFeed(seriesId: string) {
export const handler = async (_req: Request, ctx: FreshContext): Promise<Response> => {
const seriesId = ctx.params.seriesId;
try {
const feedContent = await buildFeed(seriesId);
return new Response(feedContent, {
@ -95,4 +96,13 @@ export const handler = async (_req: Request, ctx: FreshContext): Promise<Respons
"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",
},
});
}
};