generated from CaliCastle/cali-fm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
podcast.config.ts
100 lines (90 loc) · 2.54 KB
/
podcast.config.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import PodcastIndexClient from 'podcast-index-client'
import getPodcastPlatformLinks, { Platform } from 'podcast-platform-links'
import { cache } from 'react'
const REMOVED_PLATFORM: Platform[] = ['podStation', 'Podvine']
const client = new PodcastIndexClient({
key: process.env.PODCAST_INDEX_API_KEY,
secret: process.env.PODCAST_INDEX_API_SECRET,
})
export const getPodcastConfig = cache(async (itunesId: number) => {
const { feed } = await client.podcastByItunesId(itunesId)
const platforms = getPodcastPlatformLinks(itunesId, feed.url)
return {
platforms: [
...platforms
.map((platform) => ({
name: platform.platform,
link: platform.link,
}))
.filter((platform) => !REMOVED_PLATFORM.includes(platform.name)),
{
name: 'RSS',
link: feed.url,
},
],
hosts: [
{
name: feed.author,
link: feed.link,
},
],
info: {
title: feed.title,
description: feed.description,
link: feed.link,
coverArt: feed.image,
rssUrl: feed.url,
itunesId: feed.itunesId,
},
} as PodcastConfig
})
/**
* Encode episode id.
* (Certain episode id contains special characters that are not allowed in URL)
*/
function encodeEpisodeId(raw: string): string {
if (!raw.startsWith('http')) {
return raw
}
const url = new URL(raw)
const path = url.pathname.split('/')
const lastPathname = path[path.length - 1]
if (lastPathname === '' && url.search) {
return url.search.slice(1)
}
return lastPathname
}
/**
* Get podcast episodes via iTunes ID.
*/
export const getPodcastEpisodes = cache(async (itunesId: number) => {
const { items } = await client.episodesByItunesId(itunesId, { max: 1000 })
const episodes: Episode[] = items.map((item) => ({
id: encodeEpisodeId(item.id ? String(item.id) : item.link),
title: item.title,
description: item.description,
link: item.link,
published: item.datePublished * 1000,
content: item.description,
duration: item.duration,
enclosure: {
url: item.enclosureUrl,
type: item.enclosureType,
length: item.enclosureLength,
},
coverArt: item.image,
}))
return episodes
})
/**
* Get podcast episode by id.
*/
export const getPodcastEpisode = cache(
async (episodeId: string, itunesId: number) => {
const episodes = await getPodcastEpisodes(itunesId)
const decodedId = decodeURIComponent(episodeId)
return episodes.find(
(episode) => episode.id === decodedId || episode.link.endsWith(decodedId)
)
}
)