From ec482d396ccc31abd7a772dcaeb949a9737b6e75 Mon Sep 17 00:00:00 2001 From: freearhey Date: Wed, 10 Feb 2021 16:39:35 +0300 Subject: [PATCH] Create db.js --- scripts/db.js | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 scripts/db.js diff --git a/scripts/db.js b/scripts/db.js new file mode 100644 index 0000000000..8b840f85ae --- /dev/null +++ b/scripts/db.js @@ -0,0 +1,105 @@ +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) + 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: [], + add(channel) { + this.list.push(channel) + }, + all() { + return this.list + }, + forCountry(country) { + if (!country.code) return this.list.filter(channel => !channel.countries.length) + + return this.list.filter(channel => channel.countries.map(c => c.code).includes(country.code)) + }, + forLanguage(language) { + return this.list.filter(channel => channel.languages.map(c => c.code).includes(language.code)) + }, + forCategory(category) { + return this.list.filter(channel => channel.category.toLowerCase() === category.id) + }, + count() { + return this.list.length + }, + 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 + } +} + +db.categories = { + list: categories, + all() { + return this.list + }, + count() { + return this.list.length + } +} + +module.exports = db