Files
iptv/scripts/generators/indexRegionGenerator.ts

67 lines
2.0 KiB
TypeScript
Raw Normal View History

2025-04-23 20:56:19 +03:00
import { Collection, Storage, File } from '@freearhey/core'
2023-09-22 06:22:47 +03:00
import { Stream, Playlist, Region } from '../models'
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 IndexRegionGeneratorProps = {
streams: Collection
regions: Collection
2025-04-23 20:56:19 +03:00
logFile: File
2023-09-22 06:22:47 +03:00
}
export class IndexRegionGenerator 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 }: IndexRegionGeneratorProps) {
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> {
let groupedStreams = new Collection()
this.streams
.orderBy((stream: Stream) => stream.getTitle())
2025-03-31 07:50:35 +03:00
.filter((stream: Stream) => stream.isSFW())
2023-09-22 06:22:47 +03:00
.forEach((stream: Stream) => {
2025-03-31 07:50:35 +03:00
if (stream.isInternational()) {
const streamClone = stream.clone()
streamClone.groupTitle = 'International'
groupedStreams.push(streamClone)
return
}
2025-03-29 11:39:46 +03:00
if (!stream.hasBroadcastArea()) {
2023-09-22 06:22:47 +03:00
const streamClone = stream.clone()
streamClone.groupTitle = 'Undefined'
groupedStreams.push(streamClone)
return
}
2025-03-29 11:39:46 +03:00
stream.getBroadcastRegions().forEach((region: Region) => {
2023-09-22 06:22:47 +03:00
const streamClone = stream.clone()
streamClone.groupTitle = region.name
groupedStreams.push(streamClone)
})
})
groupedStreams = groupedStreams.orderBy((stream: Stream) => {
2025-03-31 07:50:35 +03:00
if (stream.groupTitle === 'International') return 'ZZ'
if (stream.groupTitle === 'Undefined') return 'ZZZ'
2023-09-22 06:22:47 +03:00
return stream.groupTitle
})
const playlist = new Playlist(groupedStreams, { public: true })
const filepath = 'index.region.m3u'
await this.storage.save(filepath, playlist.toString())
2025-04-23 20:56:19 +03:00
this.logFile.append(
JSON.stringify({ type: 'index', filepath, count: playlist.streams.count() }) + EOL
)
2023-09-22 06:22:47 +03:00
}
}