Files
iptv/scripts/generators/regionsGenerator.ts

42 lines
1.3 KiB
TypeScript
Raw Normal View History

2025-04-23 20:56:19 +03:00
import { Collection, Storage, File } from '@freearhey/core'
2025-03-29 11:39:46 +03:00
import { Playlist, Region, Stream } from '../models'
2025-07-30 19:57:31 +03:00
import { PUBLIC_DIR, EOL } from '../constants'
2025-04-23 20:56:19 +03:00
import { Generator } from './generator'
2023-09-22 06:22:47 +03:00
type RegionsGeneratorProps = {
streams: Collection
regions: Collection
2025-04-23 20:56:19 +03:00
logFile: File
2023-09-22 06:22:47 +03:00
}
export class RegionsGenerator implements Generator {
streams: Collection
regions: Collection
storage: Storage
2025-04-23 20:56:19 +03:00
logFile: File
2023-09-22 06:22:47 +03:00
2025-04-23 20:56:19 +03:00
constructor({ streams, regions, logFile }: RegionsGeneratorProps) {
2025-07-10 21:13:43 +03:00
this.streams = streams.clone()
2023-09-22 06:22:47 +03:00
this.regions = regions
this.storage = new Storage(PUBLIC_DIR)
2025-04-23 20:56:19 +03:00
this.logFile = logFile
2023-09-22 06:22:47 +03:00
}
async generate(): Promise<void> {
const streams = this.streams
2025-03-29 11:39:46 +03:00
.orderBy((stream: Stream) => stream.getTitle())
.filter((stream: Stream) => stream.isSFW())
2023-09-22 06:22:47 +03:00
this.regions.forEach(async (region: Region) => {
2025-03-29 11:39:46 +03:00
const regionStreams = streams.filter((stream: Stream) => stream.isBroadcastInRegion(region))
2023-09-22 06:22:47 +03:00
const playlist = new Playlist(regionStreams, { public: true })
const filepath = `regions/${region.code.toLowerCase()}.m3u`
await this.storage.save(filepath, playlist.toString())
2025-04-23 20:56:19 +03:00
this.logFile.append(
JSON.stringify({ type: 'region', filepath, count: playlist.streams.count() }) + EOL
2025-03-29 11:39:46 +03:00
)
2023-09-22 06:22:47 +03:00
})
}
}