-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpodcast.ts
218 lines (205 loc) · 4.92 KB
/
podcast.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { all, call, createContext, Operation, useAbortSignal } from "effection";
const PodcastContext = createContext<Episode[]>(
"podcast",
);
export interface SimplecastClient {
getEpisodes(name: string): Operation<Episode[]>;
}
export interface Podcast {
readonly title: string;
readonly id: string;
}
export interface Episode {
linkname: string;
season: {
href: string;
number: number;
next_episode_number: number;
};
audio_file_name: string;
is_explicit: boolean;
waveform_pack: string;
audio_file_url: string;
sponsors: {
href: string;
};
number: number;
authors: {
href: string;
collection: Array<
{
href: string;
name: string;
id: string;
}
>;
};
analytics: {
href: string;
};
long_description: string;
podcast: {
id: string;
href: string;
title: string;
status: "published";
image_url: string;
episodes: { count: number };
created_at: string;
account_id: string;
account: {
id: string;
href: string;
owner: {
name: string;
id: string;
email: string;
};
};
};
description: string;
audio_status: "transcoded";
legacy_id: number;
transcription: string | null;
audio_file_size: number;
waveform_json: string;
slug: string;
title: string;
campaign_preview: {
href: string;
};
is_hidden: false;
is_published: true;
warnings: Record<string | number | symbol, string>;
audio_file_path: string;
dashboard_link: string;
audio_content_type: string;
episode_feeds: [
{
id: string;
feed_id: string;
},
];
days_since_release: number;
published_at: string;
href: string;
audio: {
href: string;
};
image_url: string;
id: string;
enclosure_url: string;
ad_free_audio_file_url: string;
duration: number;
keywords: {
href: string;
collection: Array<
{
href: string;
value: string;
id: string;
hide: false;
}
>;
};
token: string;
guid: string;
created_at: string;
image_path: string;
episode_url: string;
audio_file_path_tc: string;
updated_at: string;
audio_file: {
url: string;
size: number;
path_tc: string;
path: string;
name: string;
href: string;
headliner_url: string;
ad_free_url: string;
};
}
export function* initSimpleCast(apiKey?: string) {
if (!apiKey) {
console.log(`simplecast: disabled`);
yield* PodcastContext.set([]);
} else {
let client = new HTTPClient({ apiKey });
let episodes = yield* client.getEpisodes("The Frontside Podcast");
console.dir(episodes[0].linkname);
console.log(`simplecast: loaded ${episodes.length} episodes`);
yield* PodcastContext.set(episodes);
}
}
export function* usePodcastEpisodes(): Operation<Episode[]> {
return yield* PodcastContext;
}
interface HTTPCLientOptions {
apiKey: string;
}
class HTTPClient implements SimplecastClient {
constructor(public readonly options: HTTPCLientOptions) {}
*getEpisodes(title: string): Operation<Episode[]> {
let podcasts = yield* this.getPodcasts();
let podcast = podcasts.find((p) => p.title === title);
if (!podcast) {
throw new Error(
`unable to find podcast: ${title} in [${
podcasts.map((p) => p.title).join(", ")
}]`,
);
}
let response = yield* this.request(
`/podcasts/${podcast.id}/episodes`,
{ limit: 1000, offset: 0 },
);
let json = yield* call(() => response.json());
return (yield* all(
json.collection.map((episodeMetadata: { id: string }) => {
let request = this.request.bind(this);
return call(function* () {
let response = yield* request(`/episodes/${episodeMetadata.id}`);
let episode = yield* call(() => response.json());
return {
...episode,
linkname: episode.title.toLowerCase().replaceAll(/\s/g, "-"),
};
});
}),
)) as Episode[];
}
*getPodcasts(): Operation<Podcast[]> {
let response = yield* this.request("/podcasts");
let json = yield* call(() => response.json());
return json.collection;
}
private *request(
pathname: string,
params: Record<string, string | number> = {},
): Operation<Response> {
let url = new URL(`https://api.simplecast.com`);
url.pathname = pathname;
let searchParams: Record<string, string> = {};
for (let key in params) {
searchParams[key] = String(params[key]);
}
url.search = new URLSearchParams(searchParams).toString();
let signal = yield* useAbortSignal();
let response = yield* call(() =>
fetch(url, {
signal,
headers: {
"Authorization": `Bearer ${this.options.apiKey}`,
},
})
);
if (!response.ok) {
throw new Error(`${response.status}: ${response.statusText}`, {
cause: pathname,
});
} else {
return response;
}
}
}