generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgetUniversalPodcastLink.ts
57 lines (47 loc) · 1.42 KB
/
getUniversalPodcastLink.ts
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { requestUrl, Notice } from "obsidian";
import { IAPI } from "./API/IAPI";
import { queryiTunesPodcasts } from "./iTunesAPIConsumer";
export default async function getUniversalPodcastLink(api: IAPI) {
const { title, itunesTitle, podcastName, feedUrl } = api.podcast;
try {
const iTunesResponse = await queryiTunesPodcasts(
api.podcast.podcastName
);
const podcast = iTunesResponse.find(
(pod) => pod.title === podcastName && pod.url === feedUrl
);
if (!podcast || !podcast.collectionId) {
throw new Error("Failed to get podcast from iTunes.");
}
const podLinkUrl = `https://pod.link/${podcast.collectionId}.json?limit=1000`;
const res = await requestUrl({
url: podLinkUrl,
});
if (res.status !== 200) {
throw new Error(
`Failed to get response from pod.link: ${podLinkUrl}`
);
}
const targetTitle = itunesTitle ?? title;
const ep = res.json.episodes.find(
(episode: {
episodeId: string;
title: string;
[key: string]: string;
}) => episode.title === targetTitle
);
if (!ep) {
throw new Error(
`Failed to find episode "${targetTitle}" on pod.link. URL: ${podLinkUrl}`
);
}
window.navigator.clipboard.writeText(
`https://pod.link/${podcast.collectionId}/episode/${ep.episodeId}`
);
new Notice("Universal episode link copied to clipboard.");
} catch (error) {
new Notice("Could not get podcast link.");
console.error(error);
return;
}
}