-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathrss.ts
More file actions
132 lines (118 loc) · 3.19 KB
/
rss.ts
File metadata and controls
132 lines (118 loc) · 3.19 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { htmlToText } from 'html-to-text';
import parseFeed from 'rss-to-json';
import { array, number, object, optional, parse, string } from 'valibot';
import { optimizeImage } from './optimize-episode-image';
import {
escapeHtml,
looksLikeHtml,
transformPlainTextToHtml
} from './rss-transform';
import { dasherize } from '../utils/dasherize';
import { truncate } from '../utils/truncate';
import starpodConfig from '../../starpod.config';
export interface Show {
title: string;
description: string;
image: string;
link: string;
}
export interface Episode {
id: string;
title: string;
published: number;
description: string;
duration: number;
content: string;
episodeImage?: string;
episodeNumber?: string;
episodeSlug: string;
episodeThumbnail?: string;
audio: {
src: string;
type: string;
};
}
let showInfoCache: Show | null = null;
export async function getShowInfo() {
if (showInfoCache) {
return showInfoCache;
}
// @ts-expect-error
const showInfo = (await parseFeed.parse(starpodConfig.rssFeed)) as Show;
showInfo.image = (await optimizeImage(showInfo.image, {
height: 640,
width: 640
})) as string;
showInfoCache = showInfo;
return showInfo;
}
let episodesCache: Array<Episode> | null = null;
export async function getAllEpisodes() {
if (episodesCache) {
return episodesCache;
}
let FeedSchema = object({
items: array(
object({
id: string(),
title: string(),
published: number(),
description: string(),
itunes_duration: number(),
itunes_episode: optional(number()),
itunes_episodeType: string(),
itunes_image: optional(object({ href: optional(string()) })),
enclosures: array(
object({
url: string(),
type: string()
})
)
})
)
});
// @ts-expect-error
let feed = (await parseFeed.parse(starpodConfig.rssFeed)) as Show;
let items = parse(FeedSchema, feed).items;
let episodes: Array<Episode> = await Promise.all(
items
.filter((item) => item.itunes_episodeType !== 'trailer')
.map(
async ({
description,
id,
title,
enclosures,
published,
itunes_duration,
itunes_episode,
itunes_episodeType,
itunes_image
}) => {
const episodeNumber =
itunes_episodeType === 'bonus' ? 'Bonus' : `${itunes_episode}`;
const episodeSlug = dasherize(title);
return {
id,
title: `${title}`,
content: looksLikeHtml(description)
? description
: transformPlainTextToHtml(description),
description: truncate(htmlToText(description), 260),
duration: itunes_duration,
episodeImage: itunes_image?.href,
episodeNumber,
episodeSlug,
episodeThumbnail: await optimizeImage(itunes_image?.href),
published,
audio: enclosures.map((enclosure) => ({
src: enclosure.url,
type: enclosure.type
}))[0]
};
}
)
);
episodesCache = episodes;
return episodes;
}