mirror of
https://github.com/iptv-org/epg.git
synced 2025-05-09 08:30:06 -04:00
Update edit.ts
This commit is contained in:
parent
bc4b5bd187
commit
2e64b51b79
1 changed files with 30 additions and 89 deletions
|
@ -1,68 +1,45 @@
|
||||||
import { DATA_DIR } from '../../constants'
|
import { DATA_DIR } from '../../constants'
|
||||||
import { Storage, Collection, Dictionary, Logger } from '@freearhey/core'
|
import { Storage, Collection, Logger } from '@freearhey/core'
|
||||||
import { ChannelsParser, XML, ApiChannel } from '../../core'
|
import { ChannelsParser, XML, ApiChannel } from '../../core'
|
||||||
import { Channel } from 'epg-grabber'
|
import { Channel } from 'epg-grabber'
|
||||||
import { transliterate } from 'transliteration'
|
|
||||||
import nodeCleanup from 'node-cleanup'
|
import nodeCleanup from 'node-cleanup'
|
||||||
import { program } from 'commander'
|
import { program } from 'commander'
|
||||||
import inquirer, { QuestionCollection } from 'inquirer'
|
import inquirer, { QuestionCollection } from 'inquirer'
|
||||||
import sj from '@freearhey/search-js'
|
import sj from '@freearhey/search-js'
|
||||||
|
|
||||||
program
|
program.argument('<filepath>', 'Path to *.channels.xml file to edit').parse(process.argv)
|
||||||
.argument('<filepath>', 'Path to *.channels.xml file to edit')
|
|
||||||
.option('-c, --country <name>', 'Default country (ISO 3166 code)', 'US')
|
|
||||||
.parse(process.argv)
|
|
||||||
|
|
||||||
const filepath = program.args[0]
|
const filepath = program.args[0]
|
||||||
const programOptions = program.opts()
|
|
||||||
const defaultCountry = programOptions.country.toLowerCase()
|
|
||||||
const newLabel = ' [new]'
|
|
||||||
|
|
||||||
let options = new Collection()
|
const logger = new Logger()
|
||||||
|
const storage = new Storage()
|
||||||
|
let channels = new Collection()
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const storage = new Storage()
|
|
||||||
|
|
||||||
if (!(await storage.exists(filepath))) {
|
if (!(await storage.exists(filepath))) {
|
||||||
throw new Error(`File "${filepath}" does not exists`)
|
throw new Error(`File "${filepath}" does not exists`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const parser = new ChannelsParser({ storage })
|
const parser = new ChannelsParser({ storage })
|
||||||
|
channels = await parser.parse(filepath)
|
||||||
const parsedChannels = await parser.parse(filepath)
|
|
||||||
options = parsedChannels.map((channel: Channel): { channel: Channel; delete: boolean } => {
|
|
||||||
return {
|
|
||||||
channel,
|
|
||||||
delete: false
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
const dataStorage = new Storage(DATA_DIR)
|
const dataStorage = new Storage(DATA_DIR)
|
||||||
const channelsContent = await dataStorage.json('channels.json')
|
const channelsContent = await dataStorage.json('channels.json')
|
||||||
|
|
||||||
const channelsIndex = sj.createIndex(channelsContent)
|
const channelsIndex = sj.createIndex(channelsContent)
|
||||||
|
|
||||||
const buffer = new Dictionary()
|
for (const channel of channels.all()) {
|
||||||
for (const option of options.all()) {
|
if (channel.xmltv_id) continue
|
||||||
const channel: Channel = option.channel
|
|
||||||
if (channel.xmltv_id) {
|
|
||||||
if (channel.xmltv_id !== '-') {
|
|
||||||
buffer.set(`${channel.xmltv_id}/${channel.lang}`, true)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
const choices = getOptions(channelsIndex, channel)
|
|
||||||
const question: QuestionCollection = {
|
const question: QuestionCollection = {
|
||||||
name: 'option',
|
name: 'option',
|
||||||
message: `Choose xmltv_id for "${channel.name}" (${channel.site_id}):`,
|
message: `"${channel.name}" (${channel.site_id}):`,
|
||||||
type: 'list',
|
type: 'list',
|
||||||
choices,
|
choices: getOptions(channelsIndex, channel),
|
||||||
pageSize: 10
|
pageSize: 10
|
||||||
}
|
}
|
||||||
|
|
||||||
await inquirer.prompt(question).then(async selected => {
|
await inquirer.prompt(question).then(async selected => {
|
||||||
switch (selected.option) {
|
switch (selected.option) {
|
||||||
case 'Overwrite':
|
case 'Type...':
|
||||||
const input = await getInput(channel)
|
const input = await getInput(channel)
|
||||||
channel.xmltv_id = input.xmltv_id
|
channel.xmltv_id = input.xmltv_id
|
||||||
break
|
break
|
||||||
|
@ -73,53 +50,25 @@ async function main() {
|
||||||
const [, xmltv_id] = selected.option
|
const [, xmltv_id] = selected.option
|
||||||
.replace(/ \[.*\]/, '')
|
.replace(/ \[.*\]/, '')
|
||||||
.split('|')
|
.split('|')
|
||||||
.map((i: string) => i.trim().replace(newLabel, ''))
|
.map((i: string) => i.trim())
|
||||||
channel.xmltv_id = xmltv_id
|
channel.xmltv_id = xmltv_id
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
const found = buffer.has(`${channel.xmltv_id}/${channel.lang}`)
|
|
||||||
if (found) {
|
|
||||||
const question: QuestionCollection = {
|
|
||||||
name: 'option',
|
|
||||||
message: `"${channel.xmltv_id}" already on the list. Choose an option:`,
|
|
||||||
type: 'list',
|
|
||||||
choices: ['Skip', 'Add', 'Delete'],
|
|
||||||
pageSize: 5
|
|
||||||
}
|
|
||||||
await inquirer.prompt(question).then(async selected => {
|
|
||||||
switch (selected.option) {
|
|
||||||
case 'Skip':
|
|
||||||
channel.xmltv_id = '-'
|
|
||||||
break
|
|
||||||
case 'Delete':
|
|
||||||
option.delete = true
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
break
|
|
||||||
}
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
if (channel.xmltv_id !== '-') {
|
|
||||||
buffer.set(`${channel.xmltv_id}/${channel.lang}`, true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
channels.forEach((channel: Channel) => {
|
||||||
|
if (channel.xmltv_id === '-') {
|
||||||
|
channel.xmltv_id = ''
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
||||||
function save() {
|
function save() {
|
||||||
const logger = new Logger()
|
|
||||||
const storage = new Storage()
|
|
||||||
|
|
||||||
if (!storage.existsSync(filepath)) return
|
if (!storage.existsSync(filepath)) return
|
||||||
|
|
||||||
const channels = options
|
|
||||||
.filter((option: { channel: Channel; delete: boolean }) => !option.delete)
|
|
||||||
.map((option: { channel: Channel; delete: boolean }) => option.channel)
|
|
||||||
|
|
||||||
const xml = new XML(channels)
|
const xml = new XML(channels)
|
||||||
|
|
||||||
storage.saveSync(filepath, xml.toString())
|
storage.saveSync(filepath, xml.toString())
|
||||||
|
@ -136,26 +85,19 @@ async function getInput(channel: Channel) {
|
||||||
const input = await inquirer.prompt([
|
const input = await inquirer.prompt([
|
||||||
{
|
{
|
||||||
name: 'xmltv_id',
|
name: 'xmltv_id',
|
||||||
message: ' ID:',
|
message: ' xmltv_id:',
|
||||||
type: 'input',
|
type: 'input'
|
||||||
default: generateCode(name, defaultCountry)
|
|
||||||
}
|
}
|
||||||
])
|
])
|
||||||
|
|
||||||
return { name, xmltv_id: input['xmltv_id'] }
|
return { name, xmltv_id: input['xmltv_id'] }
|
||||||
}
|
}
|
||||||
|
|
||||||
function getOptions(channelsIndex, channel: Channel) {
|
function getOptions(index, channel: Channel) {
|
||||||
const channelId = generateCode(channel.name, defaultCountry)
|
const query = prepareQuery(channel.name)
|
||||||
const query = channel.name
|
const similar = index.search(query).map(item => new ApiChannel(item))
|
||||||
.replace(/\s(SD|TV|HD|SD\/HD|HDTV)$/i, '')
|
|
||||||
.replace(/(\(|\)|,)/gi, '')
|
|
||||||
.replace(/-/gi, ' ')
|
|
||||||
.replace(/\+/gi, '')
|
|
||||||
const similar = channelsIndex.search(query).map(item => new ApiChannel(item))
|
|
||||||
|
|
||||||
const variants = new Collection()
|
const variants = new Collection()
|
||||||
variants.add(`${channel.name.trim()} | ${channelId}${newLabel}`)
|
|
||||||
similar.forEach((_channel: ApiChannel) => {
|
similar.forEach((_channel: ApiChannel) => {
|
||||||
const altNames = _channel.altNames.notEmpty() ? ` (${_channel.altNames.join(',')})` : ''
|
const altNames = _channel.altNames.notEmpty() ? ` (${_channel.altNames.join(',')})` : ''
|
||||||
const closed = _channel.closed ? ` [closed:${_channel.closed}]` : ''
|
const closed = _channel.closed ? ` [closed:${_channel.closed}]` : ''
|
||||||
|
@ -163,17 +105,16 @@ function getOptions(channelsIndex, channel: Channel) {
|
||||||
|
|
||||||
variants.add(`${_channel.name}${altNames} | ${_channel.id}${closed}${replacedBy}`)
|
variants.add(`${_channel.name}${altNames} | ${_channel.id}${closed}${replacedBy}`)
|
||||||
})
|
})
|
||||||
variants.add('Overwrite')
|
variants.add('Type...')
|
||||||
variants.add('Skip')
|
variants.add('Skip')
|
||||||
|
|
||||||
return variants.all()
|
return variants.all()
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateCode(name: string, country: string) {
|
function prepareQuery(channelName: string) {
|
||||||
const channelId: string = transliterate(name)
|
return channelName
|
||||||
.replace(/\+/gi, 'Plus')
|
.replace(/\s(SD|TV|HD|SD\/HD|HDTV)$/i, '')
|
||||||
.replace(/^&/gi, 'And')
|
.replace(/(\(|\)|,)/gi, '')
|
||||||
.replace(/[^a-z\d]+/gi, '')
|
.replace(/-/gi, ' ')
|
||||||
|
.replace(/\+/gi, '')
|
||||||
return `${channelId}.${country}`
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue