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",
"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",

View File

@ -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",

View File

@ -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))

View File

@ -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
},

View File

@ -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) {

View File

@ -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')