From 1a5af21f3567c12866018f6c338b2df21292c03c Mon Sep 17 00:00:00 2001 From: Aleksandr Statciuk Date: Sun, 1 Aug 2021 19:45:20 +0300 Subject: [PATCH] Create Playlist.js --- scripts/helpers/Playlist.js | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 scripts/helpers/Playlist.js diff --git a/scripts/helpers/Playlist.js b/scripts/helpers/Playlist.js new file mode 100644 index 000000000..4e8668edd --- /dev/null +++ b/scripts/helpers/Playlist.js @@ -0,0 +1,44 @@ +const Channel = require('./Channel') +const file = require('./file') + +module.exports = class Playlist { + constructor({ header, items, url, name, country }) { + this.url = url + this.name = name + this.country = country + this.header = header + this.channels = items + .map(item => new Channel({ data: item, header, sourceUrl: url })) + .filter(channel => channel.url) + } + + toString(options = {}) { + const config = { raw: false, ...options } + let parts = ['#EXTM3U'] + for (let key in this.header.attrs) { + let value = this.header.attrs[key] + if (value) { + parts.push(`${key}="${value}"`) + } + } + + let output = `${parts.join(' ')}\n` + for (let channel of this.channels) { + output += channel.toString(config.raw) + } + + return output + } + + save() { + const original = file.read(this.url) + const output = this.toString({ raw: true }) + if (original === output) { + return false + } else { + file.create(this.url, output) + } + + return true + } +}