From 7bcd1fedd4293873947b15d59d9d2e68d872e062 Mon Sep 17 00:00:00 2001 From: Aleksandr Statciuk Date: Mon, 20 Sep 2021 16:10:38 +0300 Subject: [PATCH 1/5] Install natural-orderby package --- package-lock.json | 14 ++++++++++++++ package.json | 1 + 2 files changed, 15 insertions(+) diff --git a/package-lock.json b/package-lock.json index ebcda9db2..9b238823f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "iptv-playlist-parser": "^0.5.4", "m3u-linter": "^0.2.1", "markdown-include": "^0.4.3", + "natural-orderby": "^2.0.3", "normalize-url": "^6.1.0", "pre-push": "^0.1.1", "progress": "^2.0.3", @@ -2948,6 +2949,14 @@ "resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz", "integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=" }, + "node_modules/natural-orderby": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/natural-orderby/-/natural-orderby-2.0.3.tgz", + "integrity": "sha512-p7KTHxU0CUrcOXe62Zfrb5Z13nLvPhSWR/so3kFulUQU0sgUll2Z0LwpsLN351eOOD+hRGu/F1g+6xDfPeD++Q==", + "engines": { + "node": "*" + } + }, "node_modules/node-int64": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/node-int64/-/node-int64-0.4.0.tgz", @@ -6045,6 +6054,11 @@ "resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz", "integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=" }, + "natural-orderby": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/natural-orderby/-/natural-orderby-2.0.3.tgz", + "integrity": "sha512-p7KTHxU0CUrcOXe62Zfrb5Z13nLvPhSWR/so3kFulUQU0sgUll2Z0LwpsLN351eOOD+hRGu/F1g+6xDfPeD++Q==" + }, "node-int64": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/node-int64/-/node-int64-0.4.0.tgz", diff --git a/package.json b/package.json index b2eb86fad..22ed71f1d 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "iptv-playlist-parser": "^0.5.4", "m3u-linter": "^0.2.1", "markdown-include": "^0.4.3", + "natural-orderby": "^2.0.3", "normalize-url": "^6.1.0", "pre-push": "^0.1.1", "progress": "^2.0.3", From d0766d5e392f54f9fa05bc73b6f52a4658b4bb8c Mon Sep 17 00:00:00 2001 From: Aleksandr Statciuk Date: Mon, 20 Sep 2021 17:08:36 +0300 Subject: [PATCH 2/5] Update utils.js --- scripts/helpers/utils.js | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/scripts/helpers/utils.js b/scripts/helpers/utils.js index eb8f3d814..9c83d86a2 100644 --- a/scripts/helpers/utils.js +++ b/scripts/helpers/utils.js @@ -1,5 +1,6 @@ -const transliteration = require('transliteration') +const { orderBy } = require('natural-orderby') const iso6393 = require('@freearhey/iso-639-3') +const transliteration = require('transliteration') const categories = require('../data/categories') const regions = require('../data/regions') @@ -54,20 +55,13 @@ utils.language2code = function (name) { return lang && lang.code ? lang.code : null } -utils.sortBy = function (arr, fields) { - return arr.sort((a, b) => { - for (let field of fields) { - let propA = a[field] ? a[field].toLowerCase() : '' - let propB = b[field] ? b[field].toLowerCase() : '' - if (propA === 'undefined') return 1 - if (propB === 'undefined') return -1 - if (propA === 'other') return 1 - if (propB === 'other') return -1 - if (propA < propB) return -1 - if (propA > propB) return 1 - } - return 0 +utils.sortBy = function (arr, fields, order = null) { + fields = fields.map(field => { + if (field === 'resolution.height') return channel => channel.resolution.height || 0 + if (field === 'status') return channel => channel.status || '' + return channel => channel[field] }) + return orderBy(arr, fields, order) } utils.removeProtocol = function (string) { From 6f6aea9699a2542f476c0e1bc74eb360e5f2d7fa Mon Sep 17 00:00:00 2001 From: Aleksandr Statciuk Date: Mon, 20 Sep 2021 17:10:23 +0300 Subject: [PATCH 3/5] Update db.js --- scripts/helpers/db.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/scripts/helpers/db.js b/scripts/helpers/db.js index 0545a6c73..f0a3b967a 100644 --- a/scripts/helpers/db.js +++ b/scripts/helpers/db.js @@ -152,8 +152,8 @@ db.channels = { count() { return this.get().length }, - sortBy(fields) { - this.list = utils.sortBy(this.list, fields) + sortBy(fields, order) { + this.list = utils.sortBy(this.list, fields, order) return this } @@ -173,8 +173,8 @@ db.countries = { count() { return this.list.length }, - sortBy(fields) { - this.list = utils.sortBy(this.list, fields) + sortBy(fields, order) { + this.list = utils.sortBy(this.list, fields, order) return this } @@ -194,8 +194,8 @@ db.languages = { count() { return this.list.length }, - sortBy(fields) { - this.list = utils.sortBy(this.list, fields) + sortBy(fields, order) { + this.list = utils.sortBy(this.list, fields, order) return this } @@ -225,8 +225,8 @@ db.playlists = { except(list = []) { return this.list.filter(playlist => !list.includes(playlist.filename)) }, - sortBy(fields) { - this.list = utils.sortBy(this.list, fields) + sortBy(fields, order) { + this.list = utils.sortBy(this.list, fields, order) return this }, From 63db7140cfaccb8aec545f691f8072c5b9e53971 Mon Sep 17 00:00:00 2001 From: Aleksandr Statciuk Date: Mon, 20 Sep 2021 17:14:45 +0300 Subject: [PATCH 4/5] Update sort.js --- scripts/sort.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/scripts/sort.js b/scripts/sort.js index 976dfcae0..c199bd5d1 100644 --- a/scripts/sort.js +++ b/scripts/sort.js @@ -22,8 +22,12 @@ async function main() { } async function sortChannels(playlist) { - const channels = [...playlist.channels] - utils.sortBy(channels, ['name', 'status', 'url']) + let channels = [...playlist.channels] + channels = utils.sortBy( + channels, + ['name', 'status', 'resolution.height', 'url'], + ['asc', 'asc', 'desc', 'asc'] + ) if (JSON.stringify(channels) !== JSON.stringify(playlist.channels)) { log.print('updated') From b2c3e452f4dadb3d7ede0d2408e52e062e44d850 Mon Sep 17 00:00:00 2001 From: Aleksandr Statciuk Date: Mon, 20 Sep 2021 17:14:50 +0300 Subject: [PATCH 5/5] Update generate.js --- scripts/generate.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/scripts/generate.js b/scripts/generate.js index 4b50ec3c1..07aaf7e1a 100644 --- a/scripts/generate.js +++ b/scripts/generate.js @@ -36,7 +36,11 @@ function createNoJekyllFile() { function generateIndex() { log.print('Generating index.m3u...\n') - const channels = db.channels.sortBy(['name', 'url']).removeDuplicates().removeOffline().get() + const channels = db.channels + .sortBy(['name', 'status', 'resolution.height', 'url'], ['asc', 'asc', 'desc', 'asc']) + .removeDuplicates() + .removeOffline() + .get() const guides = channels.map(channel => channel.tvg.url) const filename = `${ROOT_DIR}/index.m3u` @@ -57,7 +61,10 @@ function generateIndex() { function generateCategoryIndex() { log.print('Generating index.category.m3u...\n') const channels = db.channels - .sortBy(['category', 'name', 'url']) + .sortBy( + ['category', 'name', 'status', 'resolution.height', 'url'], + ['asc', 'asc', 'asc', 'desc', 'asc'] + ) .removeDuplicates() .removeOffline() .get() @@ -79,7 +86,7 @@ function generateCountryIndex() { const lines = [] for (const country of [{ code: 'undefined' }, ...db.countries.sortBy(['name']).all()]) { const channels = db.channels - .sortBy(['name', 'url']) + .sortBy(['name', 'status', 'resolution.height', 'url'], ['asc', 'asc', 'desc', 'asc']) .forCountry(country) .removeDuplicates() .removeNSFW() @@ -106,7 +113,7 @@ function generateLanguageIndex() { const lines = [] for (const language of [{ code: 'undefined' }, ...db.languages.sortBy(['name']).all()]) { const channels = db.channels - .sortBy(['name', 'url']) + .sortBy(['name', 'status', 'resolution.height', 'url'], ['asc', 'asc', 'desc', 'asc']) .forLanguage(language) .removeDuplicates() .removeNSFW() @@ -133,7 +140,7 @@ function generateCategories() { for (const category of [...db.categories.all(), { id: 'other' }]) { const channels = db.channels - .sortBy(['name', 'url']) + .sortBy(['name', 'status', 'resolution.height', 'url'], ['asc', 'asc', 'desc', 'asc']) .forCategory(category) .removeDuplicates() .removeOffline() @@ -156,7 +163,7 @@ function generateCountries() { for (const country of [...db.countries.all(), { code: 'undefined' }]) { const channels = db.channels - .sortBy(['name', 'url']) + .sortBy(['name', 'status', 'resolution.height', 'url'], ['asc', 'asc', 'desc', 'asc']) .forCountry(country) .removeDuplicates() .removeOffline() @@ -180,7 +187,7 @@ function generateLanguages() { for (const language of [...db.languages.all(), { code: 'undefined' }]) { const channels = db.channels - .sortBy(['name', 'url']) + .sortBy(['name', 'status', 'resolution.height', 'url'], ['asc', 'asc', 'desc', 'asc']) .forLanguage(language) .removeDuplicates() .removeOffline() @@ -201,7 +208,7 @@ function generateChannelsJson() { log.print('Generating channels.json...\n') const filename = `${ROOT_DIR}/channels.json` const channels = db.channels - .sortBy(['name', 'url']) + .sortBy(['name', 'status', 'resolution.height', 'url'], ['asc', 'asc', 'desc', 'asc']) .get() .map(c => c.toObject()) file.create(filename, JSON.stringify(channels))