From 6dd2dbbf316c4bfa0ae2da6b620377ae3f8512ba Mon Sep 17 00:00:00 2001 From: freearhey <7253922+freearhey@users.noreply.github.com> Date: Sun, 19 Nov 2023 10:46:24 +0300 Subject: [PATCH] Update channels:editor --- package-lock.json | 20 ++++++++++++++++++++ package.json | 1 + scripts/commands/channels/editor.ts | 19 +++++++------------ yarn.lock | 7 +++++++ 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/package-lock.json b/package-lock.json index fae52e5b..3b60ef42 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "dependencies": { "@alex_neo/jest-expect-message": "^1.0.5", "@freearhey/core": "^0.3.1", + "@freearhey/search-js": "^0.1.1", "@octokit/core": "^4.1.0", "@types/cli-progress": "^3.11.3", "@types/fs-extra": "^11.0.2", @@ -1193,6 +1194,17 @@ "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": { "version": "0.9.5", "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": { "version": "0.9.5", "resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.9.5.tgz", diff --git a/package.json b/package.json index e08b52c7..ce719e4a 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "dependencies": { "@alex_neo/jest-expect-message": "^1.0.5", "@freearhey/core": "^0.3.1", + "@freearhey/search-js": "^0.1.1", "@octokit/core": "^4.1.0", "@types/cli-progress": "^3.11.3", "@types/fs-extra": "^11.0.2", diff --git a/scripts/commands/channels/editor.ts b/scripts/commands/channels/editor.ts index 7b9417b1..34f80d63 100644 --- a/scripts/commands/channels/editor.ts +++ b/scripts/commands/channels/editor.ts @@ -6,6 +6,7 @@ import { transliterate } from 'transliteration' import nodeCleanup from 'node-cleanup' import { program } from 'commander' import inquirer, { QuestionCollection } from 'inquirer' +import sj from '@freearhey/search-js' program .argument('', 'Path to *.channels.xml file to edit') @@ -38,7 +39,8 @@ async function main() { const dataStorage = new Storage(DATA_DIR) 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() for (let option of options.all()) { @@ -49,7 +51,7 @@ async function main() { } continue } - const choices = getOptions(channels, channel) + const choices = getOptions(channelsIndex, channel) const question: QuestionCollection = { name: 'option', 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'] } } -function getOptions(channels: Collection, channel: Channel) { +function getOptions(channelsIndex, channel: Channel) { 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() variants.add(`${channel.name.trim()} | ${channelId}${newLabel}`) @@ -162,14 +165,6 @@ function getOptions(channels: Collection, channel: Channel) { 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) { const channelId: string = transliterate(name) .replace(/\+/gi, 'Plus') diff --git a/yarn.lock b/yarn.lock index a28a16a0..1f1cebeb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -381,6 +381,13 @@ object-treeify "^2.1.1" 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": version "0.9.5" resolved "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.9.5.tgz"