Merge pull request #4636 from iptv-org/update-sorting-algorithm

Update sorting algorithm
This commit is contained in:
Dum4G 2021-09-20 20:02:43 +03:00 committed by GitHub
commit 5fc21a8c82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 52 additions and 32 deletions

14
package-lock.json generated
View File

@ -14,6 +14,7 @@
"iptv-playlist-parser": "^0.5.4", "iptv-playlist-parser": "^0.5.4",
"m3u-linter": "^0.2.1", "m3u-linter": "^0.2.1",
"markdown-include": "^0.4.3", "markdown-include": "^0.4.3",
"natural-orderby": "^2.0.3",
"normalize-url": "^6.1.0", "normalize-url": "^6.1.0",
"pre-push": "^0.1.1", "pre-push": "^0.1.1",
"progress": "^2.0.3", "progress": "^2.0.3",
@ -2948,6 +2949,14 @@
"resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz", "resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz",
"integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=" "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": { "node_modules/node-int64": {
"version": "0.4.0", "version": "0.4.0",
"resolved": "https://registry.npmjs.org/node-int64/-/node-int64-0.4.0.tgz", "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", "resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz",
"integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=" "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": { "node-int64": {
"version": "0.4.0", "version": "0.4.0",
"resolved": "https://registry.npmjs.org/node-int64/-/node-int64-0.4.0.tgz", "resolved": "https://registry.npmjs.org/node-int64/-/node-int64-0.4.0.tgz",

View File

@ -17,6 +17,7 @@
"iptv-playlist-parser": "^0.5.4", "iptv-playlist-parser": "^0.5.4",
"m3u-linter": "^0.2.1", "m3u-linter": "^0.2.1",
"markdown-include": "^0.4.3", "markdown-include": "^0.4.3",
"natural-orderby": "^2.0.3",
"normalize-url": "^6.1.0", "normalize-url": "^6.1.0",
"pre-push": "^0.1.1", "pre-push": "^0.1.1",
"progress": "^2.0.3", "progress": "^2.0.3",

View File

@ -36,7 +36,11 @@ function createNoJekyllFile() {
function generateIndex() { function generateIndex() {
log.print('Generating index.m3u...\n') 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 guides = channels.map(channel => channel.tvg.url)
const filename = `${ROOT_DIR}/index.m3u` const filename = `${ROOT_DIR}/index.m3u`
@ -57,7 +61,10 @@ function generateIndex() {
function generateCategoryIndex() { function generateCategoryIndex() {
log.print('Generating index.category.m3u...\n') log.print('Generating index.category.m3u...\n')
const channels = db.channels const channels = db.channels
.sortBy(['category', 'name', 'url']) .sortBy(
['category', 'name', 'status', 'resolution.height', 'url'],
['asc', 'asc', 'asc', 'desc', 'asc']
)
.removeDuplicates() .removeDuplicates()
.removeOffline() .removeOffline()
.get() .get()
@ -79,7 +86,7 @@ function generateCountryIndex() {
const lines = [] const lines = []
for (const country of [{ code: 'undefined' }, ...db.countries.sortBy(['name']).all()]) { for (const country of [{ code: 'undefined' }, ...db.countries.sortBy(['name']).all()]) {
const channels = db.channels const channels = db.channels
.sortBy(['name', 'url']) .sortBy(['name', 'status', 'resolution.height', 'url'], ['asc', 'asc', 'desc', 'asc'])
.forCountry(country) .forCountry(country)
.removeDuplicates() .removeDuplicates()
.removeNSFW() .removeNSFW()
@ -106,7 +113,7 @@ function generateLanguageIndex() {
const lines = [] const lines = []
for (const language of [{ code: 'undefined' }, ...db.languages.sortBy(['name']).all()]) { for (const language of [{ code: 'undefined' }, ...db.languages.sortBy(['name']).all()]) {
const channels = db.channels const channels = db.channels
.sortBy(['name', 'url']) .sortBy(['name', 'status', 'resolution.height', 'url'], ['asc', 'asc', 'desc', 'asc'])
.forLanguage(language) .forLanguage(language)
.removeDuplicates() .removeDuplicates()
.removeNSFW() .removeNSFW()
@ -133,7 +140,7 @@ function generateCategories() {
for (const category of [...db.categories.all(), { id: 'other' }]) { for (const category of [...db.categories.all(), { id: 'other' }]) {
const channels = db.channels const channels = db.channels
.sortBy(['name', 'url']) .sortBy(['name', 'status', 'resolution.height', 'url'], ['asc', 'asc', 'desc', 'asc'])
.forCategory(category) .forCategory(category)
.removeDuplicates() .removeDuplicates()
.removeOffline() .removeOffline()
@ -156,7 +163,7 @@ function generateCountries() {
for (const country of [...db.countries.all(), { code: 'undefined' }]) { for (const country of [...db.countries.all(), { code: 'undefined' }]) {
const channels = db.channels const channels = db.channels
.sortBy(['name', 'url']) .sortBy(['name', 'status', 'resolution.height', 'url'], ['asc', 'asc', 'desc', 'asc'])
.forCountry(country) .forCountry(country)
.removeDuplicates() .removeDuplicates()
.removeOffline() .removeOffline()
@ -180,7 +187,7 @@ function generateLanguages() {
for (const language of [...db.languages.all(), { code: 'undefined' }]) { for (const language of [...db.languages.all(), { code: 'undefined' }]) {
const channels = db.channels const channels = db.channels
.sortBy(['name', 'url']) .sortBy(['name', 'status', 'resolution.height', 'url'], ['asc', 'asc', 'desc', 'asc'])
.forLanguage(language) .forLanguage(language)
.removeDuplicates() .removeDuplicates()
.removeOffline() .removeOffline()
@ -201,7 +208,7 @@ function generateChannelsJson() {
log.print('Generating channels.json...\n') log.print('Generating channels.json...\n')
const filename = `${ROOT_DIR}/channels.json` const filename = `${ROOT_DIR}/channels.json`
const channels = db.channels const channels = db.channels
.sortBy(['name', 'url']) .sortBy(['name', 'status', 'resolution.height', 'url'], ['asc', 'asc', 'desc', 'asc'])
.get() .get()
.map(c => c.toObject()) .map(c => c.toObject())
file.create(filename, JSON.stringify(channels)) file.create(filename, JSON.stringify(channels))

View File

@ -152,8 +152,8 @@ db.channels = {
count() { count() {
return this.get().length return this.get().length
}, },
sortBy(fields) { sortBy(fields, order) {
this.list = utils.sortBy(this.list, fields) this.list = utils.sortBy(this.list, fields, order)
return this return this
} }
@ -173,8 +173,8 @@ db.countries = {
count() { count() {
return this.list.length return this.list.length
}, },
sortBy(fields) { sortBy(fields, order) {
this.list = utils.sortBy(this.list, fields) this.list = utils.sortBy(this.list, fields, order)
return this return this
} }
@ -194,8 +194,8 @@ db.languages = {
count() { count() {
return this.list.length return this.list.length
}, },
sortBy(fields) { sortBy(fields, order) {
this.list = utils.sortBy(this.list, fields) this.list = utils.sortBy(this.list, fields, order)
return this return this
} }
@ -225,8 +225,8 @@ db.playlists = {
except(list = []) { except(list = []) {
return this.list.filter(playlist => !list.includes(playlist.filename)) return this.list.filter(playlist => !list.includes(playlist.filename))
}, },
sortBy(fields) { sortBy(fields, order) {
this.list = utils.sortBy(this.list, fields) this.list = utils.sortBy(this.list, fields, order)
return this return this
}, },

View File

@ -1,5 +1,6 @@
const transliteration = require('transliteration') const { orderBy } = require('natural-orderby')
const iso6393 = require('@freearhey/iso-639-3') const iso6393 = require('@freearhey/iso-639-3')
const transliteration = require('transliteration')
const categories = require('../data/categories') const categories = require('../data/categories')
const regions = require('../data/regions') const regions = require('../data/regions')
@ -54,20 +55,13 @@ utils.language2code = function (name) {
return lang && lang.code ? lang.code : null return lang && lang.code ? lang.code : null
} }
utils.sortBy = function (arr, fields) { utils.sortBy = function (arr, fields, order = null) {
return arr.sort((a, b) => { fields = fields.map(field => {
for (let field of fields) { if (field === 'resolution.height') return channel => channel.resolution.height || 0
let propA = a[field] ? a[field].toLowerCase() : '' if (field === 'status') return channel => channel.status || ''
let propB = b[field] ? b[field].toLowerCase() : '' return channel => channel[field]
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
}) })
return orderBy(arr, fields, order)
} }
utils.removeProtocol = function (string) { utils.removeProtocol = function (string) {

View File

@ -22,8 +22,12 @@ async function main() {
} }
async function sortChannels(playlist) { async function sortChannels(playlist) {
const channels = [...playlist.channels] let channels = [...playlist.channels]
utils.sortBy(channels, ['name', 'status', 'url']) channels = utils.sortBy(
channels,
['name', 'status', 'resolution.height', 'url'],
['asc', 'asc', 'desc', 'asc']
)
if (JSON.stringify(channels) !== JSON.stringify(playlist.channels)) { if (JSON.stringify(channels) !== JSON.stringify(playlist.channels)) {
log.print('updated') log.print('updated')