-
Notifications
You must be signed in to change notification settings - Fork 1
/
podcast-feed.js
336 lines (303 loc) · 10.9 KB
/
podcast-feed.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
import ElementBase from "./lib/element-base.js";
import { $, matchData, widont } from "./lib/common.js";
import app from "./app.js";
import "./podcast-episode.js";
import "./podcast-menu.js";
var removeCDATA = str => str.replace(/^<!\[CDATA\[|\]\]>$/g, "").trim();
var parser = new DOMParser();
var sanitizeHTML = function(source) {
// strip everything after a manual line break
source = source.replace(/<br\s*\/?>.*/, "");
var fragment = parser.parseFromString(removeCDATA(source), "text/html");
var paragraphs = fragment.querySelectorAll("p");
var blurb;
if (paragraphs.length) {
blurb = paragraphs[0].innerText;
} else {
blurb = fragment.body.innerText;
}
return blurb;
}
class PodcastFeed extends ElementBase {
static boundMethods = [
"load",
"onClickExpand",
"onClickPlayLatest",
"onClickMore",
"onClickUnsubscribe",
"onClickRename",
"onClickSearch",
"onClickRefresh",
"onClickMarkHeard",
"sendPlayRequest"
]
feed = null;
query = null;
offset = 0;
limit = 10;
pageSize = 10;
proxied = true;
etag = null;
since = null;
constructor() {
super();
this.elements.playLatest.addEventListener("click", this.onClickPlayLatest);
this.elements.title.addEventListener("click", this.onClickExpand);
this.elements.showMoreButton.addEventListener("click", this.onClickMore);
// this.elements.unsubscribeButton.addEventListener("click", this.onClickUnsubscribe);
// this.elements.renameButton.addEventListener("click", this.onClickRename);
// this.elements.searchButton.addEventListener("click", this.onClickSearch);
// this.elements.refreshButton.addEventListener("click", this.onClickRefresh);
// this.elements.markHeardButton.addEventListener("click", this.onClickMarkHeard);
this.addEventListener("menu-action:refresh", this.onClickRefresh);
this.addEventListener("menu-action:search", this.onClickSearch);
this.addEventListener("menu-action:unsubscribe", this.onClickUnsubscribe);
this.addEventListener("menu-action:rename", this.onClickRename);
this.addEventListener("menu-action:clear", this.onClickMarkHeard);
this.addEventListener("menu-action:latest", this.onClickPlayLatest);
this.addEventListener("menu-state", e => this.classList.toggle("menu-open", e.detail.open));
this.addEventListener("episode-play", this.sendPlayRequest);
}
connectedCallback() {
app.on("refresh-all", this.load);
app.on("clear-all", this.onClickMarkHeard);
}
disconnectedCallback() {
app.off("refresh-all", this.load);
app.off("clear-all", this.onClickMarkHeard);
}
static observedAttributes = ["src"]
static mirroredProps = ["src"]
attributeChangedCallback(attr, was, value) {
switch (attr) {
case "src":
this.etag = null;
this.since = null;
this.load(value);
break;
}
}
getHeader(xhr, header) {
var headers = xhr.getAllResponseHeaders();
var includes = new RegExp("^" + header, "mi");
if (!includes.test(headers)) {
return null;
}
return xhr.getResponseHeader(header);
}
getXML(url) {
return new Promise((ok, fail) => {
var xhr = new XMLHttpRequest();
xhr.open("GET", url.toString());
xhr.responseType = "document";
if (this.etag) {
xhr.setRequestHeader("If-None-Match", this.etag);
}
if (this.since) {
xhr.setRequestHeader("If-Modified-Since", this.since);
}
xhr.send();
xhr.onload = () => {
if (xhr.status == 304) return fail(xhr);
this.etag = this.getHeader(xhr, "etag");
// nobody correctly sets allow-headers for CORS requests
// but the browser will still reject them
// so only store the last-modified if it's explicitly allowed
var allowHeaders = this.getHeader(xhr, "access-control-allow-headers");
if (this.proxied || (allowHeaders && allowHeaders.match(/if-modified-since/))) {
// this.since = this.getHeader(xhr, "last-modified");
}
ok(xhr);
};
xhr.onerror = err => fail(xhr);
});
}
proxyXML(dest) {
var here = window.location.pathname;
// remove trailing path parts
here = here.replace(/\/[^\/]*$/, "");
var url = new URL(window.location.origin + here + "/proxy");
url.searchParams.set("url", dest);
return this.getXML(url);
}
async requestFeed(url) {
var request;
try {
// technically, everything is proxied now
// the bandwidth savings are too high to ignore (5MB vs. 1MB)
if (this.proxied) {
request = await this.proxyXML(url);
console.log(`Successful proxy request for ${url}`);
} else {
request = await this.getXML(url);
console.log(`Successful CORS request for ${url}`);
}
} catch (err) {
// retry through the proxy
if (!this.proxied && err.status == 0) {
this.proxied = true;
return this.requestFeed(url);
}
if (err.status == 304) {
throw err;
}
}
return request.response;
}
async load(url = this.src) {
this.classList.add("loading");
var metadata = this.metadata = await app.feeds.get(url);
this.elements.title.innerHTML = metadata.renamed || metadata.title || this.elements.title.innerHTML;
try {
var xml = await this.requestFeed(url);
} catch (err) {
this.classList.remove("loading");
if (err.status == 304) {
console.info("Feed is unchanged since last request", url);
} else {
console.error("Unable to load feed: ", url);
}
return;
}
this.classList.remove("loading");
var parsed = this.parseFeed(xml);
// save title and current request time for later
metadata.title = parsed.title;
metadata.requested = Date.now();
metadata.latest = parsed.latest;
metadata.credit = parsed.credit;
app.feeds.set(url, metadata);
// update UI
var title = metadata.renamed || metadata.title
this.elements.title.innerHTML = title;
var feedTitleLabels = this.shadowRoot.querySelectorAll(".feed-name");
feedTitleLabels.forEach(t => t.innerHTML = title);
this.feed = parsed;
var listened = metadata.listened || 0;
var unheard = this.feed.items.filter(f => f.date > listened).length;
this.dataset.unheard = unheard;
// render episode items
await window.customElements.whenDefined("podcast-episode");
this.render();
}
getViewable() {
if (this.query) {
var term = new RegExp(this.query, "i");
return this.feed.items.filter(item => item.title.match(term) || item.description.match(term));
}
return this.feed.items.slice(this.offset, this.offset + this.limit);
}
render() {
this.classList.toggle("searching", this.query);
var items = this.getViewable();
this.elements.showMoreButton.style.display = items.length == this.feed.items.length ? "none" : "";
matchData(this, items, "enclosure", function(item) {
var episode = document.createElement("podcast-episode");
episode.setAttribute("src", item.enclosure);
episode.episodeData = item;
episode.setAttribute("role", "listitem");
return episode;
});
}
parseFeed(document) {
var parsed = {};
var artwork = $.one("image url", document);
artwork = artwork ? artwork.textContent.trim() : null;
parsed.items = $("item", document).map(function(item) {
var result = {
description: "No description provided."
};
["pubDate", "title", "link"].forEach(function(k) {
var element = $.one(k, item);
var text = "";
if (element) text = removeCDATA(element.textContent);
if (text) result[k] = text;
});
var description = item.querySelector("encoded") || item.querySelector("description");
if (description) {
result.description = sanitizeHTML(description.textContent);
}
var enclosure = item.querySelector("enclosure");
if (!enclosure) return null;
result.enclosure = enclosure.getAttribute("url");
result.date = new Date(result.pubDate ? Date.parse(result.pubDate) : 0);
var trackArt = item.getElementsByTagName("itunes:image")[0];
result.artwork = trackArt ? trackArt.getAttribute("href") : artwork;
return result;
}).filter(i => i).sort((a, b) => b.date - a.date);
parsed.title = $.one("channel title", document).textContent.trim();
parsed.latest = Math.max(...parsed.items.map(i => i.date));
var credit = document.getElementsByTagName("itunes:author")[0] ||
document.getElementsByTagName("media:credit")[0] ||
$.one("author", document) || null;
parsed.credit = credit ? credit.textContent.trim() : "";
return parsed;
}
onClickPlayLatest() {
var episode = this.querySelector("podcast-episode");
episode.onClickPlay();
}
onClickSearch() {
var query = prompt("Search feed for term:", this.query || "");
this.query = query;
if (query) this.classList.add("expanded");
this.render();
}
async onClickRename() {
var metadata = await app.feeds.get(this.src);
var name = prompt("New name?", metadata.renamed || metadata.title)
if (!name) return;
metadata.renamed = name.trim();
await app.feeds.set(this.src, metadata);
this.elements.title.innerHTML = metadata.renamed || metadata.title;
this.feed.title = metadata.renamed;
}
onClickRefresh() {
this.load();
}
onClickUnsubscribe() {
app.fire("podcast-unsubscribe", { url: this.src });
}
onClickExpand() {
var expanded = this.classList.toggle("expanded");
if (expanded) {
this.elements.title.scrollIntoView({ behavior: "smooth", block: "start" });
this.elements.itemsHeader.focus({ preventScroll: true });
if (Number(this.dataset.unheard)) {
var first = this.querySelector("podcast-episode");
if (first && !first.classList.contains("expanded")) first.onExpand();
}
} else {
this.elements.title.scrollIntoView({ behavior: "smooth", block: "center" });
}
}
onClickMore() {
this.limit += this.pageSize;
this.render();
}
async onClickMarkHeard() {
var metadata = await app.feeds.get(this.src);
metadata.listened = Date.now();
await app.feeds.set(this.src, metadata);
this.dataset.unheard = 0;
// close, but do not scroll - animation needs to run
this.classList.toggle("expanded", false);
this.elements.expandButton.setAttribute("aria-pressed", false);
}
async sendPlayRequest(e) {
var { url } = e.detail;
var episode = this.feed.items.find(e => e.enclosure == url);
app.fire("play-request", {
...episode,
feed: this.feed.title,
credit: this.feed.credit
});
var metadata = await app.feeds.get(this.src);
var listened = Math.max(metadata.listened || 0, episode.date * 1);
metadata.listened = listened;
var unheard = this.feed.items.filter(f => f.date > listened).length;
app.feeds.set(this.src, metadata);
this.dataset.unheard = unheard;
}
}
PodcastFeed.define("podcast-feed", "podcast-feed.html");