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/db.js

223 lines
4.4 KiB
JavaScript

4 years ago
const categories = require('./categories')
const parser = require('./parser')
const utils = require('./utils')
4 years ago
const sfwCategories = categories.filter(c => !c.nsfw).map(c => c.name)
4 years ago
const db = {}
db.load = function () {
const items = parser.parseIndex()
for (const item of items) {
const playlist = parser.parsePlaylist(item.url)
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,
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())
}
this.nsfw = true
4 years ago
this.duplicates = 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
},
4 years ago
all() {
return this.list
},
4 years ago
sfw() {
return this.list.filter(i => sfwCategories.includes(i.category))
},
4 years ago
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.name))
},
except(list = []) {
return this.list.filter(playlist => !list.includes(playlist.name))
},
sortBy(fields) {
this.list = utils.sortBy(this.list, fields)
return this
},
count() {
return this.list.length
}
}
4 years ago
module.exports = db