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

65 lines
2.2 KiB
TypeScript

3 months ago
import { Collection, Storage, File } from '@freearhey/core'
4 months ago
import { Playlist, Region, Stream } from '../models'
import { PUBLIC_DIR } from '../constants'
3 months ago
import { Generator } from './generator'
import { EOL } from 'node:os'
type RegionsGeneratorProps = {
streams: Collection
regions: Collection
3 months ago
logFile: File
}
export class RegionsGenerator implements Generator {
streams: Collection
regions: Collection
storage: Storage
3 months ago
logFile: File
3 months ago
constructor({ streams, regions, logFile }: RegionsGeneratorProps) {
this.streams = streams
this.regions = regions
this.storage = new Storage(PUBLIC_DIR)
3 months ago
this.logFile = logFile
}
async generate(): Promise<void> {
const streams = this.streams
4 months ago
.orderBy((stream: Stream) => stream.getTitle())
.filter((stream: Stream) => stream.isSFW())
this.regions.forEach(async (region: Region) => {
4 months ago
if (region.isWorldwide()) return
4 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())
3 months ago
this.logFile.append(
JSON.stringify({ type: 'region', filepath, count: playlist.streams.count() }) + EOL
4 months ago
)
})
4 months ago
const internationalStreams = streams.filter((stream: Stream) => stream.isInternational())
const internationalPlaylist = new Playlist(internationalStreams, { public: true })
const internationalFilepath = 'regions/int.m3u'
await this.storage.save(internationalFilepath, internationalPlaylist.toString())
3 months ago
this.logFile.append(
4 months ago
JSON.stringify({
type: 'region',
filepath: internationalFilepath,
count: internationalPlaylist.streams.count()
3 months ago
}) + EOL
4 months ago
)
const undefinedStreams = streams.filter((stream: Stream) => !stream.hasBroadcastArea())
const playlist = new Playlist(undefinedStreams, { public: true })
const filepath = 'regions/undefined.m3u'
await this.storage.save(filepath, playlist.toString())
3 months ago
this.logFile.append(
JSON.stringify({ type: 'region', filepath, count: playlist.streams.count() }) + EOL
)
}
}