Files
iptv/scripts/commands/playlist/format.ts

77 lines
2.3 KiB
TypeScript
Raw Normal View History

2025-04-16 20:54:55 +03:00
import { Logger, Storage } from '@freearhey/core'
2023-09-17 04:08:50 +03:00
import { STREAMS_DIR, DATA_DIR } from '../../constants'
2025-04-16 20:54:55 +03:00
import { DataLoader, DataProcessor, PlaylistParser } from '../../core'
import { Stream, Playlist } from '../../models'
2023-09-17 04:08:50 +03:00
import { program } from 'commander'
2025-04-16 20:54:55 +03:00
import { DataLoaderData } from '../../types/dataLoader'
import { DataProcessorData } from '../../types/dataProcessor'
2023-09-17 04:08:50 +03:00
program.argument('[filepath]', 'Path to file to validate').parse(process.argv)
async function main() {
const logger = new Logger()
2025-03-29 11:39:46 +03:00
logger.info('loading data from api...')
2025-04-16 20:54:55 +03:00
const processor = new DataProcessor()
2023-09-17 04:08:50 +03:00
const dataStorage = new Storage(DATA_DIR)
2025-04-16 20:54:55 +03:00
const loader = new DataLoader({ storage: dataStorage })
const data: DataLoaderData = await loader.load()
2025-07-10 21:13:43 +03:00
const { channelsKeyById, feedsGroupedByChannelId, logosGroupedByStreamId }: DataProcessorData =
processor.process(data)
2023-09-17 04:08:50 +03:00
logger.info('loading streams...')
2025-04-16 20:54:55 +03:00
const streamsStorage = new Storage(STREAMS_DIR)
2025-03-29 11:39:46 +03:00
const parser = new PlaylistParser({
storage: streamsStorage,
2025-04-16 20:54:55 +03:00
channelsKeyById,
2025-07-10 21:13:43 +03:00
feedsGroupedByChannelId,
logosGroupedByStreamId
2025-03-29 11:39:46 +03:00
})
const files = program.args.length ? program.args : await streamsStorage.list('**/*.m3u')
2023-09-17 04:08:50 +03:00
let streams = await parser.parse(files)
logger.info(`found ${streams.count()} streams`)
logger.info('normalizing links...')
streams = streams.map(stream => {
stream.normalizeURL()
return stream
})
logger.info('removing duplicates...')
streams = streams.uniqBy(stream => stream.url)
logger.info('removing wrong id...')
streams = streams.map((stream: Stream) => {
2025-04-16 20:54:55 +03:00
if (!stream.channel || channelsKeyById.missing(stream.channel.id)) {
2025-03-29 11:39:46 +03:00
stream.id = ''
2023-09-17 04:08:50 +03:00
}
return stream
})
logger.info('sorting links...')
streams = streams.orderBy(
[
(stream: Stream) => stream.name,
2025-03-30 03:01:05 +03:00
(stream: Stream) => stream.getVerticalResolution(),
2025-03-29 11:39:46 +03:00
(stream: Stream) => stream.getLabel(),
2023-09-17 04:08:50 +03:00
(stream: Stream) => stream.url
],
['asc', 'desc', 'asc', 'asc']
)
logger.info('saving...')
2025-03-29 11:39:46 +03:00
const groupedStreams = streams.groupBy((stream: Stream) => stream.getFilepath())
2023-09-17 04:08:50 +03:00
for (let filepath of groupedStreams.keys()) {
const streams = groupedStreams.get(filepath) || []
if (!streams.length) return
const playlist = new Playlist(streams, { public: false })
2025-03-29 11:39:46 +03:00
await streamsStorage.save(filepath, playlist.toString())
2023-09-17 04:08:50 +03:00
}
}
main()