|
const path = require("path"); |
|
const fs = require("fs"); |
|
|
|
function generateXSPF({ |
|
title = "Getting started", |
|
children = [ |
|
{ |
|
pre: "http://192.168.0.103:8080", |
|
path: "1.%20Getting%20Started", |
|
file: "2-%20Prerequisites.mp4", |
|
duration: 38615, |
|
}, |
|
], |
|
}) { |
|
const playListStart = `<?xml version="1.0" encoding="UTF-8"?> |
|
<playlist xmlns="http://xspf.org/ns/0/" xmlns:vlc="http://www.videolan.org/vlc/playlist/ns/0/" version="1"> |
|
`; |
|
|
|
const playListEnd = `</playlist>`; |
|
|
|
const _title = () => `<title>${title}</title>`; |
|
const _track = ({ |
|
pre = "http://192.168.0.103:8080", |
|
path = "1.%20Getting%20Started", |
|
file = "2-%20Prerequisites.mp4", |
|
duration = 38615, |
|
index = 0, |
|
}) => |
|
`<track> |
|
<location>${[pre, path, file].join("/")}</location>` + |
|
// `<duration>${duration}</duration>` + |
|
` |
|
<extension application="http://www.videolan.org/vlc/playlist/0"> |
|
<vlc:id>${index}</vlc:id> |
|
</extension> |
|
</track>`; |
|
|
|
const trackList = (arr = []) => |
|
`<trackList>${arr |
|
.map((obj, index) => _track({ ...obj, index })) |
|
.join("")}</trackList>`; |
|
|
|
const extensionTags = ( |
|
n |
|
) => `<extension application="http://www.videolan.org/vlc/playlist/0"> |
|
${Array(n) |
|
.fill(null) |
|
.map((_, i) => `<vlc:item tid="${i}" />`) |
|
.join("")} |
|
|
|
</extension>`; |
|
const final = [ |
|
playListStart, |
|
_title(title), |
|
trackList(children), |
|
extensionTags(children.length), |
|
playListEnd, |
|
].join(""); |
|
|
|
return final; |
|
} |
|
|
|
/** |
|
* |
|
* @returns {[{title, fileContent}]} |
|
*/ |
|
function getFileObjects(obj, pre = "http://192.168.0.103:8080") { |
|
const op = []; |
|
|
|
Object.entries(obj) |
|
.sort(([a], [b]) => |
|
a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }) |
|
) |
|
.forEach(([key, value]) => { |
|
const title = key; |
|
const children = Object.entries(value) |
|
.sort(([a], [b]) => |
|
a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }) |
|
) |
|
.reduce((accum, [key, value], index) => { |
|
accum.push({ |
|
pre: pre, |
|
path: encodeURIComponent(title), |
|
file: encodeURIComponent(key), |
|
duration: value, |
|
}); |
|
return accum; |
|
}, []); |
|
|
|
// if (op.length === 0) |
|
op.push({ title, fileContent: generateXSPF({ title, children }) }); |
|
}); |
|
return op; |
|
} |
|
|
|
function generateFilesFromFileObjects(pathToJSONFile = {}) { |
|
let obj = pathToJSONFile; |
|
if (typeof pathToJSONFile === typeof "") { |
|
obj = require(pathToJSONFile); |
|
} |
|
|
|
const fileObjects = getFileObjects(obj); |
|
|
|
const HIDDEN_CHARACTER = ` `; |
|
fileObjects.forEach(({ title, fileContent }) => { |
|
const friendlyTitle = |
|
title |
|
.replaceAll("'", "") |
|
.replaceAll('"', "") |
|
.replaceAll(".", "") |
|
.replaceAll(HIDDEN_CHARACTER, "") |
|
.replaceAll(" ", "-") + ".xspf"; |
|
const filePath = path.join(__dirname, friendlyTitle); |
|
fs.writeFileSync(filePath, fileContent); |
|
}); |
|
} |
|
|
|
module.exports = { generateFilesFromFileObjects }; |
|
|
|
// generateFilesFromFileObjects("./one.json"); |