2022-02-06 22:04:56 +01:00
|
|
|
const store = require('./store')
|
|
|
|
const _ = require('lodash')
|
2021-12-12 05:10:18 +01:00
|
|
|
|
|
|
|
const playlist = {}
|
|
|
|
|
2022-02-06 22:04:56 +01:00
|
|
|
class Playlist {
|
|
|
|
constructor() {
|
|
|
|
this.links = []
|
|
|
|
}
|
2021-12-12 05:10:18 +01:00
|
|
|
|
2022-02-06 22:04:56 +01:00
|
|
|
setHeader(attrs = {}) {
|
|
|
|
this.header = attrs
|
|
|
|
}
|
2021-12-12 05:10:18 +01:00
|
|
|
|
2022-02-06 22:04:56 +01:00
|
|
|
add(url, title, attrs, vlcOpts) {
|
|
|
|
this.links.push({ url, title, attrs, vlcOpts })
|
2021-12-12 05:10:18 +01:00
|
|
|
}
|
|
|
|
|
2022-02-06 22:04:56 +01:00
|
|
|
toString() {
|
|
|
|
let output = `#EXTM3U`
|
|
|
|
for (const attr in this.header) {
|
|
|
|
const value = this.header[attr]
|
|
|
|
output += ` ${attr}="${value}"`
|
|
|
|
}
|
|
|
|
output += `\n`
|
|
|
|
|
|
|
|
for (const link of this.links) {
|
|
|
|
output += `#EXTINF:-1`
|
|
|
|
for (const name in link.attrs) {
|
|
|
|
const value = link.attrs[name]
|
|
|
|
if (value !== undefined) {
|
|
|
|
output += ` ${name}="${value}"`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output += `,${link.title}\n`
|
2021-12-12 05:10:18 +01:00
|
|
|
|
2022-02-06 22:04:56 +01:00
|
|
|
for (const name in link.vlcOpts) {
|
|
|
|
const value = link.vlcOpts[name]
|
|
|
|
if (value !== undefined) {
|
|
|
|
output += `#EXTVLCOPT:${name}=${value}\n`
|
|
|
|
}
|
|
|
|
}
|
2021-12-12 05:10:18 +01:00
|
|
|
|
2022-02-06 22:04:56 +01:00
|
|
|
output += `${link.url}\n`
|
2021-12-12 05:10:18 +01:00
|
|
|
}
|
2022-02-06 22:04:56 +01:00
|
|
|
|
|
|
|
return output
|
2021-12-12 05:10:18 +01:00
|
|
|
}
|
2022-02-06 22:04:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
playlist.create = function (items = [], options = {}) {
|
|
|
|
const p = new Playlist()
|
|
|
|
|
|
|
|
const header = {}
|
|
|
|
if (options.public) {
|
2022-02-07 23:11:47 +01:00
|
|
|
let guides = items.map(item => item.tvg_url).filter(i => i)
|
|
|
|
header['x-tvg-url'] = _.uniq(guides).sort().join(',')
|
2021-12-12 05:10:18 +01:00
|
|
|
}
|
2022-02-06 22:04:56 +01:00
|
|
|
p.setHeader(header)
|
2021-12-12 05:10:18 +01:00
|
|
|
|
2022-02-06 22:04:56 +01:00
|
|
|
for (const item of items) {
|
|
|
|
const stream = store.create(item)
|
|
|
|
|
|
|
|
let attrs
|
|
|
|
if (options.public) {
|
|
|
|
attrs = {
|
|
|
|
'tvg-id': stream.get('tvg_id'),
|
|
|
|
'tvg-country': stream.get('tvg_country'),
|
|
|
|
'tvg-language': stream.get('tvg_language'),
|
|
|
|
'tvg-logo': stream.get('tvg_logo'),
|
|
|
|
'user-agent': stream.get('http.user-agent') || undefined,
|
|
|
|
'group-title': stream.get('group_title')
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
attrs = {
|
|
|
|
'tvg-id': stream.get('tvg_id'),
|
|
|
|
'user-agent': stream.get('http.user-agent') || undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p.add(stream.get('url'), stream.get('title'), attrs, {
|
|
|
|
'http-referrer': stream.get('http.referrer') || undefined,
|
|
|
|
'http-user-agent': stream.get('http.user-agent') || undefined
|
|
|
|
})
|
|
|
|
}
|
2021-12-12 05:10:18 +01:00
|
|
|
|
2022-02-06 22:04:56 +01:00
|
|
|
return p
|
2021-12-12 05:10:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = playlist
|