Files
iptv/scripts/generators/countriesGenerator.ts

58 lines
1.9 KiB
TypeScript
Raw Normal View History

2025-03-29 11:39:46 +03:00
import { Country, Subdivision, Stream, Playlist } from '../models'
2025-04-23 20:56:19 +03:00
import { Collection, Storage, File } from '@freearhey/core'
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 CountriesGeneratorProps = {
streams: Collection
countries: Collection
2025-04-23 20:56:19 +03:00
logFile: File
2023-09-22 06:22:47 +03:00
}
export class CountriesGenerator implements Generator {
streams: Collection
countries: 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, countries, logFile }: CountriesGeneratorProps) {
2025-07-10 21:13:43 +03:00
this.streams = streams.clone()
2023-09-22 06:22:47 +03:00
this.countries = countries
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())
2023-09-22 06:22:47 +03:00
.filter((stream: Stream) => stream.isSFW())
this.countries.forEach(async (country: Country) => {
2025-03-29 11:39:46 +03:00
const countryStreams = streams.filter((stream: Stream) =>
stream.isBroadcastInCountry(country)
2023-09-22 06:22:47 +03:00
)
if (countryStreams.isEmpty()) return
const playlist = new Playlist(countryStreams, { public: true })
const filepath = `countries/${country.code.toLowerCase()}.m3u`
await this.storage.save(filepath, playlist.toString())
2025-04-23 20:56:19 +03:00
this.logFile.append(
JSON.stringify({ type: 'country', 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 undefinedStreams = streams.filter((stream: Stream) => !stream.hasBroadcastArea())
const undefinedPlaylist = new Playlist(undefinedStreams, { public: true })
const undefinedFilepath = 'countries/undefined.m3u'
await this.storage.save(undefinedFilepath, undefinedPlaylist.toString())
2025-04-23 20:56:19 +03:00
this.logFile.append(
2025-03-29 11:39:46 +03:00
JSON.stringify({
type: 'country',
filepath: undefinedFilepath,
count: undefinedPlaylist.streams.count()
2025-04-23 20:56:19 +03:00
}) + EOL
2025-03-29 11:39:46 +03:00
)
2023-09-22 06:22:47 +03:00
}
}