Files
iptv/scripts/generate.js

204 lines
5.9 KiB
JavaScript
Raw Normal View History

2021-02-10 16:39:40 +03:00
const db = require('./db')
2021-01-30 02:26:23 +03:00
const utils = require('./utils')
2019-11-02 13:26:21 +03:00
const ROOT_DIR = './.gh-pages'
2021-02-10 16:39:40 +03:00
db.load()
2019-11-02 00:27:05 +03:00
function main() {
2019-11-03 20:25:33 +03:00
createRootDirectory()
2019-11-13 17:27:59 +03:00
createNoJekyllFile()
2019-11-02 13:50:56 +03:00
generateIndex()
2021-01-27 18:54:08 +03:00
generateSFWIndex()
2021-01-30 02:26:23 +03:00
generateChannelsJson()
2019-11-02 00:27:05 +03:00
generateCountryIndex()
generateLanguageIndex()
generateCategoryIndex()
2019-11-02 13:50:56 +03:00
generateCountries()
2019-11-02 00:27:05 +03:00
generateLanguages()
2021-01-30 02:26:23 +03:00
generateCategories()
finish()
2019-11-02 00:27:05 +03:00
}
2019-11-03 20:25:33 +03:00
function createRootDirectory() {
2021-02-10 16:39:40 +03:00
console.log('Creating .gh-pages folder...')
2021-01-30 02:26:23 +03:00
utils.createDir(ROOT_DIR)
2019-11-02 13:26:21 +03:00
}
2019-11-13 17:27:59 +03:00
function createNoJekyllFile() {
2021-02-10 17:03:47 +03:00
console.log('Creating .nojekyll file...')
2021-01-30 02:26:23 +03:00
utils.createFile(`${ROOT_DIR}/.nojekyll`)
2019-11-13 17:27:59 +03:00
}
2019-11-02 13:50:56 +03:00
function generateIndex() {
2021-01-30 02:26:23 +03:00
console.log('Generating index.m3u...')
2019-11-02 13:50:56 +03:00
const filename = `${ROOT_DIR}/index.m3u`
2021-01-30 02:26:23 +03:00
utils.createFile(filename, '#EXTM3U\n')
2019-11-02 13:50:56 +03:00
2021-02-10 17:10:30 +03:00
const channels = db.channels.sortBy(['name', 'url']).all()
for (const channel of channels) {
2021-01-30 02:26:23 +03:00
utils.appendToFile(filename, channel.toString())
2019-11-02 13:50:56 +03:00
}
}
2021-01-27 18:54:08 +03:00
function generateSFWIndex() {
2021-01-30 02:26:23 +03:00
console.log('Generating index.sfw.m3u...')
2021-01-27 18:54:08 +03:00
const filename = `${ROOT_DIR}/index.sfw.m3u`
2021-01-30 02:26:23 +03:00
utils.createFile(filename, '#EXTM3U\n')
2021-01-27 18:54:08 +03:00
2021-02-10 17:10:30 +03:00
const channels = db.channels.sortBy(['name', 'url']).sfw()
for (const channel of channels) {
2021-01-30 02:26:23 +03:00
utils.appendToFile(filename, channel.toString())
2021-01-27 18:54:08 +03:00
}
}
2021-01-30 02:26:23 +03:00
function generateChannelsJson() {
console.log('Generating channels.json...')
2020-05-04 16:23:36 +03:00
const filename = `${ROOT_DIR}/channels.json`
2021-02-10 16:39:40 +03:00
const channels = db.channels
.sortBy(['name', 'url'])
.all()
.map(c => c.toJSON())
2021-01-30 02:26:23 +03:00
utils.createFile(filename, JSON.stringify(channels))
2020-05-04 16:23:36 +03:00
}
2019-11-02 00:08:27 +03:00
function generateCountryIndex() {
2021-01-30 02:26:23 +03:00
console.log('Generating index.country.m3u...')
2019-11-02 13:26:21 +03:00
const filename = `${ROOT_DIR}/index.country.m3u`
2021-01-30 02:26:23 +03:00
utils.createFile(filename, '#EXTM3U\n')
2019-11-02 00:08:27 +03:00
2021-02-10 16:39:40 +03:00
const channels = db.channels.sortBy(['name', 'url']).forCountry({ code: null })
for (const channel of channels) {
const category = channel.category
channel.category = ''
utils.appendToFile(filename, channel.toString())
channel.category = category
}
const countries = db.countries.sortBy(['name']).all()
for (const country of countries) {
const channels = db.channels.sortBy(['name', 'url']).forCountry(country)
for (const channel of channels) {
2021-02-10 11:57:56 +03:00
const category = channel.category
2021-02-10 16:39:40 +03:00
channel.category = country.name
2021-02-10 11:57:56 +03:00
utils.appendToFile(filename, channel.toString())
channel.category = category
}
}
2019-11-02 00:08:27 +03:00
}
2019-11-02 00:08:27 +03:00
function generateLanguageIndex() {
2021-01-30 02:26:23 +03:00
console.log('Generating index.language.m3u...')
2019-11-02 13:26:21 +03:00
const filename = `${ROOT_DIR}/index.language.m3u`
2021-01-30 02:26:23 +03:00
utils.createFile(filename, '#EXTM3U\n')
2019-11-02 00:08:27 +03:00
2021-02-10 17:03:47 +03:00
const channels = db.channels.sortBy(['name', 'url']).forLanguage({ code: null })
2021-02-10 16:39:40 +03:00
for (const channel of channels) {
2020-05-04 16:23:36 +03:00
const category = channel.category
2021-02-10 17:03:47 +03:00
channel.category = ''
2021-01-30 02:26:23 +03:00
utils.appendToFile(filename, channel.toString())
2020-05-04 16:23:36 +03:00
channel.category = category
}
2021-02-10 17:03:47 +03:00
const languages = db.languages.sortBy(['name']).all()
for (const language of languages) {
const channels = db.channels.sortBy(['name', 'url']).forLanguage(language)
for (const channel of channels) {
const category = channel.category
channel.category = language.name
utils.appendToFile(filename, channel.toString())
channel.category = category
}
}
2019-11-02 00:08:27 +03:00
}
function generateCategoryIndex() {
2021-01-30 02:26:23 +03:00
console.log('Generating index.category.m3u...')
const filename = `${ROOT_DIR}/index.category.m3u`
2021-01-30 02:26:23 +03:00
utils.createFile(filename, '#EXTM3U\n')
2019-11-02 00:08:27 +03:00
2021-02-10 16:39:40 +03:00
const channels = db.channels.sortBy(['category', 'name', 'url']).all()
for (const channel of channels) {
2021-01-30 02:26:23 +03:00
utils.appendToFile(filename, channel.toString())
}
}
2021-02-10 17:03:47 +03:00
function generateCategories() {
console.log('Generating a playlist for each category...')
const outputDir = `${ROOT_DIR}/categories`
2021-01-30 02:26:23 +03:00
utils.createDir(outputDir)
2019-11-02 13:50:56 +03:00
2021-02-10 17:03:47 +03:00
for (const category of db.categories.all()) {
const filename = `${outputDir}/${category.id}.m3u`
2021-01-30 02:26:23 +03:00
utils.createFile(filename, '#EXTM3U\n')
2019-11-02 15:43:41 +03:00
2021-02-10 17:03:47 +03:00
const channels = db.channels.sortBy(['name', 'url']).forCategory(category)
2021-01-30 06:18:21 +03:00
for (const channel of channels) {
2021-01-30 02:26:23 +03:00
utils.appendToFile(filename, channel.toString())
2019-11-02 13:50:56 +03:00
}
}
2021-02-10 17:03:47 +03:00
const other = `${outputDir}/other.m3u`
const channels = db.channels.sortBy(['name', 'url']).forCategory({ id: null })
utils.createFile(other, '#EXTM3U\n')
for (const channel of channels) {
utils.appendToFile(other, channel.toString())
}
2019-11-02 13:50:56 +03:00
}
2021-02-10 17:03:47 +03:00
function generateCountries() {
console.log('Generating a playlist for each country...')
const outputDir = `${ROOT_DIR}/countries`
2021-01-30 02:26:23 +03:00
utils.createDir(outputDir)
2019-11-02 13:26:21 +03:00
2021-02-10 17:03:47 +03:00
for (const country of db.countries.all()) {
const filename = `${outputDir}/${country.code}.m3u`
2021-01-30 02:26:23 +03:00
utils.createFile(filename, '#EXTM3U\n')
2019-11-02 15:43:41 +03:00
2021-02-10 17:03:47 +03:00
const channels = db.channels.sortBy(['name', 'url']).forCountry(country)
2021-01-30 06:18:21 +03:00
for (const channel of channels) {
2021-01-30 02:26:23 +03:00
utils.appendToFile(filename, channel.toString())
2019-11-02 00:21:08 +03:00
}
}
2021-02-10 17:14:43 +03:00
const other = `${outputDir}/undefined.m3u`
const channels = db.channels.sortBy(['name', 'url']).forCountry({ code: null })
utils.createFile(other, '#EXTM3U\n')
for (const channel of channels) {
utils.appendToFile(other, channel.toString())
}
2019-11-02 00:21:08 +03:00
}
2021-02-10 17:03:47 +03:00
function generateLanguages() {
console.log('Generating a playlist for each language...')
const outputDir = `${ROOT_DIR}/languages`
2021-01-30 02:26:23 +03:00
utils.createDir(outputDir)
2019-11-02 13:50:56 +03:00
2021-02-10 17:03:47 +03:00
for (const language of db.languages.all()) {
const filename = `${outputDir}/${language.code}.m3u`
2021-01-30 02:26:23 +03:00
utils.createFile(filename, '#EXTM3U\n')
2019-11-02 15:43:41 +03:00
2021-02-10 17:03:47 +03:00
const channels = db.channels.sortBy(['name', 'url']).forLanguage(language)
2021-01-30 06:18:21 +03:00
for (const channel of channels) {
2021-01-30 02:26:23 +03:00
utils.appendToFile(filename, channel.toString())
2019-11-02 00:21:08 +03:00
}
}
2021-02-10 17:14:43 +03:00
const other = `${outputDir}/undefined.m3u`
const channels = db.channels.sortBy(['name', 'url']).forLanguage({ code: null })
utils.createFile(other, '#EXTM3U\n')
for (const channel of channels) {
utils.appendToFile(other, channel.toString())
}
2019-11-02 00:21:08 +03:00
}
2021-01-30 02:26:23 +03:00
function finish() {
console.log(
2021-02-10 16:39:40 +03:00
`Countries: ${db.countries.count()}. Languages: ${db.languages.count()}. Categories: ${db.categories.count()}. Channels: ${db.channels.count()}.`
2021-01-30 02:26:23 +03:00
)
2021-02-10 16:39:40 +03:00
console.log('Done.')
2021-01-30 02:26:23 +03:00
}
2019-11-02 00:08:27 +03:00
main()