chore: Prepare for updated version of `kall`
Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
parent
6a93c0c084
commit
ae7821d31e
33
lib/nrk.ts
33
lib/nrk.ts
|
|
@ -1,4 +1,5 @@
|
||||||
import { get, OK } from "https://deno.land/x/kall@v0.1.0/mod.ts";
|
// FIXME: Remember to replace this with actual version
|
||||||
|
import { get, STATUS_CODE } from "../../kall/mod.ts";
|
||||||
import { components as searchComponents } from "./nrk-search.ts";
|
import { components as searchComponents } from "./nrk-search.ts";
|
||||||
import { components as catalogComponents } from "./nrk-catalog.ts";
|
import { components as catalogComponents } from "./nrk-catalog.ts";
|
||||||
import { external as playbackComponents } from "./nrk-playback.ts";
|
import { external as playbackComponents } from "./nrk-playback.ts";
|
||||||
|
|
@ -25,17 +26,19 @@ async function withDownloadLink(
|
||||||
type: catalogComponents["schemas"]["Type"],
|
type: catalogComponents["schemas"]["Type"],
|
||||||
): Promise<OriginalEpisode> {
|
): Promise<OriginalEpisode> {
|
||||||
// getting stream link
|
// getting stream link
|
||||||
let [playbackStatus, playbackResponse] = await get<Manifest>(
|
let { status: playbackStatus, body: playbackResponse } = await get<Manifest>(
|
||||||
`${nrkAPI}/playback/manifest/podcast/${episode.episodeId}`,
|
`${nrkAPI}/playback/manifest/podcast/${episode.episodeId}`,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (type === "series") {
|
if (type === "series") {
|
||||||
[playbackStatus, playbackResponse] = await get<Manifest>(
|
const { status, body } = await get<Manifest>(
|
||||||
`${nrkAPI}/playback/manifest/program/${episode.episodeId}`,
|
`${nrkAPI}/playback/manifest/program/${episode.episodeId}`,
|
||||||
);
|
);
|
||||||
|
playbackStatus = status;
|
||||||
|
playbackResponse = body;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (playbackStatus === OK && playbackResponse) {
|
if (playbackStatus === STATUS_CODE.OK && playbackResponse) {
|
||||||
return { ...episode, url: playbackResponse.playable.assets[0].url };
|
return { ...episode, url: playbackResponse.playable.assets[0].url };
|
||||||
} else {
|
} else {
|
||||||
throw `Error getting downloadLink for ${episode.episodeId}, serie: ${episode.originalTitle}. Status: ${playbackStatus}`;
|
throw `Error getting downloadLink for ${episode.episodeId}, serie: ${episode.originalTitle}. Status: ${playbackStatus}`;
|
||||||
|
|
@ -48,18 +51,18 @@ export const nrkRadio = {
|
||||||
if (query === "") {
|
if (query === "") {
|
||||||
throw "Empty search query.";
|
throw "Empty search query.";
|
||||||
}
|
}
|
||||||
const [status, response] = await get<
|
const { status, body } = await get<
|
||||||
searchComponents["schemas"]["searchresult"]
|
searchComponents["schemas"]["searchresult"]
|
||||||
>(`${nrkAPI}/radio/search/search?q=${query}`);
|
>(`${nrkAPI}/radio/search/search?q=${query}`);
|
||||||
if (status === OK && response) {
|
if (status === STATUS_CODE.OK && body) {
|
||||||
return response.results.series?.results;
|
return body.results.series?.results;
|
||||||
}
|
}
|
||||||
throw `Something went wrong with ${query} - got status ${status}`;
|
throw `Something went wrong with ${query} - got status ${status}`;
|
||||||
},
|
},
|
||||||
getSerieData: async (seriesId: string) => {
|
getSerieData: async (seriesId: string) => {
|
||||||
let [
|
let [
|
||||||
[episodeStatus, episodeResponse],
|
{ status: episodeStatus, body: episodeResponse },
|
||||||
[seriesStatus, serieResponse],
|
{ status: seriesStatus, body: serieResponse },
|
||||||
] = await Promise.all([
|
] = await Promise.all([
|
||||||
get<PodcastEpisodes>(
|
get<PodcastEpisodes>(
|
||||||
`${nrkAPI}/radio/catalog/podcast/${seriesId}/episodes`,
|
`${nrkAPI}/radio/catalog/podcast/${seriesId}/episodes`,
|
||||||
|
|
@ -69,10 +72,10 @@ export const nrkRadio = {
|
||||||
),
|
),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (episodeStatus !== OK || seriesStatus !== OK) {
|
if (episodeStatus !== STATUS_CODE.OK || seriesStatus !== STATUS_CODE.OK) {
|
||||||
[
|
[
|
||||||
[episodeStatus, episodeResponse],
|
{ status: episodeStatus, body: episodeResponse },
|
||||||
[seriesStatus, serieResponse],
|
{ status: seriesStatus, body: serieResponse },
|
||||||
] = await Promise.all([
|
] = await Promise.all([
|
||||||
get<catalogComponents["schemas"]["EpisodesHalResource"]>(
|
get<catalogComponents["schemas"]["EpisodesHalResource"]>(
|
||||||
`https://psapi.nrk.no/radio/catalog/series/${seriesId}/episodes`,
|
`https://psapi.nrk.no/radio/catalog/series/${seriesId}/episodes`,
|
||||||
|
|
@ -84,7 +87,7 @@ export const nrkRadio = {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
episodeStatus === OK && seriesStatus === OK &&
|
episodeStatus === STATUS_CODE.OK && seriesStatus === STATUS_CODE.OK &&
|
||||||
serieResponse?.series && episodeResponse?._embedded.episodes?.length
|
serieResponse?.series && episodeResponse?._embedded.episodes?.length
|
||||||
) {
|
) {
|
||||||
const episodes = await Promise.all(
|
const episodes = await Promise.all(
|
||||||
|
|
@ -99,8 +102,8 @@ export const nrkRadio = {
|
||||||
},
|
},
|
||||||
getEpisode: async (seriesId: string, episodeId: string): Promise<PodcastEpisode | null> => {
|
getEpisode: async (seriesId: string, episodeId: string): Promise<PodcastEpisode | null> => {
|
||||||
const url = `${nrkAPI}/radio/catalog/podcast/${seriesId}/episodes/${episodeId}`;
|
const url = `${nrkAPI}/radio/catalog/podcast/${seriesId}/episodes/${episodeId}`;
|
||||||
const [status, episode] = await get<PodcastEpisode>(url);
|
const { status, body: episode } = await get<PodcastEpisode>(url);
|
||||||
if (status === OK && episode) {
|
if (status === STATUS_CODE.OK && episode) {
|
||||||
return episode;
|
return episode;
|
||||||
}
|
}
|
||||||
console.error(`Error getting episode ${episodeId}. Status: ${status}. Series: ${seriesId}`);
|
console.error(`Error getting episode ${episodeId}. Status: ${status}. Series: ${seriesId}`);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue