-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
135 lines (121 loc) · 3.67 KB
/
app.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
"use strict";
function log(text) {
let info = document.getElementById('info');
info.append(text + '\n');
}
window.onload = function () {
let fileInput = document.getElementById('plistFile');
fileInput.addEventListener('change', function (e) {
var file = fileInput.files[0];
var reader = new FileReader();
reader.onloadend = function (e) {
try {
parsePlist(reader.result);
} catch(e) {
console.error(e);
log("🛑 " + e);
}
};
reader.readAsText(file);
});
}
function parsePlist(body) {
// http://help.dottoro.com/ljbcjfot.php
var parser = new DOMParser();
var dom = parser.parseFromString(body, "application/xml");
var plist = dom.documentElement;
assert(plist, 'plist');
var ret = [];
var array = plist.childNodes[1];
assert(array, 'array');
var dict = array.childNodes[1];
var array = dict.childNodes[3];
for (var i=0;i<array.childNodes.length;i++) {
var dict = array.childNodes[i];
if (dict.nodeName === 'dict') {
var kvs = dict.childNodes;
if (kvs.length < 11) { continue; }
if (kvs[11].firstChild.nodeValue === "Podcasts") {
var podcasts_array = kvs[3];
for(var m=0;m<podcasts_array.childNodes.length; m++ ) {
var current = podcasts_array.childNodes[m];
if (current.nodeName === 'dict') {
let podcast = parsePodcastDict(current)
if (podcast) {
ret.push(parsePodcastDict(current));
}
}
}
}
}
}
download(ret);
log(`✅ Exported ${ret.length} podcasts`);
}
function parsePodcastDict(dict) {
// feedUrl: "http://www.ximalaya.com/album/4324892.xml"
// storeId: "1211598895"
// title: "《赖世雄美语音标》讲解音频"
// uuid: "55C5D14D-0CCC-4B79-AEA9-17CA3609F522"
var o = {};
for (var i=0;i<dict.children.length-1;i=i+=2) {
var k = dict.children[i];
k = k.textContent;
var v = dict.children[i+1];
v = v.textContent;
o[k] = v;
// console.log(i, dict.childNodes[i], k ,v);
if (k==='title') {
o.title = v.replace(/&/g, ' and ').replace(/\s{2,}/g, ' ');
} else if (k==='feedUrl') {
o.feedUrl = v.replace( /&/g, '&' );
}
}
if (!o.feedUrl) {
log(`⚠️ Exclude podcast "${o.title}" due to missing feed url`)
return null;
}
return o;
}
function generateOPML(podcasts) {
var head = [
'<?xml version="1.0" encoding="UTF-8"?>',
'<opml version="1.0">',
`<head><title>macOS Podcast exporter</title><dateCreated>${new Date().toLocaleString()}</dateCreated></head>`,
'<body>'
];
var body = [];
var tail = [
'</body>',
'</opml>'
];
for(var i=0;i<podcasts.length;i++) {
var current = podcasts[i];
body.push(`<outline type="rss" text="${current.title}" xmlUrl="${current.feedUrl}" />`);
}
return head.join('\n') + body.join('\n') + tail.join('\n');
}
// https://www.bennadel.com/blog/3472-downloading-text-using-blobs-url-createobjecturl-and-the-anchor-download-attribute-in-javascript.htm
function download(podcasts) {
var opml = generateOPML(podcasts);
console.log("opml", opml);
var blob = new Blob(
[ opml ],
{
type : "text/plain;charset=utf-8"
}
);
var downloadUrl = URL.createObjectURL( blob );
var download = document.getElementById("download");
download.setAttribute( "href", downloadUrl );
download.setAttribute( "download", `Podcasts-${new Date().toLocaleString()}.opml` );
download.click();
}
function assert(e, nodeName) {
if (e != null && e.nodeName !== nodeName) {
let warn = document.getElementById('warn');
warn.innerText = e;
console.error(e);
alert(`expected ${nodeName}, current ${e.nodeName}`);
}
}