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.
iptv/scripts/core/playlistParser.ts

52 lines
1.4 KiB
TypeScript

3 months ago
import { Collection, Storage, Dictionary } from '@freearhey/core'
2 years ago
import parser from 'iptv-playlist-parser'
2 years ago
import { Stream } from '../models'
2 years ago
3 months ago
type PlaylistPareserProps = {
storage: Storage
feedsGroupedByChannelId: Dictionary
3 months ago
channelsKeyById: Dictionary
3 months ago
}
2 years ago
export class PlaylistParser {
storage: Storage
3 months ago
feedsGroupedByChannelId: Dictionary
3 months ago
channelsKeyById: Dictionary
2 years ago
3 months ago
constructor({ storage, feedsGroupedByChannelId, channelsKeyById }: PlaylistPareserProps) {
2 years ago
this.storage = storage
3 months ago
this.feedsGroupedByChannelId = feedsGroupedByChannelId
3 months ago
this.channelsKeyById = channelsKeyById
2 years ago
}
2 years ago
async parse(files: string[]): Promise<Collection> {
let streams = new Collection()
for (const filepath of files) {
if (!this.storage.existsSync(filepath)) continue
4 months ago
const _streams: Collection = await this.parseFile(filepath)
2 years ago
streams = streams.concat(_streams)
}
return streams
}
async parseFile(filepath: string): Promise<Collection> {
const content = await this.storage.load(filepath)
2 years ago
const parsed: parser.Playlist = parser.parse(content)
3 months ago
const streams = new Collection(parsed.items).map((data: parser.PlaylistItem) => {
3 months ago
const stream = new Stream()
.fromPlaylistItem(data)
3 months ago
.withFeed(this.feedsGroupedByChannelId)
3 months ago
.withChannel(this.channelsKeyById)
3 months ago
.setFilepath(filepath)
return stream
2 years ago
})
2 years ago
return streams
2 years ago
}
}