1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// import { fetch } from "https://esm.town/v/std/fetch?v=4";
import ky, { HTTPError } from "npm:ky@1.1.3";
const BASE_URL = "https://podnews.net";
const isString = (arg): arg is string => typeof arg === "string";
export async function getPodnewsId(podcastId: string) {
// https://podnews.net/podcast/subscribe-pages
const redirectURL = new URL(`/podcast/${podcastId}`, BASE_URL);
let resp;
try {
resp = await ky.get(redirectURL, { redirect: "manual" });
} catch (err) {
if (!(err instanceof HTTPError)) throw err;
const resp = err.response;
if (resp.status < 300 || resp.status >= 400) throw err;
const podcastURL = new URL(resp.headers.get("Location"), BASE_URL);
const [_, podnewsId] = podcastURL.pathname.split("/").filter(Boolean);
return podnewsId;
}
}
type PodcastInfo = { podcastId: string } | { podnewsId: string };
export async function getFeedUrl(podcastInfo: PodcastInfo) {
let podnewsId;
if ("podnewsId" in podcastInfo) {
podnewsId = podcastInfo.podnewsId;
} else {
podnewsId = await getPodnewsId(podcastInfo.podcastId);
}
const apiURL = new URL(`/api/podcast-search/lookup-${podnewsId}`, BASE_URL);
const data = await ky.get(apiURL).json();
if (!Array.isArray(data)) return;
const feedUrl = data.at(0)?.feedUrl;
if (isString(feedUrl)) return feedUrl;
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
December 6, 2023