-
Notifications
You must be signed in to change notification settings - Fork 18
/
podcast.config.ts
101 lines (89 loc) · 2.42 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
101
import { cache } from 'react'
import { parse } from 'rss-to-json'
import type { SupportedDirectory } from './app/[locale]/PodcastDirectoryLink'
type PodcastConfig = {
directories: SupportedDirectory[]
hosts: Host[]
}
/**
* TODO: Add your podcast config here
*/
export const podcastConfig: PodcastConfig = {
/**
* Step 1. Add your podcast directories here
* We support links from:
* Apple Podcasts, Google Podcasts, Spotify, Stitcher, Overcast,
* Pocket Casts Castro, 小宇宙, 哔哩哔哩, YouTube
*/
directories: [
'https://podcasts.apple.com/us/podcast/lex-fridman-podcast/id1434243584',
'https://open.spotify.com/show/2MAi0BvDc6GTFvKFPXnkCL',
'https://www.youtube.com/lexfridman',
],
/**
* Step 2. Add your podcast hosts here
*/
hosts: [
{
name: 'Lex Fridman',
link: 'https://lexfridman.com/',
},
],
}
/**
* Get podcast via RSS feed.
*/
export const getPodcast = cache(async () => {
const feed = await parse(process.env.NEXT_PUBLIC_PODCAST_RSS || '')
const podcast: Podcast = {
title: feed.title,
description: feed.description,
link: feed.link,
coverArt: feed.image,
}
return podcast
})
/**
* 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 RSS feed.
*/
export const getPodcastEpisodes = cache(async () => {
const feed = await parse(process.env.NEXT_PUBLIC_PODCAST_RSS || '')
const episodes: Episode[] = feed.items.map((item) => ({
id: encodeEpisodeId(item.id ?? item.link),
title: item.title,
description: item.description,
link: item.link,
published: item.published,
content: item.content,
duration: item.itunes_duration,
enclosure: item.enclosures[0],
coverArt: item.itunes_image?.href,
}))
return episodes
})
/**
* Get podcast episode by id.
*/
export const getPodcastEpisode = cache(async (id: string) => {
const episodes = await getPodcastEpisodes()
const decodedId = decodeURIComponent(id)
return episodes.find(
(episode) => episode.id === decodedId || episode.link.endsWith(decodedId)
)
})