-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathplaylist.js
54 lines (44 loc) · 1.46 KB
/
playlist.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
'use strict';
const {
ElementTree,
Element: element,
SubElement: subElement
} = require('elementtree');
const escape = title => title.replace(/-/g, '\u2013');
const trimLeft = (strings, ...values) => interweave(strings, ...values).trimLeft();
const plsEntry = (stream, pos = 1) => trimLeft`
File${pos}=${stream.location}
Title${pos}=${stream.title}
Length${pos}=-1`;
module.exports.pls = streams => trimLeft`
[playlist]
NumberOfEntries=${streams.length}
${streams.map((stream, i) => plsEntry(stream, i + 1)).join('\n\n')}
Version=2`;
const m3u8Entry = stream => trimLeft`
#EXTINF:0,${escape(stream.title)}
${stream.location}`;
module.exports.m3u8 = streams => trimLeft`
#EXTM3U
${streams.map(stream => m3u8Entry(stream)).join('\n')}`;
const xspfTrack = (trackList, stream) => {
const track = subElement(trackList, 'track');
const location = subElement(track, 'location');
location.text = stream.location;
const title = subElement(track, 'title');
title.text = stream.title;
};
module.exports.xspf = streams => {
const root = element('playlist');
root.set('xmlns', 'http://xspf.org/ns/0/');
root.set('version', 1);
const trackList = subElement(root, 'trackList');
streams.forEach(stream => xspfTrack(trackList, stream));
return (new ElementTree(root)).write({ indent: 2 });
};
function interweave(strings, ...values) {
let output = '';
values.forEach((val, i) => (output += strings[i] + val));
output += strings[values.length];
return output;
}