Fix line endings char

This commit is contained in:
freearhey
2026-04-11 23:45:57 +03:00
parent 0c43b864f9
commit 2d547ae74b

View File

@@ -1,194 +1,194 @@
import { loadData, data, searchChannels } from '../../api' import { loadData, data, searchChannels } from '../../api'
import { Collection, Logger } from '@freearhey/core' import { Collection, Logger } from '@freearhey/core'
import { select, input } from '@inquirer/prompts' import { select, input } from '@inquirer/prompts'
import { Playlist, Stream } from '../../models' import { Playlist, Stream } from '../../models'
import { Storage } from '@freearhey/storage-js' import { Storage } from '@freearhey/storage-js'
import { PlaylistParser } from '../../core' import { PlaylistParser } from '../../core'
import nodeCleanup from 'node-cleanup' import nodeCleanup from 'node-cleanup'
import * as sdk from '@iptv-org/sdk' import * as sdk from '@iptv-org/sdk'
import { truncate } from '../../utils' import { truncate } from '../../utils'
import { Command } from 'commander' import { Command } from 'commander'
import readline from 'readline' import readline from 'readline'
import path from 'path' import path from 'path'
type ChoiceValue = { type: string; value?: sdk.Models.Feed | sdk.Models.Channel } type ChoiceValue = { type: string; value?: sdk.Models.Feed | sdk.Models.Channel }
type Choice = { name: string; short?: string; value: ChoiceValue; default?: boolean } type Choice = { name: string; short?: string; value: ChoiceValue; default?: boolean }
if (process.platform === 'win32') { if (process.platform === 'win32') {
readline readline
.createInterface({ .createInterface({
input: process.stdin, input: process.stdin,
output: process.stdout output: process.stdout
}) })
.on('SIGINT', function () { .on('SIGINT', function () {
process.emit('SIGINT') process.emit('SIGINT')
}) })
} }
const program = new Command() const program = new Command()
program.argument('<filepath>', 'Path to *.channels.xml file to edit').parse(process.argv) program.argument('<filepath>', 'Path to *.channels.xml file to edit').parse(process.argv)
const filepath = program.args[0] const filepath = program.args[0]
const logger = new Logger() const logger = new Logger()
const resolvedPath = path.resolve(filepath) const resolvedPath = path.resolve(filepath)
const relative = path.relative(process.cwd(), resolvedPath) const relative = path.relative(process.cwd(), resolvedPath)
if (relative.startsWith('..') || path.isAbsolute(relative)) { if (relative.startsWith('..') || path.isAbsolute(relative)) {
console.error(`Error: filepath "${filepath}" is outside the working directory`) console.error(`Error: filepath "${filepath}" is outside the working directory`)
process.exit(1) process.exit(1)
} }
const storage = new Storage() const storage = new Storage()
let parsedStreams = new Collection<Stream>() let parsedStreams = new Collection<Stream>()
main(filepath) main(filepath)
nodeCleanup(() => { nodeCleanup(() => {
save(filepath) save(filepath)
}) })
export default async function main(filepath: string) { export default async function main(filepath: string) {
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`)
} }
logger.info('loading data from api...') logger.info('loading data from api...')
await loadData() await loadData()
logger.info('loading streams...') logger.info('loading streams...')
const parser = new PlaylistParser({ const parser = new PlaylistParser({
storage storage
}) })
parsedStreams = await parser.parseFile(filepath) parsedStreams = await parser.parseFile(filepath)
const streamsWithoutId = parsedStreams.filter((stream: Stream) => !stream.tvgId) const streamsWithoutId = parsedStreams.filter((stream: Stream) => !stream.tvgId)
logger.info( logger.info(
`found ${parsedStreams.count()} streams (including ${streamsWithoutId.count()} without ID)` `found ${parsedStreams.count()} streams (including ${streamsWithoutId.count()} without ID)`
) )
logger.info('starting...\n') logger.info('starting...\n')
for (const stream of streamsWithoutId.all()) { for (const stream of streamsWithoutId.all()) {
try { try {
stream.tvgId = await selectChannel(stream) stream.tvgId = await selectChannel(stream)
} catch (err) { } catch (err) {
logger.info(err.message) logger.info(err.message)
break break
} }
} }
streamsWithoutId.forEach((stream: Stream) => { streamsWithoutId.forEach((stream: Stream) => {
if (stream.tvgId === '-') { if (stream.tvgId === '-') {
stream.tvgId = '' stream.tvgId = ''
} }
}) })
} }
async function selectChannel(stream: Stream): Promise<string> { async function selectChannel(stream: Stream): Promise<string> {
const similarChannels = searchChannels(stream.title) const similarChannels = searchChannels(stream.title)
const url = truncate(stream.url, 50) const url = truncate(stream.url, 50)
const selected: ChoiceValue = await select({ const selected: ChoiceValue = await select({
message: `Select channel ID for "${stream.title}" (${url}):`, message: `Select channel ID for "${stream.title}" (${url}):`,
choices: getChannelChoises(similarChannels), choices: getChannelChoises(similarChannels),
pageSize: 10 pageSize: 10
}) })
switch (selected.type) { switch (selected.type) {
case 'skip': case 'skip':
return '-' return '-'
case 'type': { case 'type': {
const typedChannelId = await input({ message: ' Channel ID:' }) const typedChannelId = await input({ message: ' Channel ID:' })
if (!typedChannelId) return '' if (!typedChannelId) return ''
const selectedFeedId = await selectFeed(typedChannelId) const selectedFeedId = await selectFeed(typedChannelId)
if (selectedFeedId === '-') return typedChannelId if (selectedFeedId === '-') return typedChannelId
return [typedChannelId, selectedFeedId].join('@') return [typedChannelId, selectedFeedId].join('@')
} }
case 'channel': { case 'channel': {
const selectedChannel = selected.value const selectedChannel = selected.value
if (!selectedChannel) return '' if (!selectedChannel) return ''
const selectedFeedId = await selectFeed(selectedChannel.id) const selectedFeedId = await selectFeed(selectedChannel.id)
if (selectedFeedId === '-') return selectedChannel.id if (selectedFeedId === '-') return selectedChannel.id
return [selectedChannel.id, selectedFeedId].join('@') return [selectedChannel.id, selectedFeedId].join('@')
} }
} }
return '' return ''
} }
async function selectFeed(channelId: string): Promise<string> { async function selectFeed(channelId: string): Promise<string> {
const channelFeeds = new Collection(data.feedsGroupedByChannel.get(channelId)) const channelFeeds = new Collection(data.feedsGroupedByChannel.get(channelId))
const choices = getFeedChoises(channelFeeds) const choices = getFeedChoises(channelFeeds)
const selected: ChoiceValue = await select({ const selected: ChoiceValue = await select({
message: `Select feed ID for "${channelId}":`, message: `Select feed ID for "${channelId}":`,
choices, choices,
pageSize: 10 pageSize: 10
}) })
switch (selected.type) { switch (selected.type) {
case 'skip': case 'skip':
return '-' return '-'
case 'type': case 'type':
return await input({ message: ' Feed ID:', default: 'SD' }) return await input({ message: ' Feed ID:', default: 'SD' })
case 'feed': case 'feed':
const selectedFeed = selected.value const selectedFeed = selected.value
if (!selectedFeed) return '' if (!selectedFeed) return ''
return selectedFeed.id return selectedFeed.id
} }
return '' return ''
} }
function getChannelChoises(channels: Collection<sdk.Models.Channel>): Choice[] { function getChannelChoises(channels: Collection<sdk.Models.Channel>): Choice[] {
const choises: Choice[] = [] const choises: Choice[] = []
channels.forEach((channel: sdk.Models.Channel) => { channels.forEach((channel: sdk.Models.Channel) => {
const names = new Collection([channel.name, ...channel.alt_names]).uniq().join(', ') const names = new Collection([channel.name, ...channel.alt_names]).uniq().join(', ')
choises.push({ choises.push({
value: { value: {
type: 'channel', type: 'channel',
value: channel value: channel
}, },
name: `${channel.id} (${names})`, name: `${channel.id} (${names})`,
short: `${channel.id}` short: `${channel.id}`
}) })
}) })
choises.push({ name: 'Type...', value: { type: 'type' } }) choises.push({ name: 'Type...', value: { type: 'type' } })
choises.push({ name: 'Skip', value: { type: 'skip' } }) choises.push({ name: 'Skip', value: { type: 'skip' } })
return choises return choises
} }
function getFeedChoises(feeds: Collection<sdk.Models.Feed>): Choice[] { function getFeedChoises(feeds: Collection<sdk.Models.Feed>): Choice[] {
const choises: Choice[] = [] const choises: Choice[] = []
feeds.forEach((feed: sdk.Models.Feed) => { feeds.forEach((feed: sdk.Models.Feed) => {
let name = `${feed.id} (${feed.name})` let name = `${feed.id} (${feed.name})`
if (feed.is_main) name += ' [main]' if (feed.is_main) name += ' [main]'
choises.push({ choises.push({
value: { value: {
type: 'feed', type: 'feed',
value: feed value: feed
}, },
default: feed.is_main, default: feed.is_main,
name, name,
short: feed.id short: feed.id
}) })
}) })
choises.push({ name: 'Type...', value: { type: 'type' } }) choises.push({ name: 'Type...', value: { type: 'type' } })
choises.push({ name: 'Skip', value: { type: 'skip' } }) choises.push({ name: 'Skip', value: { type: 'skip' } })
return choises return choises
} }
function save(filepath: string) { function save(filepath: string) {
if (!storage.existsSync(filepath)) return if (!storage.existsSync(filepath)) return
const playlist = new Playlist(parsedStreams) const playlist = new Playlist(parsedStreams)
storage.saveSync(filepath, playlist.toString()) storage.saveSync(filepath, playlist.toString())
logger.info(`\nFile '${filepath}' successfully saved`) logger.info(`\nFile '${filepath}' successfully saved`)
} }