-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathworker-script.js
342 lines (311 loc) · 11.3 KB
/
worker-script.js
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
const baseUrl = "https://radio-france-rss.aerion.workers.dev";
const routePrefixRss = "/rss/";
const routeSearch = "/search/";
const getRadioFranceUrl = async (path) => {
const url = `https://api.radiofrance.fr/v1/${path}`;
const response = await fetch(url, {
method: "GET",
headers: {
Accept: "application/x.radiofrance.mobileapi+json",
"User-Agent": "AppRF",
"x-token": "9ab343ce-cae2-4bdb-90ca-526a3dede870",
},
});
return await response.json();
};
const getShowDiffusions = async (showId, page) => {
const diffusions = [];
const manifestations = [];
let showDetails = undefined;
const shouldFetchAllDiffusions = page === -1;
if (shouldFetchAllDiffusions) {
page = 0;
}
let json;
do {
json = await getRadioFranceUrl(
`shows/${showId}/diffusions?filter[manifestations][exists]=true&include=show&include=manifestations&include=series&page[offset]=${page}`
);
if (showDetails === undefined) {
showDetails = json.included.shows[showId];
if (showDetails === undefined) {
// For some reason, for some podcasts, the show is not included in the manifestations
// It's very rare, but it's the case for the show 1aaba3dd-be85-4bbd-b046-c1343affc505
// Mitigate it by calling the show details endpoint directly
const showDetailsJson = await getRadioFranceUrl(`shows/${showId}`);
showDetails = showDetailsJson.data.shows;
}
}
diffusions.push(...json.data.map((item) => item.diffusions));
for (const k in json.included.manifestations) {
manifestations[k] = json.included.manifestations[k];
}
page += 1;
} while (shouldFetchAllDiffusions && json.links.next !== undefined);
return {
diffusions,
showDetails,
manifestations,
nextPageIdx: json.links.next !== undefined ? page : undefined,
};
};
const getImgUrl = (visuals, fallbackImgId) => {
let chosenId;
if (!visuals || visuals.length === 0) {
chosenId = fallbackImgId;
} else {
const visualsMap = visuals.reduce((res, item) => {
res[item.name] = item.visual_uuid;
return res;
}, {});
chosenId =
visualsMap["square_banner"] ??
visualsMap["square_visual"] ??
visuals[0].visual_uuid;
}
if (!chosenId) {
return null;
}
return `https://api.radiofrance.fr/v1/services/embed/image/${chosenId}?preset=568x568`;
};
const buildFeed = (
{ diffusions, showDetails, manifestations },
nextPageUrl
) => {
const buildElement = (name, innerText) => {
return !!innerText ? `<${name}>${innerText}</${name}>` : "";
};
const buildImgElement = (url) => {
return !!url ? `<itunes:image href="${imgUrl}"/>` : "";
};
const escapeXml = (unsafe) => {
if (typeof unsafe === "undefined") return unsafe;
// From https://stackoverflow.com/a/27979933
return unsafe.replace(/[<>&'"]/g, function (c) {
switch (c) {
case "<":
return "<";
case ">":
return ">";
case "&":
return "&";
case "'":
return "'";
case '"':
return """;
}
});
};
const buildItem = (diffusion) => {
const manifestation =
manifestations[
diffusion.relationships.manifestations.find(
(manifId) =>
manifestations[manifId]?.principal &&
!["youtube", "dailymotion"].includes(
manifestations[manifId]?.mediaType
)
)
];
if (typeof manifestation === "undefined") {
console.log(
`Item ${diffusion.id} visible at ${diffusion.path} has no mp3 version, skipping`
);
return "";
}
let guid = diffusion.id;
if (new Date(diffusion.createdTime * 1000) <= new Date("Sep 12 2022")) {
// backward compatibility: keep old id generation.
guid = manifestation.id;
}
const description = diffusion.standfirst ?? diffusion.bodyMarkdown ?? "";
const imgUrl = getImgUrl(diffusion.visuals, diffusion.mainImage);
return ` <item>
<title>${escapeXml(diffusion.title)}</title>
<guid>${guid}</guid>
${buildElement("link", diffusion.path)}
<description>${escapeXml(description)}</description>
<enclosure url="${manifestation.url}" type="audio/mpeg" />
<pubDate>${new Date(
diffusion.createdTime * 1000
).toUTCString()}</pubDate>
<itunes:duration>${new Date(manifestation.duration * 1000)
.toISOString()
.substring(11, 19)}</itunes:duration>
${buildElement("itunes:image", diffusion.path)}
${buildImgElement(imgUrl)}
</item>`;
};
const nextPageXmlTag = nextPageUrl
? `
<atom:link href="${nextPageUrl}" rel="next" />`
: "";
const imgUrl = getImgUrl(showDetails.visuals, showDetails.mainImage);
return `<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:pa="http://podcastaddict.com" xmlns:podcastRF="http://radiofrance.fr/Lancelot/Podcast#" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0" version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>${escapeXml(showDetails.title)}</title>
<link>${showDetails.path}</link>
${nextPageXmlTag}
<description>${escapeXml(showDetails.standfirst)}</description>
${buildImgElement(imgUrl)}
${diffusions.map(buildItem).join("\n")}
</channel>
</rss>`;
};
const getHomePageContents = () => {
return `<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>RSS Radio France pour tous</title>
<link rel="stylesheet" href="https://unpkg.com/mvp.css" />
</head>
<body>
<header>
<h1>RSS Radio France pour tous</h1>
<p>Le site pour rétablir les flux RSS de Radio France</p>
</header>
<main style="padding-top: 0px;">
<section>
<form>
<label for="query">Titre de l'émission</label>
<input
type="search"
name="query"
id="query"
placeholder="Rechercher un podcast…"
required
/>
<button>Rechercher</button>
</form>
</section>
<section id="search-results-container"></section>
</main>
<footer>
<section>
<a href="https://github.com/Aerion/rss-radio-france-pour-tous">
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 16 16"
width="16"
height="16"
style="vertical-align: middle; margin-right: 4px"
>
<path
style="fill: var(--color-link)"
fill-rule="evenodd"
d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0016 8c0-4.42-3.58-8-8-8z"
></path></svg
>Aerion/rss-radio-france-pour-tous</a
>
</section>
</footer>
</body>
<script>
const searchResultsContainer = document.querySelector(
"#search-results-container"
);
const setSearchResults = (searchResults) => {
if (searchResults.length === 0) {
const noResultsNode = document.createElement("p");
noResultsNode.innerHTML = "Aucun résultat trouvé pour cette recherche.<br>Êtes-vous sûr que ce podcast existe sur le site de Radio France ?";
noResultsNode.style.textAlign = "center";
noResultsNode.style.fontWeight = "bold";
searchResultsContainer.replaceChildren(noResultsNode);
} else {
const nodes = searchResults.map((searchResult) => {
const node = document.createElement("aside");
const imgNodeHTML = searchResult['imgUrl'] ? \`<img src="\${searchResult['imgUrl']}" />\` : '<div style="width: 100%;background: lightgray;aspect-ratio: 1/1;"></div>';
node.innerHTML = \`
\${imgNodeHTML}
<h3>\${searchResult["title"]}</h3>
<small>\${searchResult["standfirst"]}</small>
<p>
<a href="\${searchResult['rssUrl']}" target="_blank">Flux RSS</a> - <a href="\${searchResult['path']}" target="_blank">Émission</a>
</p>
\`;
return node;
});
searchResultsContainer.replaceChildren(...nodes);
}
};
const form = document.querySelector("form");
const queryInput = document.querySelector("input");
const handleSubmit = async (evt) => {
evt.preventDefault();
const query = queryInput.value;
if (query.length === 0) {
alert("Un champ de recherche doit être renseigné");
return;
}
const response = await fetch(
"${baseUrl}/search/?query=" +
encodeURIComponent(query)
);
const searchResults = await response.json();
setSearchResults(searchResults);
};
form.addEventListener("submit", handleSubmit);
</script>
</html>
`;
};
const getSearchResults = async (query) => {
const json = await getRadioFranceUrl(
`/stations/search?value=${encodeURIComponent(query)}&include=show`
);
return json.data
.filter((item) => item.resultItems.model === "show")
.map((item) => {
const show = json.included.shows[item.resultItems.relationships.show[0]];
return {
title: show.title,
path: show.path,
standfirst: show.standfirst,
imgUrl: getImgUrl(show.visuals, show.mainImage),
rssUrl: `${baseUrl}${routePrefixRss}${show.id}`,
};
});
};
const handleRequest = async (request) => {
const url = new URL(request.url);
if (url.pathname.startsWith(routePrefixRss)) {
const showId = url.pathname.substring(routePrefixRss.length);
const page = parseInt(url.searchParams.get("page") || "0", 10);
const showDiffusions = await getShowDiffusions(showId, page);
let nextPageUrl;
if (showDiffusions.nextPageIdx !== undefined) {
nextPageUrl = new URL(url);
nextPageUrl.searchParams.delete("page");
nextPageUrl.searchParams.append("page", showDiffusions.nextPageIdx);
} else {
nextPageUrl = undefined;
}
const feed = buildFeed(showDiffusions, nextPageUrl);
return new Response(feed, {
headers: {
"Content-Type": "application/xml; charset=utf-8",
},
});
} else if (url.pathname === routeSearch) {
const searchResults = await getSearchResults(url.searchParams.get("query"));
return new Response(JSON.stringify(searchResults), {
headers: {
"Content-Type": "application/json",
},
});
} else if (url.pathname === "/") {
return new Response(getHomePageContents(), {
headers: {
"Content-Type": "text/html",
},
});
} else {
return new Response(null, { status: 404, statusText: "Not found" });
}
};