iptv/scripts/generate.js

233 lines
6.7 KiB
JavaScript
Raw Normal View History

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