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/generators/regionsGenerator.ts

42 lines
1.3 KiB
TypeScript

7 months ago
import { Collection, Storage, File } from '@freearhey/core'
8 months ago
import { Playlist, Region, Stream } from '../models'
3 months ago
import { PUBLIC_DIR, EOL } from '../constants'
7 months ago
import { Generator } from './generator'
type RegionsGeneratorProps = {
streams: Collection
regions: Collection
7 months ago
logFile: File
}
export class RegionsGenerator implements Generator {
streams: Collection
regions: Collection
storage: Storage
7 months ago
logFile: File
7 months ago
constructor({ streams, regions, logFile }: RegionsGeneratorProps) {
4 months ago
this.streams = streams.clone()
this.regions = regions
this.storage = new Storage(PUBLIC_DIR)
7 months ago
this.logFile = logFile
}
async generate(): Promise<void> {
const streams = this.streams
8 months ago
.orderBy((stream: Stream) => stream.getTitle())
.filter((stream: Stream) => stream.isSFW())
this.regions.forEach(async (region: Region) => {
8 months ago
const regionStreams = streams.filter((stream: Stream) => stream.isBroadcastInRegion(region))
const playlist = new Playlist(regionStreams, { public: true })
const filepath = `regions/${region.code.toLowerCase()}.m3u`
await this.storage.save(filepath, playlist.toString())
7 months ago
this.logFile.append(
JSON.stringify({ type: 'region', filepath, count: playlist.streams.count() }) + EOL
8 months ago
)
})
}
}