mirror of
https://github.com/iptv-org/epg
synced 2025-12-18 03:16:53 -05:00
Update scripts
This commit is contained in:
60
scripts/commands/channels/format.ts
Normal file
60
scripts/commands/channels/format.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { Collection, Logger } from '@freearhey/core'
|
||||
import epgGrabber, { EPGGrabber } from 'epg-grabber'
|
||||
import { generateChannelsXML } from '../../core'
|
||||
import { Storage } from '@freearhey/storage-js'
|
||||
import { SITES_DIR } from '../../constants'
|
||||
import { data, loadData } from '../../api'
|
||||
import { Channel } from '../../models'
|
||||
import { program } from 'commander'
|
||||
|
||||
program.argument('[filepath...]', 'Path to file to format').parse(process.argv)
|
||||
|
||||
async function main() {
|
||||
const logger = new Logger()
|
||||
|
||||
logger.info('loading data from api...')
|
||||
await loadData()
|
||||
|
||||
logger.info('loading *.channels.xml files...')
|
||||
const storage = new Storage()
|
||||
const files = program.args.length
|
||||
? program.args
|
||||
: await storage.list(`${SITES_DIR}/**/*.channels.xml`)
|
||||
|
||||
logger.info(`found ${files.length} file(s)`)
|
||||
|
||||
logger.info('formating...')
|
||||
for (const filepath of files) {
|
||||
if (!storage.existsSync(filepath)) continue
|
||||
|
||||
const xml = await storage.load(filepath)
|
||||
const parsedChannels = EPGGrabber.parseChannelsXML(xml)
|
||||
const channelsFromXML = new Collection(parsedChannels).map(
|
||||
(channel: epgGrabber.Channel) => new Channel(channel.toObject())
|
||||
)
|
||||
|
||||
channelsFromXML.forEach((channel: Channel) => {
|
||||
if (!channel.xmltv_id) return
|
||||
if (data.feedsKeyByStreamId.get(channel.xmltv_id)) return
|
||||
|
||||
const channelData = data.channelsKeyById.get(channel.xmltv_id)
|
||||
if (channelData) {
|
||||
const mainFeed = channelData.getMainFeed()
|
||||
if (mainFeed) {
|
||||
channel.xmltv_id = mainFeed.getStreamId()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
channel.xmltv_id = ''
|
||||
})
|
||||
|
||||
channelsFromXML.sortBy((channel: Channel) => channel.site_id)
|
||||
|
||||
const output = generateChannelsXML(channelsFromXML)
|
||||
|
||||
await storage.save(filepath, output)
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
||||
Reference in New Issue
Block a user