2025-10-08 21:25:22 +03:00
|
|
|
import { Collection } from '@freearhey/core'
|
2026-05-25 13:29:39 +03:00
|
|
|
import * as sdk from '@iptv-org/sdk'
|
2025-10-08 21:25:22 +03:00
|
|
|
import { Stream } from '../models'
|
|
|
|
|
|
|
|
|
|
type PlaylistOptions = {
|
2026-05-25 13:29:39 +03:00
|
|
|
public?: boolean
|
2025-10-08 21:25:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class Playlist {
|
|
|
|
|
streams: Collection<Stream>
|
2026-05-25 13:29:39 +03:00
|
|
|
public: boolean
|
2025-10-08 21:25:22 +03:00
|
|
|
|
|
|
|
|
constructor(streams: Collection<Stream>, options?: PlaylistOptions) {
|
|
|
|
|
this.streams = streams
|
2026-05-25 13:29:39 +03:00
|
|
|
this.public = options?.public === true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getGuides(): Collection<sdk.Models.Guide> {
|
|
|
|
|
const guides = new Collection<sdk.Models.Guide>()
|
|
|
|
|
|
|
|
|
|
this.streams.forEach(stream => {
|
|
|
|
|
guides.concat(stream.getGuides())
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return guides
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getGuideUrls(): Collection<string> {
|
|
|
|
|
function byFormat(source: sdk.Types.GuideSource) {
|
|
|
|
|
if (source.format === 'GZIP') return 2
|
|
|
|
|
if (source.format === 'XML') return 1
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const urls = new Collection<string>()
|
|
|
|
|
|
|
|
|
|
this.getGuides().forEach((guide: sdk.Models.Guide) => {
|
|
|
|
|
const sources = new Collection(guide.sources)
|
|
|
|
|
|
|
|
|
|
const source = sources
|
|
|
|
|
.filter((source: sdk.Types.GuideSource) => ['GZIP', 'XML'].includes(source.format))
|
|
|
|
|
.sortBy([byFormat], ['desc'])
|
|
|
|
|
.first()
|
|
|
|
|
|
|
|
|
|
if (source) urls.add(source.url)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return urls.uniq()
|
2025-10-08 21:25:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toString() {
|
2026-05-25 13:29:39 +03:00
|
|
|
let output = '#EXTM3U'
|
|
|
|
|
|
|
|
|
|
const guideUrls = this.getGuideUrls()
|
|
|
|
|
if (guideUrls.count()) {
|
|
|
|
|
output += ` x-tvg-url="${guideUrls.join(',')}"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
output += '\r\n'
|
2025-10-08 21:25:22 +03:00
|
|
|
|
|
|
|
|
this.streams.forEach((stream: Stream) => {
|
2026-05-25 13:29:39 +03:00
|
|
|
output += stream.toString({ public: this.public }) + '\r\n'
|
2025-10-08 21:25:22 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return output
|
|
|
|
|
}
|
|
|
|
|
}
|