Update channels:editor

This commit is contained in:
freearhey 2023-11-19 10:46:24 +03:00
parent f3dee3dc0c
commit 6dd2dbbf31
4 changed files with 35 additions and 12 deletions

20
package-lock.json generated
View file

@ -10,6 +10,7 @@
"dependencies": { "dependencies": {
"@alex_neo/jest-expect-message": "^1.0.5", "@alex_neo/jest-expect-message": "^1.0.5",
"@freearhey/core": "^0.3.1", "@freearhey/core": "^0.3.1",
"@freearhey/search-js": "^0.1.1",
"@octokit/core": "^4.1.0", "@octokit/core": "^4.1.0",
"@types/cli-progress": "^3.11.3", "@types/cli-progress": "^3.11.3",
"@types/fs-extra": "^11.0.2", "@types/fs-extra": "^11.0.2",
@ -1193,6 +1194,17 @@
"url": "https://github.com/sponsors/isaacs" "url": "https://github.com/sponsors/isaacs"
} }
}, },
"node_modules/@freearhey/search-js": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/@freearhey/search-js/-/search-js-0.1.1.tgz",
"integrity": "sha512-nmXhqBg5lh6GxMT2g1hgmSc/pbXGw0P4ZxXaPt/cnuZYtvPHSo0CPOF65iNC9LEqvrrnLig4RFlG1dBDPNAU0g==",
"dependencies": {
"lodash": "^4.17.21"
},
"engines": {
"node": ">=16.0.0"
}
},
"node_modules/@humanwhocodes/config-array": { "node_modules/@humanwhocodes/config-array": {
"version": "0.9.5", "version": "0.9.5",
"resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.9.5.tgz", "resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.9.5.tgz",
@ -9286,6 +9298,14 @@
} }
} }
}, },
"@freearhey/search-js": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/@freearhey/search-js/-/search-js-0.1.1.tgz",
"integrity": "sha512-nmXhqBg5lh6GxMT2g1hgmSc/pbXGw0P4ZxXaPt/cnuZYtvPHSo0CPOF65iNC9LEqvrrnLig4RFlG1dBDPNAU0g==",
"requires": {
"lodash": "^4.17.21"
}
},
"@humanwhocodes/config-array": { "@humanwhocodes/config-array": {
"version": "0.9.5", "version": "0.9.5",
"resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.9.5.tgz", "resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.9.5.tgz",

View file

@ -29,6 +29,7 @@
"dependencies": { "dependencies": {
"@alex_neo/jest-expect-message": "^1.0.5", "@alex_neo/jest-expect-message": "^1.0.5",
"@freearhey/core": "^0.3.1", "@freearhey/core": "^0.3.1",
"@freearhey/search-js": "^0.1.1",
"@octokit/core": "^4.1.0", "@octokit/core": "^4.1.0",
"@types/cli-progress": "^3.11.3", "@types/cli-progress": "^3.11.3",
"@types/fs-extra": "^11.0.2", "@types/fs-extra": "^11.0.2",

View file

@ -6,6 +6,7 @@ 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'
program program
.argument('<filepath>', 'Path to *.channels.xml file to edit') .argument('<filepath>', 'Path to *.channels.xml file to edit')
@ -38,7 +39,8 @@ async function main() {
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 channels = new Collection(channelsContent).map(data => new ApiChannel(data))
const channelsIndex = sj.createIndex(channelsContent)
const buffer = new Dictionary() const buffer = new Dictionary()
for (let option of options.all()) { for (let option of options.all()) {
@ -49,7 +51,7 @@ async function main() {
} }
continue continue
} }
const choices = getOptions(channels, channel) 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: `Choose xmltv_id for "${channel.name}" (${channel.site_id}):`,
@ -143,9 +145,10 @@ async function getInput(channel: Channel) {
return { name, xmltv_id: input['xmltv_id'] } return { name, xmltv_id: input['xmltv_id'] }
} }
function getOptions(channels: Collection, channel: Channel) { function getOptions(channelsIndex, channel: Channel) {
const channelId = generateCode(channel.name, defaultCountry) const channelId = generateCode(channel.name, defaultCountry)
const similar = getSimilar(channels, channelId) const query = channel.name.replace(/\s(SD|TV|HD)$/i, '')
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}`) variants.add(`${channel.name.trim()} | ${channelId}${newLabel}`)
@ -162,14 +165,6 @@ function getOptions(channels: Collection, channel: Channel) {
return variants.all() return variants.all()
} }
function getSimilar(channels: Collection, channelId: string) {
const normChannelId = channelId.split('.')[0].slice(0, 8).toLowerCase()
return channels.filter((channel: ApiChannel) =>
channel.id.split('.')[0].toLowerCase().startsWith(normChannelId)
)
}
function generateCode(name: string, country: string) { function generateCode(name: string, country: string) {
const channelId: string = transliterate(name) const channelId: string = transliterate(name)
.replace(/\+/gi, 'Plus') .replace(/\+/gi, 'Plus')

View file

@ -381,6 +381,13 @@
object-treeify "^2.1.1" object-treeify "^2.1.1"
signale "^1.4.0" signale "^1.4.0"
"@freearhey/search-js@^0.1.1":
version "0.1.1"
resolved "https://registry.npmjs.org/@freearhey/search-js/-/search-js-0.1.1.tgz"
integrity sha512-nmXhqBg5lh6GxMT2g1hgmSc/pbXGw0P4ZxXaPt/cnuZYtvPHSo0CPOF65iNC9LEqvrrnLig4RFlG1dBDPNAU0g==
dependencies:
lodash "^4.17.21"
"@humanwhocodes/config-array@^0.9.2": "@humanwhocodes/config-array@^0.9.2":
version "0.9.5" version "0.9.5"
resolved "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.9.5.tgz" resolved "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.9.5.tgz"