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

195 lines
3.8 KiB
JavaScript

4 years ago
const categories = require('./categories')
const parser = require('./parser')
const utils = require('./utils')
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
add(channel) {
this.list.push(channel)
},
4 years ago
get() {
let output
if (this.filter) {
switch (this.filter.field) {
case 'countries':
if (!this.filter.value) {
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':
if (!this.filter.value) {
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':
if (!this.filter.value) {
output = this.list.filter(channel => !channel.category)
} else {
output = this.list.filter(
channel => channel.category.toLowerCase() === this.filter.value
)
}
break
}
} else {
output = this.all()
}
this.filter = null
return output
},
4 years ago
all() {
return this.list
},
4 years ago
sfw() {
const sfwCategories = categories.filter(c => !c.nsfw).map(c => c.name)
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