Files
epg/scripts/commands/channels/parse.ts

89 lines
2.4 KiB
TypeScript
Raw Normal View History

2025-07-18 22:51:01 +03:00
import { Logger, File, Storage } from '@freearhey/core'
import { ChannelsParser } from '../../core'
import { ChannelList } from '../../models'
2025-01-05 19:33:01 +03:00
import { pathToFileURL } from 'node:url'
2025-07-18 22:51:01 +03:00
import epgGrabber from 'epg-grabber'
import { Command } from 'commander'
2023-10-15 14:08:23 +03:00
const program = new Command()
program
.requiredOption('-c, --config <config>', 'Config file')
.option('-s, --set [args...]', 'Set custom arguments')
.option('-o, --output <output>', 'Output file')
.parse(process.argv)
type ParseOptions = {
config: string
set?: string
output?: string
clean?: boolean
}
const options: ParseOptions = program.opts()
async function main() {
2025-07-18 22:51:01 +03:00
function isPromise(promise: object[] | Promise<object[]>) {
return (
!!promise &&
typeof promise === 'object' &&
typeof (promise as Promise<object[]>).then === 'function'
)
}
2023-10-15 14:08:23 +03:00
const storage = new Storage()
const logger = new Logger()
2025-07-18 22:51:01 +03:00
const parser = new ChannelsParser({ storage })
2023-10-15 14:08:23 +03:00
const file = new File(options.config)
const dir = file.dirname()
2025-01-24 19:20:01 +03:00
const config = (await import(pathToFileURL(options.config).toString())).default
2023-10-15 14:08:23 +03:00
const outputFilepath = options.output || `${dir}/${config.site}.channels.xml`
2025-07-18 22:51:01 +03:00
let channelList = new ChannelList({ channels: [] })
2023-11-17 17:48:28 +03:00
if (await storage.exists(outputFilepath)) {
2025-07-18 22:51:01 +03:00
channelList = await parser.parse(outputFilepath)
2023-10-15 14:08:23 +03:00
}
const args: {
[key: string]: string
} = {}
if (Array.isArray(options.set)) {
options.set.forEach((arg: string) => {
const [key, value] = arg.split(':')
args[key] = value
})
}
let parsedChannels = config.channels(args)
if (isPromise(parsedChannels)) {
parsedChannels = await parsedChannels
}
2025-07-18 22:51:01 +03:00
parsedChannels = parsedChannels.map((channel: epgGrabber.Channel) => {
2023-10-15 14:08:23 +03:00
channel.site = config.site
return channel
})
2025-07-18 22:51:01 +03:00
const newChannelList = new ChannelList({ channels: [] })
parsedChannels.forEach((channel: epgGrabber.Channel) => {
if (!channel.site_id) return
const found: epgGrabber.Channel | undefined = channelList.get(channel.site_id)
2023-10-15 14:08:23 +03:00
2023-11-17 17:48:28 +03:00
if (found) {
channel.xmltv_id = found.xmltv_id
2023-11-18 13:55:41 +03:00
channel.lang = found.lang
2023-11-17 17:48:28 +03:00
}
2025-07-18 22:51:01 +03:00
newChannelList.add(channel)
2023-11-17 17:48:28 +03:00
})
2025-07-18 22:51:01 +03:00
newChannelList.sort()
2023-11-17 17:48:28 +03:00
2025-07-18 22:51:01 +03:00
await storage.save(outputFilepath, newChannelList.toString())
2023-10-15 14:08:23 +03:00
logger.info(`File '${outputFilepath}' successfully saved`)
}
main()