Files
iptv/scripts/generators/regionsGenerator.ts

65 lines
2.2 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'
2023-09-22 06:22:47 +03:00
import { PUBLIC_DIR } from '../constants'
2025-04-23 20:56:19 +03:00
import { Generator } from './generator'
import { EOL } from 'node:os'
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
if (region.isWorldwide()) return
2023-09-22 06:22:47 +03:00
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
})
2025-03-29 11:39:46 +03:00
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())
2025-04-23 20:56:19 +03:00
this.logFile.append(
2025-03-29 11:39:46 +03:00
JSON.stringify({
type: 'region',
filepath: internationalFilepath,
count: internationalPlaylist.streams.count()
2025-04-23 20:56:19 +03:00
}) + EOL
2025-03-29 11:39:46 +03:00
)
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())
2025-04-23 20:56:19 +03:00
this.logFile.append(
JSON.stringify({ type: 'region', filepath, count: playlist.streams.count() }) + EOL
)
2023-09-22 06:22:47 +03:00
}
}