refactor: Move responsibility of where fetch should happen
Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
parent
bac665a57a
commit
6c10e738ec
|
|
@ -37,9 +37,8 @@ async function search(query: string): Promise<SearchResultList | null> {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getSerieData(
|
export type SeriesData = { episodes: OriginalEpisode[] } & (RadioSeries["series"] | Podcast["series"]);
|
||||||
seriesId: string,
|
async function getSerieData(seriesId: string): Promise<SeriesData | null> {
|
||||||
): Promise<{ episodes: OriginalEpisode[] } & (RadioSeries["series"] | Podcast["series"]) | null> {
|
|
||||||
let [
|
let [
|
||||||
{ status: episodeStatus, body: episodeResponse },
|
{ status: episodeStatus, body: episodeResponse },
|
||||||
{ status: seriesStatus, body: serieResponse },
|
{ status: seriesStatus, body: serieResponse },
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ import { nrkRadio, OriginalEpisode } from "../../../lib/nrk.ts";
|
||||||
import { FreshContext } from "$fresh/server.ts";
|
import { FreshContext } from "$fresh/server.ts";
|
||||||
import { declaration, serialize, tag } from "https://raw.githubusercontent.com/olaven/serialize-xml/v0.4.0/mod.ts";
|
import { declaration, serialize, tag } from "https://raw.githubusercontent.com/olaven/serialize-xml/v0.4.0/mod.ts";
|
||||||
import { getHostName } from "../../../utils.ts";
|
import { getHostName } from "../../../utils.ts";
|
||||||
|
import { SeriesData } from "../../../lib/nrk.ts";
|
||||||
|
|
||||||
function toItemTag(seriesId: string, episode: OriginalEpisode) {
|
function toItemTag(seriesId: string, episode: OriginalEpisode) {
|
||||||
const description = episode.titles.subtitle || "";
|
const description = episode.titles.subtitle || "";
|
||||||
|
|
@ -28,10 +29,9 @@ function toItemTag(seriesId: string, episode: OriginalEpisode) {
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function buildFeed(seriesId: string) {
|
function buildFeed(series: SeriesData) {
|
||||||
const serie = await nrkRadio.getSerieData(seriesId);
|
const imageUrl = series.squareImage?.at(-1)?.url ?? "";
|
||||||
const imageUrl = serie.squareImage?.at(-1)?.url ?? "";
|
const linkValue = `https://radio.nrk.no/podkast/${series.id}`;
|
||||||
const linkValue = `https://radio.nrk.no/podkast/${serie.id}`;
|
|
||||||
|
|
||||||
// Quickly adapted from https://raw.githubusercontent.com/olaven/paperpod/1cde9abd3174b26e126aa74fc5a3b63fd078c0fd/packages/converter/src/rss.ts
|
// Quickly adapted from https://raw.githubusercontent.com/olaven/paperpod/1cde9abd3174b26e126aa74fc5a3b63fd078c0fd/packages/converter/src/rss.ts
|
||||||
return serialize(
|
return serialize(
|
||||||
|
|
@ -43,7 +43,7 @@ async function buildFeed(seriesId: string) {
|
||||||
"rss",
|
"rss",
|
||||||
[
|
[
|
||||||
tag("channel", [
|
tag("channel", [
|
||||||
tag("title", serie.titles.title),
|
tag("title", series.titles.title),
|
||||||
tag("link", linkValue),
|
tag("link", linkValue),
|
||||||
tag("itunes:author", "NRK"),
|
tag("itunes:author", "NRK"),
|
||||||
/* serie.category.id does not overlap with Apple's supported categories..
|
/* serie.category.id does not overlap with Apple's supported categories..
|
||||||
|
|
@ -58,7 +58,7 @@ async function buildFeed(seriesId: string) {
|
||||||
]),
|
]),
|
||||||
tag(
|
tag(
|
||||||
"description",
|
"description",
|
||||||
serie.titles.subtitle || "",
|
series.titles.subtitle || "",
|
||||||
),
|
),
|
||||||
tag("ttl", "60"), //60 minutes
|
tag("ttl", "60"), //60 minutes
|
||||||
...(imageUrl
|
...(imageUrl
|
||||||
|
|
@ -68,12 +68,12 @@ async function buildFeed(seriesId: string) {
|
||||||
]),
|
]),
|
||||||
tag("image", [
|
tag("image", [
|
||||||
tag("url", imageUrl),
|
tag("url", imageUrl),
|
||||||
tag("title", serie.titles.title),
|
tag("title", series.titles.title),
|
||||||
tag("link", linkValue),
|
tag("link", linkValue),
|
||||||
]),
|
]),
|
||||||
]
|
]
|
||||||
: []),
|
: []),
|
||||||
...serie.episodes.map((episode) => toItemTag(seriesId, episode)),
|
...series.episodes.map((episode) => toItemTag(series.id, episode)),
|
||||||
]),
|
]),
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
|
|
@ -88,8 +88,12 @@ async function buildFeed(seriesId: string) {
|
||||||
|
|
||||||
export const handler = async (_req: Request, ctx: FreshContext): Promise<Response> => {
|
export const handler = async (_req: Request, ctx: FreshContext): Promise<Response> => {
|
||||||
const seriesId = ctx.params.seriesId;
|
const seriesId = ctx.params.seriesId;
|
||||||
|
const series = await nrkRadio.getSerieData(seriesId);
|
||||||
|
if (!series) {
|
||||||
|
return new Response(`Couldn't find series with seriesId: ${seriesId}`, { status: 404 });
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const feedContent = await buildFeed(seriesId);
|
const feedContent = buildFeed(series);
|
||||||
|
|
||||||
return new Response(feedContent, {
|
return new Response(feedContent, {
|
||||||
headers: {
|
headers: {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue