mirror of https://github.com/iptv-org/iptv
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
import parser from 'iptv-playlist-parser'
|
|
import { Playlist, Stream } from '../models'
|
|
import { Collection, Storage } from './'
|
|
|
|
export class PlaylistParser {
|
|
storage: Storage
|
|
|
|
constructor({ storage }: { storage: Storage }) {
|
|
this.storage = storage
|
|
}
|
|
|
|
async parse(filepath: string): Promise<Playlist> {
|
|
const streams = new Collection()
|
|
|
|
const content = await this.storage.read(filepath)
|
|
const parsed: parser.Playlist = parser.parse(content)
|
|
|
|
parsed.items.forEach((item: parser.PlaylistItem) => {
|
|
const { name, label, quality } = parseTitle(item.name)
|
|
const stream = new Stream({
|
|
channel: item.tvg.id,
|
|
name,
|
|
label,
|
|
quality,
|
|
filepath,
|
|
line: item.line,
|
|
url: item.url,
|
|
httpReferrer: item.http.referrer,
|
|
userAgent: item.http['user-agent']
|
|
})
|
|
|
|
streams.add(stream)
|
|
})
|
|
|
|
return new Playlist(streams)
|
|
}
|
|
}
|
|
|
|
function parseTitle(title: string): { name: string; label: string; quality: string } {
|
|
const [, label] = title.match(/ \[(.*)\]$/) || [null, '']
|
|
const [, quality] = title.match(/ \(([0-9]+p)\)/) || [null, '']
|
|
const name = title.replace(` (${quality})`, '').replace(` [${label}]`, '')
|
|
|
|
return { name, label, quality }
|
|
}
|