-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsermon.ts
178 lines (164 loc) · 5.28 KB
/
sermon.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { Episode } from "./rss/episode";
type SanityRef = {
_ref: string;
_type: string;
};
type SanitySermon = {
_id: string;
_type: string;
durationInSeconds?: number;
event?: {
name: string;
};
passages?: string[];
preachedAt: string;
series?: {
name: string;
subtitle?: string;
imageUrl?: string;
};
speakers?: {
name: string;
jobTitle?: string;
}[];
title: string;
url: string;
youtubeVideoSermonStartTimestamp: string | null
youtubeVideoSermonEndTimestamp: string | null
};
export type Sermon = {
title: string;
passages: string[];
series: {
title: string;
subtitle: string | null;
} | null;
preachedAt: string;
duration: number | null;
link: string;
speakers: { name: string; jobTitle: string | null }[];
event: string | null;
};
export function parseSermonFromSanityResponse(
sanitySermon: SanitySermon
): Sermon {
var durationInSeconds: number | null = null
if (sanitySermon.durationInSeconds != null) {
durationInSeconds = sanitySermon.durationInSeconds ?? null
} else if (sanitySermon.youtubeVideoSermonEndTimestamp != null && sanitySermon.youtubeVideoSermonStartTimestamp != null) {
const startParts = sanitySermon.youtubeVideoSermonStartTimestamp.split(":").map(part => parseInt(part))
const endParts = sanitySermon.youtubeVideoSermonEndTimestamp.split(":").map(part => parseInt(part))
const start = (startParts[0] * 60 * 60) + (startParts[1] * 60) + startParts[2]
const end = (endParts[0] * 60 * 60) + (endParts[1] * 60) + endParts[2]
durationInSeconds = end - start
}
return {
title: sanitySermon.title,
series:
sanitySermon.series != null
? {
title: sanitySermon.series.name,
subtitle: sanitySermon.series.subtitle ?? null,
}
: null,
preachedAt: sanitySermon.preachedAt,
duration: durationInSeconds,
link: sanitySermon.url,
speakers:
sanitySermon.speakers?.map((s) => {
return {
name: s.name,
jobTitle: s.jobTitle ?? null,
};
}) ?? [],
event: sanitySermon.event?.name ?? null,
passages: sanitySermon.passages ?? [],
};
}
export function convertSermonToEpisode(sermon: Sermon): Episode {
const longSeriesTitle = ((): string | null => {
if (sermon.series == null) {
return null;
}
if (sermon.series.subtitle != null) {
return `${sermon.series.title}: ${sermon.series.subtitle}`;
}
return sermon.series.title;
})();
const passageSummary =
sermon.passages.length > 0 ? sermon.passages.join(", ") : null;
const speakerSummary =
sermon.speakers.length > 0
? sermon.speakers
.map((s) => {
if (s.jobTitle != null) {
return `${s.name} (${s.jobTitle})`;
}
return s.name;
})
.join(", ")
: null;
const description = joinNonNullish(
[
joinNonNullish([speakerSummary, sermon.event], " – "),
longSeriesTitle,
passageSummary, // already in episode title so doesn't need to be prominent
],
"\n"
);
const customElements = (() => {
const dict: Record<string, string> = {};
if (speakerSummary != null) {
dict["ccm:author"] = speakerSummary;
}
if (sermon.series != null) {
dict["ccm:seriesname"] = sermon.series.title;
if (sermon.series.subtitle != null) {
dict["ccm:seriessubtitle"] = sermon.series.subtitle;
}
}
if (passageSummary != null) {
dict["ccm:biblepassage"] = passageSummary;
}
if (sermon.event != null) {
dict["ccm:event"] = sermon.event;
}
return dict;
})();
const title = (() => {
if (passageSummary == null) {
return sermon.title;
}
if (
passageSummary.toLocaleLowerCase() ===
sermon.title.toLocaleLowerCase()
) {
// Occasionally they are identical. While it'd be better to fix
// the underlying data, this isn't always practical so this is
// a reasonable compromise.
return sermon.title;
}
return `${sermon.title} (${passageSummary})`;
})();
return {
title,
mediaUrl: sermon.link,
description,
author: speakerSummary ?? "Christ Church Mayfair",
durationInSeconds: sermon.duration ?? 1800, // default to 30 mins if we don't know
releaseDate: sermon.preachedAt,
keywords: [
"Sermon",
sermon.speakers.map((s) => s.name).join(", "),
sermon.event,
longSeriesTitle,
].filter((a): a is string => a != null && a !== ""),
customElements,
};
}
function joinNonNullish(
strings: (string | null | undefined)[],
separator: string
): string {
return strings.filter(Boolean).join(separator);
}