iptv/scripts/generate.js

201 lines
5.2 KiB
JavaScript
Raw Normal View History

2021-08-01 20:31:49 +03:00
const file = require('./helpers/file')
const log = require('./helpers/log')
const db = require('./helpers/db')
2019-11-02 13:26:21 +03:00
const ROOT_DIR = './.gh-pages'
2021-08-01 20:31:49 +03:00
async function main() {
await loadDatabase()
2019-11-03 20:25:33 +03:00
createRootDirectory()
2019-11-13 17:27:59 +03:00
createNoJekyllFile()
2021-08-03 03:39:16 +03:00
generateIndex()
generateCategoryIndex()
2019-11-02 00:27:05 +03:00
generateCountryIndex()
generateLanguageIndex()
2021-08-03 03:39:16 +03:00
generateCategories()
generateCountries()
generateLanguages()
generateChannelsJson()
showResults()
2021-08-01 20:31:49 +03:00
}
async function loadDatabase() {
log.print('Loading database...\n')
await db.load()
2019-11-02 00:27:05 +03:00
}
2019-11-03 20:25:33 +03:00
function createRootDirectory() {
2021-08-01 20:31:49 +03:00
log.print('Creating .gh-pages folder...\n')
file.createDir(ROOT_DIR)
2019-11-02 13:26:21 +03:00
}
2019-11-13 17:27:59 +03:00
function createNoJekyllFile() {
2021-08-01 20:31:49 +03:00
log.print('Creating .nojekyll...\n')
file.create(`${ROOT_DIR}/.nojekyll`)
2019-11-13 17:27:59 +03:00
}
2019-11-02 13:50:56 +03:00
function generateIndex() {
2021-08-01 20:31:49 +03:00
log.print('Generating index.m3u...\n')
2019-11-02 13:50:56 +03:00
const filename = `${ROOT_DIR}/index.m3u`
2021-08-01 20:31:49 +03:00
file.create(filename, '#EXTM3U\n')
2019-11-02 13:50:56 +03:00
2021-06-05 17:13:09 +03:00
const nsfwFilename = `${ROOT_DIR}/index.nsfw.m3u`
2021-08-01 20:31:49 +03:00
file.create(nsfwFilename, '#EXTM3U\n')
2021-05-07 18:34:14 +03:00
2021-08-06 07:49:43 +03:00
const channels = db.channels.sortBy(['name', 'url']).removeDuplicates().removeOffline().get()
2021-02-10 17:10:30 +03:00
for (const channel of channels) {
2021-06-05 17:13:09 +03:00
if (!channel.isNSFW()) {
2021-08-01 20:31:49 +03:00
file.append(filename, channel.toString())
}
2021-08-01 20:31:49 +03:00
file.append(nsfwFilename, channel.toString())
2019-11-02 13:50:56 +03:00
}
}
2021-05-07 18:34:14 +03:00
function generateCategoryIndex() {
2021-08-01 20:31:49 +03:00
log.print('Generating index.category.m3u...\n')
2021-05-07 18:34:14 +03:00
const filename = `${ROOT_DIR}/index.category.m3u`
2021-08-01 20:31:49 +03:00
file.create(filename, '#EXTM3U\n')
2021-01-27 18:54:08 +03:00
2021-08-06 07:49:43 +03:00
const channels = db.channels
.sortBy(['category', 'name', 'url'])
.removeDuplicates()
.removeOffline()
.get()
2021-02-10 17:10:30 +03:00
for (const channel of channels) {
2021-08-01 20:31:49 +03:00
file.append(filename, channel.toString())
2021-01-27 18:54:08 +03:00
}
}
2019-11-02 00:08:27 +03:00
function generateCountryIndex() {
2021-08-01 20:31:49 +03:00
log.print('Generating index.country.m3u...\n')
2019-11-02 13:26:21 +03:00
const filename = `${ROOT_DIR}/index.country.m3u`
2021-08-01 20:31:49 +03:00
file.create(filename, '#EXTM3U\n')
2019-11-02 00:08:27 +03:00
2021-05-10 16:25:26 +03:00
for (const country of [{ code: 'undefined' }, ...db.countries.sortBy(['name']).all()]) {
const channels = db.channels
.sortBy(['name', 'url'])
.forCountry(country)
.removeDuplicates()
2021-08-06 07:49:43 +03:00
.removeOffline()
2021-05-10 16:25:26 +03:00
.get()
for (const channel of channels) {
2021-08-03 02:30:26 +03:00
const groupTitle = channel.group.title
2021-06-05 17:13:09 +03:00
const nsfw = channel.isNSFW()
2021-08-03 02:30:26 +03:00
channel.group.title = country.name || ''
2021-06-05 17:13:09 +03:00
if (!nsfw) {
2021-08-01 20:31:49 +03:00
file.append(filename, channel.toString())
}
2021-08-03 02:30:26 +03:00
channel.group.title = groupTitle
2021-02-10 11:57:56 +03:00
}
}
2019-11-02 00:08:27 +03:00
}
2019-11-02 00:08:27 +03:00
function generateLanguageIndex() {
2021-08-01 20:31:49 +03:00
log.print('Generating index.language.m3u...\n')
2019-11-02 13:26:21 +03:00
const filename = `${ROOT_DIR}/index.language.m3u`
2021-08-01 20:31:49 +03:00
file.create(filename, '#EXTM3U\n')
2019-11-02 00:08:27 +03:00
2021-05-10 16:25:26 +03:00
for (const language of [{ code: 'undefined' }, ...db.languages.sortBy(['name']).all()]) {
const channels = db.channels
.sortBy(['name', 'url'])
.forLanguage(language)
.removeDuplicates()
2021-08-06 07:49:43 +03:00
.removeOffline()
2021-05-10 16:25:26 +03:00
.get()
2021-02-10 17:03:47 +03:00
for (const channel of channels) {
2021-08-03 02:30:26 +03:00
const groupTitle = channel.group.title
2021-06-05 17:13:09 +03:00
const nsfw = channel.isNSFW()
2021-08-03 02:30:26 +03:00
channel.group.title = language.name || ''
2021-06-05 17:13:09 +03:00
if (!nsfw) {
2021-08-01 20:31:49 +03:00
file.append(filename, channel.toString())
}
2021-08-03 02:30:26 +03:00
channel.group.title = groupTitle
2021-02-10 17:03:47 +03:00
}
}
2019-11-02 00:08:27 +03:00
}
2021-02-10 17:03:47 +03:00
function generateCategories() {
2021-08-01 20:31:49 +03:00
log.print(`Generating /categories...\n`)
2021-02-10 17:03:47 +03:00
const outputDir = `${ROOT_DIR}/categories`
2021-08-01 20:31:49 +03:00
file.createDir(outputDir)
2019-11-02 13:50:56 +03:00
2021-05-07 18:34:14 +03:00
for (const category of [...db.categories.all(), { id: 'other' }]) {
2021-02-10 17:03:47 +03:00
const filename = `${outputDir}/${category.id}.m3u`
2021-08-01 20:31:49 +03:00
file.create(filename, '#EXTM3U\n')
2019-11-02 15:43:41 +03:00
2021-05-10 16:25:26 +03:00
const channels = db.channels
.sortBy(['name', 'url'])
.forCategory(category)
.removeDuplicates()
2021-08-06 07:49:43 +03:00
.removeOffline()
2021-05-10 16:25:26 +03:00
.get()
2021-01-30 06:18:21 +03:00
for (const channel of channels) {
2021-08-01 20:31:49 +03:00
file.append(filename, channel.toString())
2019-11-02 13:50:56 +03:00
}
}
}
2021-02-10 17:03:47 +03:00
function generateCountries() {
2021-08-01 20:31:49 +03:00
log.print(`Generating /countries...\n`)
2021-02-10 17:03:47 +03:00
const outputDir = `${ROOT_DIR}/countries`
2021-08-01 20:31:49 +03:00
file.createDir(outputDir)
2019-11-02 13:26:21 +03:00
2021-05-07 18:34:14 +03:00
for (const country of [...db.countries.all(), { code: 'undefined' }]) {
2021-02-10 17:03:47 +03:00
const filename = `${outputDir}/${country.code}.m3u`
2021-08-01 20:31:49 +03:00
file.create(filename, '#EXTM3U\n')
2019-11-02 15:43:41 +03:00
2021-05-10 16:25:26 +03:00
const channels = db.channels
.sortBy(['name', 'url'])
.forCountry(country)
.removeDuplicates()
2021-08-06 07:49:43 +03:00
.removeOffline()
2021-05-10 16:25:26 +03:00
.get()
2021-01-30 06:18:21 +03:00
for (const channel of channels) {
2021-06-05 17:13:09 +03:00
if (!channel.isNSFW()) {
2021-08-01 20:31:49 +03:00
file.append(filename, channel.toString())
}
2019-11-02 00:21:08 +03:00
}
}
}
2021-02-10 17:03:47 +03:00
function generateLanguages() {
2021-08-01 20:31:49 +03:00
log.print(`Generating /languages...\n`)
2021-02-10 17:03:47 +03:00
const outputDir = `${ROOT_DIR}/languages`
2021-08-01 20:31:49 +03:00
file.createDir(outputDir)
2019-11-02 13:50:56 +03:00
2021-05-07 18:34:14 +03:00
for (const language of [...db.languages.all(), { code: 'undefined' }]) {
2021-02-10 17:03:47 +03:00
const filename = `${outputDir}/${language.code}.m3u`
2021-08-01 20:31:49 +03:00
file.create(filename, '#EXTM3U\n')
2019-11-02 15:43:41 +03:00
2021-05-10 16:25:26 +03:00
const channels = db.channels
.sortBy(['name', 'url'])
.forLanguage(language)
.removeDuplicates()
2021-08-06 07:49:43 +03:00
.removeOffline()
2021-05-10 16:25:26 +03:00
.get()
2021-01-30 06:18:21 +03:00
for (const channel of channels) {
2021-06-05 17:13:09 +03:00
if (!channel.isNSFW()) {
2021-08-01 20:31:49 +03:00
file.append(filename, channel.toString())
}
2019-11-02 00:21:08 +03:00
}
}
2021-05-07 18:34:14 +03:00
}
2021-02-10 17:14:43 +03:00
2021-05-07 18:34:14 +03:00
function generateChannelsJson() {
2021-08-01 20:31:49 +03:00
log.print('Generating channels.json...\n')
2021-05-07 18:34:14 +03:00
const filename = `${ROOT_DIR}/channels.json`
const channels = db.channels
.sortBy(['name', 'url'])
2021-05-10 16:25:26 +03:00
.get()
2021-05-08 14:53:42 +03:00
.map(c => c.toObject())
2021-08-01 20:31:49 +03:00
file.create(filename, JSON.stringify(channels))
2019-11-02 00:21:08 +03:00
}
2021-08-01 20:31:49 +03:00
function showResults() {
log.print(
`Total: ${db.channels.count()} channels, ${db.countries.count()} countries, ${db.languages.count()} languages, ${db.categories.count()} categories.\n`
2021-01-30 02:26:23 +03:00
)
}
2019-11-02 00:08:27 +03:00
main()