iptv/scripts/helpers/db.js

230 lines
4.4 KiB
JavaScript
Raw Normal View History

2021-08-30 19:27:12 +02:00
const categories = require('../data/categories')
2021-02-10 14:39:35 +01:00
const parser = require('./parser')
const utils = require('./utils')
const db = {}
2021-08-01 19:31:46 +02:00
db.load = async function () {
2021-02-10 14:39:35 +01:00
const items = parser.parseIndex()
for (const item of items) {
2021-08-01 19:31:46 +02:00
const playlist = await parser.parsePlaylist(item.url)
2021-02-20 13:46:56 +01:00
db.playlists.add(playlist)
2021-02-10 14:39:35 +01:00
for (const channel of playlist.channels) {
db.channels.add(channel)
for (const country of channel.countries) {
if (!db.countries.has(country)) {
db.countries.add(country)
}
}
for (const language of channel.languages) {
if (!db.languages.has(language)) {
db.languages.add(language)
}
}
}
}
}
db.channels = {
list: [],
2021-02-11 05:16:19 +01:00
filter: null,
2021-05-10 15:12:13 +02:00
duplicates: true,
2021-08-06 06:49:46 +02:00
offline: true,
2021-06-16 19:04:33 +02:00
nsfw: true,
2021-02-10 14:39:35 +01:00
add(channel) {
this.list.push(channel)
},
2021-02-11 05:16:19 +01:00
get() {
let output
if (this.filter) {
switch (this.filter.field) {
case 'countries':
2021-05-07 17:34:20 +02:00
if (this.filter.value === 'undefined') {
2021-02-11 05:16:19 +01:00
output = this.list.filter(channel => !channel.countries.length)
} else {
output = this.list.filter(channel =>
channel.countries.map(c => c.code).includes(this.filter.value)
)
}
break
case 'languages':
2021-05-07 17:34:20 +02:00
if (this.filter.value === 'undefined') {
2021-02-11 05:16:19 +01:00
output = this.list.filter(channel => !channel.languages.length)
} else {
output = this.list.filter(channel =>
2021-02-11 05:27:48 +01:00
channel.languages.map(c => c.code).includes(this.filter.value)
2021-02-11 05:16:19 +01:00
)
}
break
case 'category':
2021-05-07 17:34:20 +02:00
if (this.filter.value === 'other') {
2021-02-11 05:16:19 +01:00
output = this.list.filter(channel => !channel.category)
} else {
output = this.list.filter(
channel => channel.category.toLowerCase() === this.filter.value
)
}
break
}
} else {
2021-05-10 15:12:13 +02:00
output = this.list
2021-02-11 05:16:19 +01:00
}
2021-05-10 15:12:13 +02:00
if (!this.duplicates) {
const buffer = []
output = output.filter(channel => {
const info = channel.getInfo()
if (buffer.includes(info)) return false
buffer.push(info)
return true
})
}
2021-06-06 12:15:10 +02:00
if (!this.nsfw) {
output = output.filter(channel => !channel.isNSFW())
}
2021-08-06 06:49:46 +02:00
if (!this.offline) {
output = output.filter(channel => channel.status !== 'Offline')
}
2021-06-06 12:15:10 +02:00
this.nsfw = true
2021-05-10 15:12:13 +02:00
this.duplicates = true
2021-08-06 06:49:46 +02:00
this.offline = true
2021-02-11 05:16:19 +01:00
this.filter = null
return output
},
2021-05-10 15:12:13 +02:00
removeDuplicates() {
this.duplicates = false
return this
},
2021-06-06 12:15:10 +02:00
removeNSFW() {
this.nsfw = false
return this
},
2021-08-06 06:49:46 +02:00
removeOffline() {
this.offline = false
return this
},
2021-02-10 14:39:35 +01:00
all() {
return this.list
},
forCountry(country) {
2021-02-11 05:16:19 +01:00
this.filter = {
field: 'countries',
value: country.code
}
2021-02-10 14:39:35 +01:00
2021-02-11 05:16:19 +01:00
return this
2021-02-10 14:39:35 +01:00
},
forLanguage(language) {
2021-02-11 05:16:19 +01:00
this.filter = {
field: 'languages',
value: language.code
}
2021-02-10 15:03:42 +01:00
2021-02-11 05:16:19 +01:00
return this
2021-02-10 14:39:35 +01:00
},
forCategory(category) {
2021-02-11 05:16:19 +01:00
this.filter = {
field: 'category',
value: category.id
}
2021-02-10 15:03:42 +01:00
2021-02-11 05:16:19 +01:00
return this
2021-02-10 14:39:35 +01:00
},
count() {
2021-02-11 05:16:19 +01:00
return this.get().length
2021-02-10 14:39:35 +01:00
},
sortBy(fields) {
this.list = utils.sortBy(this.list, fields)
return this
}
}
db.countries = {
list: [],
has(country) {
return this.list.map(c => c.code).includes(country.code)
},
add(country) {
this.list.push(country)
},
all() {
return this.list
},
count() {
return this.list.length
},
sortBy(fields) {
this.list = utils.sortBy(this.list, fields)
return this
}
}
db.languages = {
list: [],
has(language) {
return this.list.map(c => c.code).includes(language.code)
},
add(language) {
this.list.push(language)
},
all() {
return this.list
},
count() {
return this.list.length
2021-02-10 15:03:42 +01:00
},
sortBy(fields) {
this.list = utils.sortBy(this.list, fields)
return this
2021-02-10 14:39:35 +01:00
}
}
db.categories = {
list: categories,
all() {
return this.list
},
count() {
return this.list.length
}
}
2021-02-20 13:46:56 +01:00
db.playlists = {
list: [],
add(playlist) {
this.list.push(playlist)
},
all() {
return this.list
},
only(list = []) {
return this.list.filter(playlist => list.includes(playlist.filename))
2021-02-20 13:46:56 +01:00
},
except(list = []) {
return this.list.filter(playlist => !list.includes(playlist.filename))
2021-02-20 13:46:56 +01:00
},
sortBy(fields) {
this.list = utils.sortBy(this.list, fields)
return this
},
count() {
return this.list.length
}
}
2021-02-10 14:39:35 +01:00
module.exports = db