iptv/scripts/db.js

195 lines
3.8 KiB
JavaScript
Raw Normal View History

2021-02-10 14:39:35 +01:00
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)
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-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':
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 =>
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':
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
},
2021-02-10 14:39:35 +01:00
all() {
return this.list
},
2021-02-10 15:10:27 +01:00
sfw() {
const sfwCategories = categories.filter(c => !c.nsfw).map(c => c.name)
return this.list.filter(i => sfwCategories.includes(i.category))
},
2021-02-10 14:39:35 +01:00
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.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
}
}
2021-02-10 14:39:35 +01:00
module.exports = db