You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iptv/scripts/helpers/db.js

231 lines
4.5 KiB
JavaScript

const categories = require('../data/categories')
4 years ago
const parser = require('./parser')
const utils = require('./utils')
3 years ago
const file = require('./file')
4 years ago
const db = {}
3 years ago
db.load = async function () {
3 years ago
let files = await file.list()
for (const file of files) {
const playlist = await parser.parsePlaylist(file)
4 years ago
db.playlists.add(playlist)
4 years ago
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: [],
4 years ago
filter: null,
4 years ago
duplicates: true,
3 years ago
offline: true,
4 years ago
nsfw: true,
4 years ago
add(channel) {
this.list.push(channel)
},
4 years ago
get() {
let output
if (this.filter) {
switch (this.filter.field) {
case 'countries':
4 years ago
if (this.filter.value === 'undefined') {
4 years ago
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':
4 years ago
if (this.filter.value === 'undefined') {
4 years ago
output = this.list.filter(channel => !channel.languages.length)
} else {
output = this.list.filter(channel =>
4 years ago
channel.languages.map(c => c.code).includes(this.filter.value)
4 years ago
)
}
break
case 'category':
4 years ago
if (this.filter.value === 'other') {
4 years ago
output = this.list.filter(channel => !channel.category)
} else {
output = this.list.filter(
channel => channel.category.toLowerCase() === this.filter.value
)
}
break
}
} else {
4 years ago
output = this.list
4 years ago
}
4 years ago
if (!this.duplicates) {
const buffer = []
output = output.filter(channel => {
const info = channel.getInfo()
if (buffer.includes(info)) return false
buffer.push(info)
return true
})
}
4 years ago
if (!this.nsfw) {
output = output.filter(channel => !channel.isNSFW())
}
3 years ago
if (!this.offline) {
output = output.filter(channel => channel.status !== 'Offline')
}
4 years ago
this.nsfw = true
4 years ago
this.duplicates = true
3 years ago
this.offline = true
4 years ago
this.filter = null
return output
},
4 years ago
removeDuplicates() {
this.duplicates = false
return this
},
4 years ago
removeNSFW() {
this.nsfw = false
return this
},
3 years ago
removeOffline() {
this.offline = false
return this
},
4 years ago
all() {
return this.list
},
forCountry(country) {
4 years ago
this.filter = {
field: 'countries',
value: country.code
}
4 years ago
4 years ago
return this
4 years ago
},
forLanguage(language) {
4 years ago
this.filter = {
field: 'languages',
value: language.code
}
4 years ago
4 years ago
return this
4 years ago
},
forCategory(category) {
4 years ago
this.filter = {
field: 'category',
value: category.id
}
4 years ago
4 years ago
return this
4 years ago
},
count() {
4 years ago
return this.get().length
4 years ago
},
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
4 years ago
},
sortBy(fields) {
this.list = utils.sortBy(this.list, fields)
return this
4 years ago
}
}
db.categories = {
list: categories,
all() {
return this.list
},
count() {
return this.list.length
}
}
4 years ago
db.playlists = {
list: [],
add(playlist) {
this.list.push(playlist)
},
all() {
return this.list
},
only(list = []) {
return this.list.filter(playlist => list.includes(playlist.filename))
4 years ago
},
except(list = []) {
return this.list.filter(playlist => !list.includes(playlist.filename))
4 years ago
},
sortBy(fields) {
this.list = utils.sortBy(this.list, fields)
return this
},
count() {
return this.list.length
}
}
4 years ago
module.exports = db