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