Update generate.js

This commit is contained in:
Aleksandr Statciuk 2021-08-01 20:31:49 +03:00
parent 61b2a014f7
commit 911fe272d6
1 changed files with 46 additions and 41 deletions

View File

@ -1,11 +1,11 @@
const db = require('./db') const file = require('./helpers/file')
const utils = require('./utils') const log = require('./helpers/log')
const db = require('./helpers/db')
const ROOT_DIR = './.gh-pages' const ROOT_DIR = './.gh-pages'
db.load() async function main() {
await loadDatabase()
function main() {
createRootDirectory() createRootDirectory()
createNoJekyllFile() createNoJekyllFile()
generateIndex() generateIndex()
@ -16,51 +16,56 @@ function main() {
generateCountries() generateCountries()
generateLanguages() generateLanguages()
generateChannelsJson() generateChannelsJson()
finish() showResults()
}
async function loadDatabase() {
log.print('Loading database...\n')
await db.load()
} }
function createRootDirectory() { function createRootDirectory() {
console.log('Creating .gh-pages folder...') log.print('Creating .gh-pages folder...\n')
utils.createDir(ROOT_DIR) file.createDir(ROOT_DIR)
} }
function createNoJekyllFile() { function createNoJekyllFile() {
console.log('Creating .nojekyll...') log.print('Creating .nojekyll...\n')
utils.createFile(`${ROOT_DIR}/.nojekyll`) file.create(`${ROOT_DIR}/.nojekyll`)
} }
function generateIndex() { function generateIndex() {
console.log('Generating index.m3u...') log.print('Generating index.m3u...\n')
const filename = `${ROOT_DIR}/index.m3u` const filename = `${ROOT_DIR}/index.m3u`
utils.createFile(filename, '#EXTM3U\n') file.create(filename, '#EXTM3U\n')
const nsfwFilename = `${ROOT_DIR}/index.nsfw.m3u` const nsfwFilename = `${ROOT_DIR}/index.nsfw.m3u`
utils.createFile(nsfwFilename, '#EXTM3U\n') file.create(nsfwFilename, '#EXTM3U\n')
const channels = db.channels.sortBy(['name', 'url']).removeDuplicates().get() const channels = db.channels.sortBy(['name', 'url']).removeDuplicates().get()
for (const channel of channels) { for (const channel of channels) {
if (!channel.isNSFW()) { if (!channel.isNSFW()) {
utils.appendToFile(filename, channel.toString()) file.append(filename, channel.toString())
} }
utils.appendToFile(nsfwFilename, channel.toString()) file.append(nsfwFilename, channel.toString())
} }
} }
function generateCategoryIndex() { function generateCategoryIndex() {
console.log('Generating index.category.m3u...') log.print('Generating index.category.m3u...\n')
const filename = `${ROOT_DIR}/index.category.m3u` const filename = `${ROOT_DIR}/index.category.m3u`
utils.createFile(filename, '#EXTM3U\n') file.create(filename, '#EXTM3U\n')
const channels = db.channels.sortBy(['category', 'name', 'url']).removeDuplicates().get() const channels = db.channels.sortBy(['category', 'name', 'url']).removeDuplicates().get()
for (const channel of channels) { for (const channel of channels) {
utils.appendToFile(filename, channel.toString()) file.append(filename, channel.toString())
} }
} }
function generateCountryIndex() { function generateCountryIndex() {
console.log('Generating index.country.m3u...') log.print('Generating index.country.m3u...\n')
const filename = `${ROOT_DIR}/index.country.m3u` const filename = `${ROOT_DIR}/index.country.m3u`
utils.createFile(filename, '#EXTM3U\n') file.create(filename, '#EXTM3U\n')
for (const country of [{ code: 'undefined' }, ...db.countries.sortBy(['name']).all()]) { for (const country of [{ code: 'undefined' }, ...db.countries.sortBy(['name']).all()]) {
const channels = db.channels const channels = db.channels
@ -73,7 +78,7 @@ function generateCountryIndex() {
const nsfw = channel.isNSFW() const nsfw = channel.isNSFW()
channel.category = country.name || '' channel.category = country.name || ''
if (!nsfw) { if (!nsfw) {
utils.appendToFile(filename, channel.toString()) file.append(filename, channel.toString())
} }
channel.category = category channel.category = category
} }
@ -81,9 +86,9 @@ function generateCountryIndex() {
} }
function generateLanguageIndex() { function generateLanguageIndex() {
console.log('Generating index.language.m3u...') log.print('Generating index.language.m3u...\n')
const filename = `${ROOT_DIR}/index.language.m3u` const filename = `${ROOT_DIR}/index.language.m3u`
utils.createFile(filename, '#EXTM3U\n') file.create(filename, '#EXTM3U\n')
for (const language of [{ code: 'undefined' }, ...db.languages.sortBy(['name']).all()]) { for (const language of [{ code: 'undefined' }, ...db.languages.sortBy(['name']).all()]) {
const channels = db.channels const channels = db.channels
@ -96,7 +101,7 @@ function generateLanguageIndex() {
const nsfw = channel.isNSFW() const nsfw = channel.isNSFW()
channel.category = language.name || '' channel.category = language.name || ''
if (!nsfw) { if (!nsfw) {
utils.appendToFile(filename, channel.toString()) file.append(filename, channel.toString())
} }
channel.category = category channel.category = category
} }
@ -104,13 +109,13 @@ function generateLanguageIndex() {
} }
function generateCategories() { function generateCategories() {
console.log(`Generating /categories...`) log.print(`Generating /categories...\n`)
const outputDir = `${ROOT_DIR}/categories` const outputDir = `${ROOT_DIR}/categories`
utils.createDir(outputDir) file.createDir(outputDir)
for (const category of [...db.categories.all(), { id: 'other' }]) { for (const category of [...db.categories.all(), { id: 'other' }]) {
const filename = `${outputDir}/${category.id}.m3u` const filename = `${outputDir}/${category.id}.m3u`
utils.createFile(filename, '#EXTM3U\n') file.create(filename, '#EXTM3U\n')
const channels = db.channels const channels = db.channels
.sortBy(['name', 'url']) .sortBy(['name', 'url'])
@ -118,19 +123,19 @@ function generateCategories() {
.removeDuplicates() .removeDuplicates()
.get() .get()
for (const channel of channels) { for (const channel of channels) {
utils.appendToFile(filename, channel.toString()) file.append(filename, channel.toString())
} }
} }
} }
function generateCountries() { function generateCountries() {
console.log(`Generating /countries...`) log.print(`Generating /countries...\n`)
const outputDir = `${ROOT_DIR}/countries` const outputDir = `${ROOT_DIR}/countries`
utils.createDir(outputDir) file.createDir(outputDir)
for (const country of [...db.countries.all(), { code: 'undefined' }]) { for (const country of [...db.countries.all(), { code: 'undefined' }]) {
const filename = `${outputDir}/${country.code}.m3u` const filename = `${outputDir}/${country.code}.m3u`
utils.createFile(filename, '#EXTM3U\n') file.create(filename, '#EXTM3U\n')
const channels = db.channels const channels = db.channels
.sortBy(['name', 'url']) .sortBy(['name', 'url'])
@ -139,20 +144,20 @@ function generateCountries() {
.get() .get()
for (const channel of channels) { for (const channel of channels) {
if (!channel.isNSFW()) { if (!channel.isNSFW()) {
utils.appendToFile(filename, channel.toString()) file.append(filename, channel.toString())
} }
} }
} }
} }
function generateLanguages() { function generateLanguages() {
console.log(`Generating /languages...`) log.print(`Generating /languages...\n`)
const outputDir = `${ROOT_DIR}/languages` const outputDir = `${ROOT_DIR}/languages`
utils.createDir(outputDir) file.createDir(outputDir)
for (const language of [...db.languages.all(), { code: 'undefined' }]) { for (const language of [...db.languages.all(), { code: 'undefined' }]) {
const filename = `${outputDir}/${language.code}.m3u` const filename = `${outputDir}/${language.code}.m3u`
utils.createFile(filename, '#EXTM3U\n') file.create(filename, '#EXTM3U\n')
const channels = db.channels const channels = db.channels
.sortBy(['name', 'url']) .sortBy(['name', 'url'])
@ -161,25 +166,25 @@ function generateLanguages() {
.get() .get()
for (const channel of channels) { for (const channel of channels) {
if (!channel.isNSFW()) { if (!channel.isNSFW()) {
utils.appendToFile(filename, channel.toString()) file.append(filename, channel.toString())
} }
} }
} }
} }
function generateChannelsJson() { function generateChannelsJson() {
console.log('Generating channels.json...') log.print('Generating channels.json...\n')
const filename = `${ROOT_DIR}/channels.json` const filename = `${ROOT_DIR}/channels.json`
const channels = db.channels const channels = db.channels
.sortBy(['name', 'url']) .sortBy(['name', 'url'])
.get() .get()
.map(c => c.toObject()) .map(c => c.toObject())
utils.createFile(filename, JSON.stringify(channels)) file.create(filename, JSON.stringify(channels))
} }
function finish() { function showResults() {
console.log( log.print(
`\nTotal: ${db.channels.count()} channels, ${db.countries.count()} countries, ${db.languages.count()} languages, ${db.categories.count()} categories.` `Total: ${db.channels.count()} channels, ${db.countries.count()} countries, ${db.languages.count()} languages, ${db.categories.count()} categories.\n`
) )
} }