diff --git a/lib/nrk.ts b/lib/nrk.ts index a547aac..e5e312e 100644 --- a/lib/nrk.ts +++ b/lib/nrk.ts @@ -4,9 +4,13 @@ import { components as catalogComponents } from "./nrk-catalog.ts"; import { z } from "zod"; type ArrayElement = A extends readonly (infer T)[] ? T : never; +type PodcastEpisodes = catalogComponents["schemas"]["EpisodesHalResource"]; +type Podcast = catalogComponents["schemas"]["SeriesHalResource"]; +type PodcastEpisodesSingle = catalogComponents["schemas"]["EpisodeHalResource"]; + export type Serie = catalogComponents["schemas"]["SeriesViewModel"]; -type _OriginalEpisode = catalogComponents["schemas"]["EpisodeHalResource"]; -export type OriginalEpisode = _OriginalEpisode & { url: string }; +export type OriginalEpisode = PodcastEpisodesSingle & { url: string }; +export type PodcastEpisode = catalogComponents["schemas"]["PodcastEpisodeHalResource"]; export type SearchResultList = searchComponents["schemas"]["seriesResult"]["results"]; export type SearchResult = ArrayElement & { description?: string; @@ -32,7 +36,7 @@ type Manifest = z.infer; const nrkAPI = `https://psapi.nrk.no`; async function withDownloadLink( - episode: _OriginalEpisode, + episode: PodcastEpisodesSingle, ): Promise { // getting stream link const [playbackStatus, playbackResponseRaw] = await get( @@ -63,10 +67,10 @@ export const nrkRadio = { [episodeStatus, episodeResponse], [seriesStatus, serieResponse], ] = await Promise.all([ - get( + get( `${nrkAPI}/radio/catalog/podcast/${seriesId}/episodes`, ), - get( + get( `${nrkAPI}/radio/catalog/podcast/${seriesId}`, ), ]); @@ -85,9 +89,9 @@ export const nrkRadio = { } throw `Error getting episodes for ${seriesId}: EpisodeStatus: ${episodeStatus}. SerieStatus: ${seriesStatus}`; }, - getEpisode: async (seriesId: string, episodeId: string): Promise => { + getEpisode: async (seriesId: string, episodeId: string): Promise => { const url = `${nrkAPI}/radio/catalog/podcast/${seriesId}/episodes/${episodeId}`; - const [status, episode] = await get(url); + const [status, episode] = await get(url); if (status === OK && episode) { return episode; } diff --git a/routes/api/feeds/[seriesId]/[episodeId]/chapters.ts b/routes/api/feeds/[seriesId]/[episodeId]/chapters.ts index 67c273d..19eee90 100644 --- a/routes/api/feeds/[seriesId]/[episodeId]/chapters.ts +++ b/routes/api/feeds/[seriesId]/[episodeId]/chapters.ts @@ -1,13 +1,16 @@ import { FreshContext } from "$fresh/server.ts"; import { parse, toSeconds } from "https://esm.sh/iso8601-duration@2.1.1"; -import { nrkRadio, OriginalEpisode } from "../../../../../lib/nrk.ts"; +import { nrkRadio, PodcastEpisode } from "../../../../../lib/nrk.ts"; type Chapter = { title: string | undefined; startTime: number | undefined; }; -function toChapters(episode: OriginalEpisode): Chapter[] { +function toChapters(episode: PodcastEpisode): Chapter[] | null { + if (!episode.indexPoints) { + return null; + } return episode.indexPoints.map((indexPoint) => ({ title: indexPoint.title, startTime: indexPoint.startPoint ? toSeconds(parse(indexPoint.startPoint)) : undefined,