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

217 lines
6.5 KiB
TypeScript
Raw Normal View History

2025-04-02 07:13:39 +03:00
import { Storage, Collection, Logger, Dictionary } from '@freearhey/core'
2025-07-18 22:51:01 +03:00
import type { DataProcessorData } from '../../types/dataProcessor'
import type { DataLoaderData } from '../../types/dataLoader'
import { ChannelSearchableData } from '../../types/channel'
import { Channel, ChannelList, Feed } from '../../models'
import { DataProcessor, DataLoader } from '../../core'
2025-04-02 07:13:39 +03:00
import { select, input } from '@inquirer/prompts'
2025-07-18 22:51:01 +03:00
import { ChannelsParser } from '../../core'
2023-10-15 14:08:23 +03:00
import { DATA_DIR } from '../../constants'
import nodeCleanup from 'node-cleanup'
2025-04-19 02:06:15 +03:00
import sjs from '@freearhey/search-js'
import epgGrabber from 'epg-grabber'
2025-07-18 22:51:01 +03:00
import { Command } from 'commander'
import readline from 'readline'
2025-04-02 07:13:39 +03:00
type ChoiceValue = { type: string; value?: Feed | Channel }
2025-04-19 02:06:15 +03:00
type Choice = { name: string; short?: string; value: ChoiceValue; default?: boolean }
2025-01-16 21:32:50 +03:00
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-04-02 07:13:39 +03:00
const program = new Command()
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()
2025-07-18 22:51:01 +03:00
let channelList = new ChannelList({ channels: [] })
2023-10-15 14:08:23 +03:00
2025-04-02 07:13:39 +03:00
main(filepath)
nodeCleanup(() => {
2025-07-18 22:51:01 +03:00
save(filepath, channelList)
2025-04-02 07:13:39 +03:00
})
export default async function main(filepath: string) {
2023-10-15 14:08:23 +03:00
if (!(await storage.exists(filepath))) {
throw new Error(`File "${filepath}" does not exists`)
}
2025-04-19 02:06:15 +03:00
logger.info('loading data from api...')
const processor = new DataProcessor()
const dataStorage = new Storage(DATA_DIR)
const loader = new DataLoader({ storage: dataStorage })
const data: DataLoaderData = await loader.load()
2025-07-18 22:51:01 +03:00
const { channels, channelsKeyById, feedsGroupedByChannelId }: DataProcessorData =
2025-04-19 02:06:15 +03:00
processor.process(data)
logger.info('loading channels...')
2023-10-15 14:08:23 +03:00
const parser = new ChannelsParser({ storage })
2025-07-18 22:51:01 +03:00
channelList = await parser.parse(filepath)
const parsedChannelsWithoutId = channelList.channels.filter(
2025-04-19 02:06:15 +03:00
(channel: epgGrabber.Channel) => !channel.xmltv_id
)
logger.info(
2025-07-18 22:51:01 +03:00
`found ${channelList.channels.count()} channels (including ${parsedChannelsWithoutId.count()} without ID)`
2025-04-19 02:06:15 +03:00
)
logger.info('creating search index...')
const items = channels.map((channel: Channel) => channel.getSearchable()).all()
const searchIndex = sjs.createIndex(items, {
searchable: ['name', 'altNames', 'guideNames', 'streamNames', 'feedFullNames']
2025-04-02 07:13:39 +03:00
})
2023-10-15 14:08:23 +03:00
2025-04-19 02:06:15 +03:00
logger.info('starting...\n')
2025-07-18 22:51:01 +03:00
for (const channel of parsedChannelsWithoutId.all()) {
2025-04-02 07:13:39 +03:00
try {
2025-07-18 22:51:01 +03:00
channel.xmltv_id = await selectChannel(
channel,
2025-04-19 02:06:15 +03:00
searchIndex,
feedsGroupedByChannelId,
channelsKeyById
)
} catch (err) {
logger.info(err.message)
2025-04-02 07:13:39 +03:00
break
2023-10-15 14:08:23 +03:00
}
2023-11-17 16:41:14 +03:00
}
2025-01-12 23:10:50 +03:00
2025-04-19 02:06:15 +03:00
parsedChannelsWithoutId.forEach((channel: epgGrabber.Channel) => {
2025-01-12 23:10:50 +03:00
if (channel.xmltv_id === '-') {
channel.xmltv_id = ''
}
})
2023-10-15 14:08:23 +03:00
}
2025-04-02 07:13:39 +03:00
async function selectChannel(
channel: epgGrabber.Channel,
2025-04-19 02:06:15 +03:00
searchIndex,
feedsGroupedByChannelId: Dictionary,
channelsKeyById: Dictionary
2025-04-02 07:13:39 +03:00
): Promise<string> {
2025-04-19 02:06:15 +03:00
const query = escapeRegex(channel.name)
2025-04-02 07:13:39 +03:00
const similarChannels = searchIndex
2025-04-19 02:06:15 +03:00
.search(query)
.map((item: ChannelSearchableData) => channelsKeyById.get(item.id))
2025-04-02 07:13:39 +03:00
const selected: ChoiceValue = await select({
message: `Select channel ID for "${channel.name}" (${channel.site_id}):`,
choices: getChannelChoises(new Collection(similarChannels)),
pageSize: 10
})
2023-10-15 14:08:23 +03:00
2025-04-02 07:13:39 +03:00
switch (selected.type) {
case 'skip':
return '-'
case 'type': {
const typedChannelId = await input({ message: ' Channel ID:' })
2025-04-19 02:06:15 +03:00
if (!typedChannelId) return ''
const selectedFeedId = await selectFeed(typedChannelId, feedsGroupedByChannelId)
if (selectedFeedId === '-') return typedChannelId
return [typedChannelId, selectedFeedId].join('@')
2025-04-02 07:13:39 +03:00
}
case 'channel': {
const selectedChannel = selected.value
if (!selectedChannel) return ''
2025-07-18 22:51:01 +03:00
const selectedFeedId = await selectFeed(selectedChannel.id || '', feedsGroupedByChannelId)
if (selectedFeedId === '-') return selectedChannel.id || ''
2025-04-02 07:13:39 +03:00
return [selectedChannel.id, selectedFeedId].join('@')
}
}
2023-10-15 14:08:23 +03:00
2025-04-02 07:13:39 +03:00
return ''
}
2023-10-15 14:08:23 +03:00
2025-04-02 07:13:39 +03:00
async function selectFeed(channelId: string, feedsGroupedByChannelId: Dictionary): Promise<string> {
2025-04-19 02:06:15 +03:00
const channelFeeds = feedsGroupedByChannelId.has(channelId)
? new Collection(feedsGroupedByChannelId.get(channelId))
: new Collection()
const choices = getFeedChoises(channelFeeds)
2023-10-15 14:08:23 +03:00
2025-04-02 07:13:39 +03:00
const selected: ChoiceValue = await select({
message: `Select feed ID for "${channelId}":`,
2025-04-19 02:06:15 +03:00
choices,
2025-04-02 07:13:39 +03:00
pageSize: 10
})
switch (selected.type) {
2025-04-19 02:06:15 +03:00
case 'skip':
return '-'
2025-04-02 07:13:39 +03:00
case 'type':
2025-04-19 02:06:15 +03:00
return await input({ message: ' Feed ID:', default: 'SD' })
2025-04-02 07:13:39 +03:00
case 'feed':
const selectedFeed = selected.value
if (!selectedFeed) return ''
2025-07-18 22:51:01 +03:00
return selectedFeed.id || ''
2025-04-02 07:13:39 +03:00
}
return ''
2023-10-15 14:08:23 +03:00
}
2025-04-02 07:13:39 +03:00
function getChannelChoises(channels: Collection): Choice[] {
const choises: Choice[] = []
2023-10-15 14:08:23 +03:00
2025-04-02 07:13:39 +03:00
channels.forEach((channel: Channel) => {
2025-04-19 02:06:15 +03:00
const names = new Collection([channel.name, ...channel.getAltNames().all()]).uniq().join(', ')
2025-04-02 07:13:39 +03:00
choises.push({
value: {
type: 'channel',
value: channel
},
name: `${channel.id} (${names})`,
short: `${channel.id}`
})
})
choises.push({ name: 'Type...', value: { type: 'type' } })
choises.push({ name: 'Skip', value: { type: 'skip' } })
2023-10-15 14:08:23 +03:00
2025-04-02 07:13:39 +03:00
return choises
2023-10-15 14:08:23 +03:00
}
2025-04-02 07:13:39 +03:00
function getFeedChoises(feeds: Collection): Choice[] {
const choises: Choice[] = []
2023-10-15 14:08:23 +03:00
2025-04-02 07:13:39 +03:00
feeds.forEach((feed: Feed) => {
let name = `${feed.id} (${feed.name})`
if (feed.isMain) name += ' [main]'
2023-10-15 14:08:23 +03:00
2025-04-02 07:13:39 +03:00
choises.push({
value: {
type: 'feed',
value: feed
},
2025-04-19 02:06:15 +03:00
default: feed.isMain,
2025-04-02 07:13:39 +03:00
name,
short: feed.id
})
2023-10-15 14:08:23 +03:00
})
2025-04-02 07:13:39 +03:00
choises.push({ name: 'Type...', value: { type: 'type' } })
2025-04-19 02:06:15 +03:00
choises.push({ name: 'Skip', value: { type: 'skip' } })
2025-04-02 07:13:39 +03:00
return choises
}
2025-07-18 22:51:01 +03:00
function save(filepath: string, channelList: ChannelList) {
2025-04-02 07:13:39 +03:00
if (!storage.existsSync(filepath)) return
2025-07-18 22:51:01 +03:00
storage.saveSync(filepath, channelList.toString())
2025-04-02 07:13:39 +03:00
logger.info(`\nFile '${filepath}' successfully saved`)
2023-10-15 14:08:23 +03:00
}
2025-04-19 02:06:15 +03:00
function escapeRegex(string: string) {
return string.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&')
}