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/indexCountryGenerator.ts

65 lines
1.9 KiB
TypeScript

3 months ago
import { Collection, Storage, File } from '@freearhey/core'
4 months ago
import { Stream, Playlist, Country } from '../models'
import { PUBLIC_DIR } from '../constants'
3 months ago
import { Generator } from './generator'
import { EOL } from 'node:os'
type IndexCountryGeneratorProps = {
streams: Collection
3 months ago
logFile: File
}
export class IndexCountryGenerator implements Generator {
streams: Collection
storage: Storage
3 months ago
logFile: File
3 months ago
constructor({ streams, logFile }: IndexCountryGeneratorProps) {
this.streams = streams
this.storage = new Storage(PUBLIC_DIR)
3 months ago
this.logFile = logFile
}
async generate(): Promise<void> {
let groupedStreams = new Collection()
this.streams
4 months ago
.orderBy((stream: Stream) => stream.getTitle())
.filter((stream: Stream) => stream.isSFW())
.forEach((stream: Stream) => {
if (!stream.hasBroadcastArea()) {
const streamClone = stream.clone()
streamClone.groupTitle = 'Undefined'
groupedStreams.add(streamClone)
return
}
if (stream.isInternational()) {
const streamClone = stream.clone()
streamClone.groupTitle = 'International'
groupedStreams.add(streamClone)
}
4 months ago
stream.getBroadcastCountries().forEach((country: Country) => {
const streamClone = stream.clone()
streamClone.groupTitle = country.name
groupedStreams.add(streamClone)
})
})
groupedStreams = groupedStreams.orderBy((stream: Stream) => {
if (stream.groupTitle === 'International') return 'ZZ'
if (stream.groupTitle === 'Undefined') return 'ZZZ'
return stream.groupTitle
})
const playlist = new Playlist(groupedStreams, { public: true })
const filepath = 'index.country.m3u'
await this.storage.save(filepath, playlist.toString())
3 months ago
this.logFile.append(
JSON.stringify({ type: 'index', filepath, count: playlist.streams.count() }) + EOL
)
}
}