MCPcopy
hub / github.com/iptv-org/iptv / CountriesGenerator

Class CountriesGenerator

scripts/generators/countriesGenerator.ts:14–80  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12}
13
14export class CountriesGenerator implements Generator {
15 streams: Collection<Stream>
16 countries: Collection<sdk.Models.Country>
17 storage: Storage
18 logFile: File
19
20 constructor({ streams, countries, logFile }: CountriesGeneratorProps) {
21 this.streams = streams.clone()
22 this.countries = countries
23 this.storage = new Storage(PUBLIC_DIR)
24 this.logFile = logFile
25 }
26
27 async generate(): Promise<void> {
28 const streams = this.streams
29 .sortBy((stream: Stream) => stream.title)
30 .filter((stream: Stream) => stream.isSFW())
31
32 const streamsGroupedByCountryCode = {}
33 streams.forEach((stream: Stream) => {
34 stream.getBroadcastCountries().forEach((country: sdk.Models.Country) => {
35 if (streamsGroupedByCountryCode[country.code]) {
36 streamsGroupedByCountryCode[country.code].add(stream)
37 } else {
38 streamsGroupedByCountryCode[country.code] = new Collection<Stream>([stream])
39 }
40 })
41 })
42
43 for (const countryCode in streamsGroupedByCountryCode) {
44 const countryStreams = streamsGroupedByCountryCode[countryCode]
45
46 const playlist = new Playlist(countryStreams, { public: true })
47 const filepath = `countries/${countryCode.toLowerCase()}.m3u`
48 await this.storage.save(filepath, playlist.toString())
49 this.logFile.append(
50 JSON.stringify({ type: 'country', filepath, count: playlist.streams.count() }) + EOL
51 )
52 }
53
54 const internationalStreams = streams.filter((stream: Stream) => stream.isInternational())
55 const internationalPlaylist = new Playlist(internationalStreams, { public: true })
56 const internationalFilepath = 'countries/int.m3u'
57 await this.storage.save(internationalFilepath, internationalPlaylist.toString())
58 this.logFile.append(
59 JSON.stringify({
60 type: 'country',
61 filepath: internationalFilepath,
62 count: internationalPlaylist.streams.count()
63 }) + EOL
64 )
65
66 const undefinedStreams = streams.filter((stream: Stream) =>
67 stream.getBroadcastAreaCodes().isEmpty()
68 )
69 const undefinedPlaylist = new Playlist(undefinedStreams, { public: true })
70 const undefinedFilepath = 'countries/undefined.m3u'
71 await this.storage.save(undefinedFilepath, undefinedPlaylist.toString())

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected