Files
iptv/scripts/core/playlistParser.ts

52 lines
1.4 KiB
TypeScript
Raw Normal View History

2025-03-29 11:39:46 +03:00
import { Collection, Storage, Dictionary } from '@freearhey/core'
2023-09-15 18:40:35 +03:00
import parser from 'iptv-playlist-parser'
2023-09-17 04:08:50 +03:00
import { Stream } from '../models'
2023-09-15 18:40:35 +03:00
2025-03-29 11:39:46 +03:00
type PlaylistPareserProps = {
storage: Storage
feedsGroupedByChannelId: Dictionary
2025-04-16 20:54:55 +03:00
channelsKeyById: Dictionary
2025-03-29 11:39:46 +03:00
}
2023-09-15 18:40:35 +03:00
export class PlaylistParser {
storage: Storage
2025-03-29 11:39:46 +03:00
feedsGroupedByChannelId: Dictionary
2025-04-16 20:54:55 +03:00
channelsKeyById: Dictionary
2023-09-15 18:40:35 +03:00
2025-04-16 20:54:55 +03:00
constructor({ storage, feedsGroupedByChannelId, channelsKeyById }: PlaylistPareserProps) {
2023-09-15 18:40:35 +03:00
this.storage = storage
2025-03-29 11:39:46 +03:00
this.feedsGroupedByChannelId = feedsGroupedByChannelId
2025-04-16 20:54:55 +03:00
this.channelsKeyById = channelsKeyById
2023-09-15 18:40:35 +03:00
}
2023-09-17 04:08:50 +03:00
async parse(files: string[]): Promise<Collection> {
let streams = new Collection()
2023-09-22 06:22:47 +03:00
for (const filepath of files) {
2025-05-19 19:12:11 +03:00
if (!this.storage.existsSync(filepath)) continue
2025-03-15 07:31:10 +03:00
const _streams: Collection = await this.parseFile(filepath)
2023-09-17 04:08:50 +03:00
streams = streams.concat(_streams)
}
return streams
}
async parseFile(filepath: string): Promise<Collection> {
2023-09-22 05:17:22 +03:00
const content = await this.storage.load(filepath)
2023-09-15 18:40:35 +03:00
const parsed: parser.Playlist = parser.parse(content)
2025-03-29 11:39:46 +03:00
const streams = new Collection(parsed.items).map((data: parser.PlaylistItem) => {
2025-04-16 20:54:55 +03:00
const stream = new Stream()
.fromPlaylistItem(data)
2025-03-29 11:39:46 +03:00
.withFeed(this.feedsGroupedByChannelId)
2025-04-16 20:54:55 +03:00
.withChannel(this.channelsKeyById)
2025-03-29 11:39:46 +03:00
.setFilepath(filepath)
return stream
2023-09-15 18:40:35 +03:00
})
2023-09-17 04:08:50 +03:00
return streams
2023-09-15 18:40:35 +03:00
}
}