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

124 lines
3.4 KiB
TypeScript
Raw Normal View History

2023-10-15 14:08:23 +03:00
import { DATA_DIR } from '../../constants'
2025-01-12 23:10:50 +03:00
import { Storage, Collection, Logger } from '@freearhey/core'
2023-10-15 14:08:23 +03:00
import { ChannelsParser, XML, ApiChannel } from '../../core'
import { Channel } from 'epg-grabber'
import nodeCleanup from 'node-cleanup'
import { program } from 'commander'
import inquirer, { QuestionCollection } from 'inquirer'
2025-01-13 02:29:58 +03:00
import Fuse from 'fuse.js'
2025-01-16 21:32:50 +03:00
import readline from 'readline'
if (process.platform === 'win32') {
readline
.createInterface({
input: process.stdin,
output: process.stdout
})
.on('SIGINT', function () {
process.emit('SIGINT')
})
}
2023-10-15 14:08:23 +03:00
2025-01-12 23:10:50 +03:00
program.argument('<filepath>', 'Path to *.channels.xml file to edit').parse(process.argv)
2023-10-15 14:08:23 +03:00
const filepath = program.args[0]
2025-01-12 23:10:50 +03:00
const logger = new Logger()
const storage = new Storage()
let channels = new Collection()
2023-10-15 14:08:23 +03:00
async function main() {
if (!(await storage.exists(filepath))) {
throw new Error(`File "${filepath}" does not exists`)
}
const parser = new ChannelsParser({ storage })
2025-01-12 23:10:50 +03:00
channels = await parser.parse(filepath)
2023-10-15 14:08:23 +03:00
const dataStorage = new Storage(DATA_DIR)
const channelsContent = await dataStorage.json('channels.json')
2025-01-13 02:29:58 +03:00
const searchIndex = new Fuse(channelsContent, { keys: ['name', 'alt_names'], threshold: 0.4 })
2023-10-15 14:08:23 +03:00
2025-01-12 23:10:50 +03:00
for (const channel of channels.all()) {
if (channel.xmltv_id) continue
2023-10-15 14:08:23 +03:00
const question: QuestionCollection = {
name: 'option',
2025-01-13 02:29:58 +03:00
message: `Select xmltv_id for "${channel.name}" (${channel.site_id}):`,
2023-10-15 14:08:23 +03:00
type: 'list',
2025-01-13 02:29:58 +03:00
choices: getOptions(searchIndex, channel),
2023-10-15 14:08:23 +03:00
pageSize: 10
}
await inquirer.prompt(question).then(async selected => {
switch (selected.option) {
2025-01-12 23:10:50 +03:00
case 'Type...':
2023-10-15 14:08:23 +03:00
const input = await getInput(channel)
channel.xmltv_id = input.xmltv_id
break
case 'Skip':
channel.xmltv_id = '-'
break
default:
const [, xmltv_id] = selected.option
.replace(/ \[.*\]/, '')
.split('|')
2025-01-12 23:10:50 +03:00
.map((i: string) => i.trim())
2023-10-15 14:08:23 +03:00
channel.xmltv_id = xmltv_id
break
}
})
2023-11-17 16:41:14 +03:00
}
2025-01-12 23:10:50 +03:00
channels.forEach((channel: Channel) => {
if (channel.xmltv_id === '-') {
channel.xmltv_id = ''
}
})
2023-10-15 14:08:23 +03:00
}
main()
function save() {
if (!storage.existsSync(filepath)) return
const xml = new XML(channels)
storage.saveSync(filepath, xml.toString())
logger.info(`\nFile '${filepath}' successfully saved`)
}
nodeCleanup(() => {
save()
})
async function getInput(channel: Channel) {
const name = channel.name.trim()
const input = await inquirer.prompt([
{
name: 'xmltv_id',
2025-01-12 23:10:50 +03:00
message: ' xmltv_id:',
type: 'input'
2023-10-15 14:08:23 +03:00
}
])
return { name, xmltv_id: input['xmltv_id'] }
}
2025-01-12 23:10:50 +03:00
function getOptions(index, channel: Channel) {
2025-01-13 02:29:58 +03:00
const similar = index.search(channel.name).map(result => new ApiChannel(result.item))
2023-10-15 14:08:23 +03:00
const variants = new Collection()
similar.forEach((_channel: ApiChannel) => {
const altNames = _channel.altNames.notEmpty() ? ` (${_channel.altNames.join(',')})` : ''
2023-11-19 10:27:12 +03:00
const closed = _channel.closed ? ` [closed:${_channel.closed}]` : ''
2023-10-15 14:08:23 +03:00
const replacedBy = _channel.replacedBy ? `[replaced_by:${_channel.replacedBy}]` : ''
2023-11-17 16:41:14 +03:00
variants.add(`${_channel.name}${altNames} | ${_channel.id}${closed}${replacedBy}`)
2023-10-15 14:08:23 +03:00
})
2025-01-12 23:10:50 +03:00
variants.add('Type...')
2023-10-15 14:08:23 +03:00
variants.add('Skip')
return variants.all()
}