iptv/scripts/helpers/Playlist.js

42 lines
925 B
JavaScript
Raw Normal View History

2021-08-01 18:45:20 +02:00
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
2021-08-02 20:15:33 +02:00
this.channels = items.map(item => new Channel(item)).filter(channel => channel.url)
2021-08-02 18:07:10 +02:00
this.updated = false
2021-08-01 18:45:20 +02:00
}
getHeader() {
let header = ['#EXTM3U']
2021-08-01 18:45:20 +02:00
for (let key in this.header.attrs) {
let value = this.header.attrs[key]
if (value) {
header.push(`${key}="${value}"`)
2021-08-01 18:45:20 +02:00
}
}
return header.join(' ')
}
toString(options = {}) {
const config = { raw: false, ...options }
let output = `${this.getHeader()}\n`
2021-08-01 18:45:20 +02:00
for (let channel of this.channels) {
output += channel.toString(config.raw)
}
return output
}
save() {
2021-08-02 18:07:10 +02:00
if (this.updated) {
file.create(this.url, this.toString())
2021-08-01 18:45:20 +02:00
}
}
}