iptv/scripts/core/playlist.js

63 lines
1.5 KiB
JavaScript
Raw Normal View History

2022-02-06 22:04:56 +01:00
const store = require('./store')
2022-03-11 16:31:05 +01:00
const m3u = require('./m3u')
2022-02-06 22:04:56 +01:00
const _ = require('lodash')
2021-12-12 05:10:18 +01:00
const playlist = {}
2022-02-06 22:04:56 +01:00
class Playlist {
2022-02-14 00:34:24 +01:00
constructor(items = [], options = {}) {
this.header = {}
if (options.public) {
let guides = items
.map(item => (item.guides.length ? item.guides[0].url : null))
.filter(i => i)
this.header['x-tvg-url'] = _.uniq(guides).sort().join(',')
}
2022-02-06 22:04:56 +01:00
this.links = []
2022-02-14 00:34:24 +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'),
'group-title': stream.get('group_title'),
'user-agent': stream.get('user_agent') || undefined
2022-02-14 00:34:24 +01:00
}
} else {
attrs = {
'tvg-id': stream.get('tvg_id'),
status: stream.get('status'),
'user-agent': stream.get('user_agent') || undefined
2022-02-14 00:34:24 +01:00
}
}
2021-12-12 05:10:18 +01:00
2022-02-14 00:34:24 +01:00
const vlcOpts = {
'http-referrer': stream.get('http_referrer') || undefined,
'http-user-agent': stream.get('user_agent') || undefined
2022-02-14 00:34:24 +01:00
}
2021-12-12 05:10:18 +01:00
2022-02-14 00:34:24 +01:00
this.links.push({
url: stream.get('url'),
title: stream.get('title'),
attrs,
vlcOpts
})
}
2021-12-12 05:10:18 +01:00
}
2022-02-06 22:04:56 +01:00
toString() {
2022-03-11 16:31:05 +01:00
return m3u.create(this.links, this.header)
2021-12-12 05:10:18 +01:00
}
2022-02-06 22:04:56 +01:00
}
2022-02-14 00:34:24 +01:00
playlist.create = function (items, options) {
return new Playlist(items, options)
2021-12-12 05:10:18 +01:00
}
module.exports = playlist