iptv/scripts/generate.js

266 lines
7.2 KiB
JavaScript
Raw Normal View History

2021-01-30 00:26:23 +01:00
const utils = require('./utils')
const parser = require('./parser')
2021-01-31 18:11:52 +01:00
const categories = require('./categories')
2019-11-02 11:26:21 +01:00
const ROOT_DIR = './.gh-pages'
2019-11-01 22:08:27 +01:00
let list = {
all: [],
2021-02-10 09:57:56 +01:00
playlists: {},
2019-11-01 22:08:27 +01:00
languages: {},
2021-02-10 09:57:56 +01:00
categories: {},
countries: {}
2019-07-20 09:03:31 +02:00
}
2019-11-01 22:27:05 +01:00
function main() {
parseIndex()
2019-11-03 18:25:33 +01:00
createRootDirectory()
2019-11-13 15:27:59 +01:00
createNoJekyllFile()
2019-11-02 11:50:56 +01:00
generateIndex()
2021-01-27 16:54:08 +01:00
generateSFWIndex()
2021-01-30 00:26:23 +01:00
generateChannelsJson()
2019-11-01 22:27:05 +01:00
generateCountryIndex()
generateLanguageIndex()
generateCategoryIndex()
2019-11-02 11:50:56 +01:00
generateCountries()
2019-11-01 22:27:05 +01:00
generateLanguages()
2021-01-30 00:26:23 +01:00
generateCategories()
finish()
2019-11-01 22:27:05 +01:00
}
2019-11-03 18:25:33 +01:00
function createRootDirectory() {
2021-01-30 00:26:23 +01:00
console.log('Creating root directory...')
utils.createDir(ROOT_DIR)
2019-11-02 11:26:21 +01:00
}
2019-11-13 15:27:59 +01:00
function createNoJekyllFile() {
2021-01-30 00:26:23 +01:00
console.log('Creating .nojekyll...')
utils.createFile(`${ROOT_DIR}/.nojekyll`)
2019-11-13 15:27:59 +01:00
}
2019-11-01 22:08:27 +01:00
function parseIndex() {
2021-01-30 00:26:23 +01:00
console.log(`Parsing index...`)
const items = parser.parseIndex()
2021-01-31 18:11:52 +01:00
for (const category of categories) {
2021-01-30 04:18:21 +01:00
list.categories[category.id] = []
}
list.categories['other'] = []
2021-01-30 00:26:23 +01:00
for (let item of items) {
const playlist = parser.parsePlaylist(item.url)
for (let channel of playlist.channels) {
2019-11-01 22:08:27 +01:00
// all
list.all.push(channel)
2021-02-10 09:57:56 +01:00
// playlists
2021-01-29 19:30:58 +01:00
if (!channel.countries.length) {
2021-02-10 09:57:56 +01:00
const UNDEFINED = 'undefined'
if (!list.playlists[UNDEFINED]) {
list.playlists[UNDEFINED] = []
2021-01-29 19:30:58 +01:00
}
2021-02-10 09:57:56 +01:00
list.playlists[UNDEFINED].push(channel)
2021-01-29 19:30:58 +01:00
} else {
for (let country of channel.countries) {
const countryCode = country.code || 'undefined'
2021-02-10 09:57:56 +01:00
if (!list.playlists[countryCode]) {
list.playlists[countryCode] = []
2021-01-29 19:30:58 +01:00
}
2021-02-10 09:57:56 +01:00
list.playlists[countryCode].push(channel)
2021-01-29 19:30:58 +01:00
}
}
2021-02-10 09:57:56 +01:00
// countries
if (!channel.countries.length) {
const UNDEFINED = 'undefined'
if (!list.countries[UNDEFINED]) {
list.countries[UNDEFINED] = []
}
list.countries[UNDEFINED].push(channel)
} else {
for (let country of channel.countries) {
const countryName = country.name
if (!list.countries[countryName]) {
list.countries[countryName] = []
}
list.countries[countryName].push(channel)
}
}
// languages
2021-01-29 20:16:33 +01:00
if (!channel.languages.length) {
2021-02-10 09:57:56 +01:00
const UNDEFINED = 'undefined'
if (!list.languages[UNDEFINED]) {
list.languages[UNDEFINED] = []
}
2021-02-10 09:57:56 +01:00
list.languages[UNDEFINED].push(channel)
2020-05-04 15:23:36 +02:00
} else {
2021-01-29 20:16:33 +01:00
for (let language of channel.languages) {
2020-05-04 15:23:36 +02:00
const languageCode = language.code || 'undefined'
2021-01-29 19:30:58 +01:00
if (!list.languages[languageCode]) {
list.languages[languageCode] = []
2020-05-04 15:23:36 +02:00
}
2021-01-29 19:30:58 +01:00
list.languages[languageCode].push(channel)
2020-05-04 15:23:36 +02:00
}
}
2021-02-10 09:57:56 +01:00
// categories
2021-01-30 04:18:21 +01:00
const categoryId = channel.category.toLowerCase()
if (!list.categories[categoryId]) {
list.categories['other'].push(channel)
} else {
list.categories[categoryId].push(channel)
2019-10-31 13:21:08 +01:00
}
}
2019-11-01 22:08:27 +01:00
}
2021-02-10 09:57:56 +01:00
list.countries = Object.keys(list.countries)
.sort((a, b) => {
if (a === 'undefined') return -1
if (b === 'undefined') return 1
if (a < b) return -1
if (a > b) return 1
return 0
})
.reduce((obj, key) => {
obj[key] = list.countries[key]
return obj
}, {})
2019-11-01 22:08:27 +01:00
}
2019-11-02 11:50:56 +01:00
function generateIndex() {
2021-01-30 00:26:23 +01:00
console.log('Generating index.m3u...')
2019-11-02 11:50:56 +01:00
const filename = `${ROOT_DIR}/index.m3u`
2021-01-30 00:26:23 +01:00
utils.createFile(filename, '#EXTM3U\n')
2019-11-02 11:50:56 +01:00
2021-01-30 00:26:23 +01:00
const channels = utils.sortBy(list.all, ['name', 'url'])
2020-04-11 03:34:50 +02:00
for (let channel of channels) {
2021-01-30 00:26:23 +01:00
utils.appendToFile(filename, channel.toString())
2019-11-02 11:50:56 +01:00
}
}
2021-01-27 16:54:08 +01:00
function generateSFWIndex() {
2021-01-30 00:26:23 +01:00
console.log('Generating index.sfw.m3u...')
2021-01-27 16:54:08 +01:00
const filename = `${ROOT_DIR}/index.sfw.m3u`
2021-01-30 00:26:23 +01:00
utils.createFile(filename, '#EXTM3U\n')
2021-01-27 16:54:08 +01:00
2021-01-30 00:26:23 +01:00
const sorted = utils.sortBy(list.all, ['name', 'url'])
const channels = utils.filterNSFW(sorted)
2021-01-27 16:54:08 +01:00
for (let channel of channels) {
2021-01-30 00:26:23 +01:00
utils.appendToFile(filename, channel.toString())
2021-01-27 16:54:08 +01:00
}
}
2021-01-30 00:26:23 +01:00
function generateChannelsJson() {
console.log('Generating channels.json...')
2020-05-04 15:23:36 +02:00
const filename = `${ROOT_DIR}/channels.json`
2021-01-30 00:26:23 +01:00
const sorted = utils.sortBy(list.all, ['name', 'url'])
2020-05-04 15:23:36 +02:00
const channels = sorted.map(c => c.toJSON())
2021-01-30 00:26:23 +01:00
utils.createFile(filename, JSON.stringify(channels))
2020-05-04 15:23:36 +02:00
}
2019-11-01 22:08:27 +01:00
function generateCountryIndex() {
2021-01-30 00:26:23 +01:00
console.log('Generating index.country.m3u...')
2019-11-02 11:26:21 +01:00
const filename = `${ROOT_DIR}/index.country.m3u`
2021-01-30 00:26:23 +01:00
utils.createFile(filename, '#EXTM3U\n')
2019-11-01 22:08:27 +01:00
2021-02-10 09:57:56 +01:00
for (let country in list.countries) {
const channels = utils.sortBy(list.countries[country], ['name', 'url'])
for (let channel of channels) {
const category = channel.category
channel.category = country !== 'undefined' ? country : ''
utils.appendToFile(filename, channel.toString())
channel.category = category
}
}
2019-11-01 22:08:27 +01:00
}
2019-11-01 22:08:27 +01:00
function generateLanguageIndex() {
2021-01-30 00:26:23 +01:00
console.log('Generating index.language.m3u...')
2019-11-02 11:26:21 +01:00
const filename = `${ROOT_DIR}/index.language.m3u`
2021-01-30 00:26:23 +01:00
utils.createFile(filename, '#EXTM3U\n')
2019-11-01 22:08:27 +01:00
2021-01-30 00:26:23 +01:00
const channels = utils.sortBy(list.all, ['tvgLanguage', 'name', 'url'])
2020-04-11 03:34:50 +02:00
for (let channel of channels) {
2020-05-04 15:23:36 +02:00
const category = channel.category
2021-01-30 00:26:23 +01:00
channel.category = channel.tvgLanguage
utils.appendToFile(filename, channel.toString())
2020-05-04 15:23:36 +02:00
channel.category = category
}
2019-11-01 22:08:27 +01:00
}
function generateCategoryIndex() {
2021-01-30 00:26:23 +01:00
console.log('Generating index.category.m3u...')
const filename = `${ROOT_DIR}/index.category.m3u`
2021-01-30 00:26:23 +01:00
utils.createFile(filename, '#EXTM3U\n')
2019-11-01 22:08:27 +01:00
2021-01-30 00:26:23 +01:00
const channels = utils.sortBy(list.all, ['category', 'name', 'url'])
2020-04-11 03:34:50 +02:00
for (let channel of channels) {
2021-01-30 00:26:23 +01:00
utils.appendToFile(filename, channel.toString())
}
}
2019-11-02 11:50:56 +01:00
function generateCountries() {
2021-01-30 00:26:23 +01:00
console.log('Generating /countries...')
2019-11-02 11:50:56 +01:00
const outputDir = `${ROOT_DIR}/countries`
2021-01-30 00:26:23 +01:00
utils.createDir(outputDir)
2019-11-02 11:50:56 +01:00
2021-02-10 09:57:56 +01:00
for (const code in list.playlists) {
const filename = `${outputDir}/${code}.m3u`
2021-01-30 00:26:23 +01:00
utils.createFile(filename, '#EXTM3U\n')
2019-11-02 13:43:41 +01:00
2021-02-10 09:57:56 +01:00
let channels = Object.values(list.playlists[code])
2021-01-30 04:18:21 +01:00
channels = utils.sortBy(channels, ['name', 'url'])
for (const channel of channels) {
2021-01-30 00:26:23 +01:00
utils.appendToFile(filename, channel.toString())
2019-11-02 11:50:56 +01:00
}
}
}
2021-01-29 19:30:58 +01:00
function generateLanguages() {
2021-01-30 00:26:23 +01:00
console.log('Generating /languages...')
2021-01-29 19:30:58 +01:00
const outputDir = `${ROOT_DIR}/languages`
2021-01-30 00:26:23 +01:00
utils.createDir(outputDir)
2019-11-02 11:26:21 +01:00
2021-01-30 04:18:21 +01:00
for (const languageId in list.languages) {
const filename = `${outputDir}/${languageId}.m3u`
2021-01-30 00:26:23 +01:00
utils.createFile(filename, '#EXTM3U\n')
2019-11-02 13:43:41 +01:00
2021-01-30 04:18:21 +01:00
let channels = Object.values(list.languages[languageId])
channels = utils.sortBy(channels, ['name', 'url'])
for (const channel of channels) {
2021-01-30 00:26:23 +01:00
utils.appendToFile(filename, channel.toString())
2019-11-01 22:21:08 +01:00
}
}
}
2021-01-29 19:30:58 +01:00
function generateCategories() {
2021-01-30 00:26:23 +01:00
console.log('Generating /categories...')
2021-01-29 19:30:58 +01:00
const outputDir = `${ROOT_DIR}/categories`
2021-01-30 00:26:23 +01:00
utils.createDir(outputDir)
2019-11-02 11:50:56 +01:00
2021-01-31 18:11:52 +01:00
for (const category of categories) {
2021-01-30 04:18:21 +01:00
const filename = `${outputDir}/${category.id}.m3u`
2021-01-30 00:26:23 +01:00
utils.createFile(filename, '#EXTM3U\n')
2019-11-02 13:43:41 +01:00
2021-01-30 04:18:21 +01:00
let channels = Object.values(list.categories[category.id])
channels = utils.sortBy(channels, ['name', 'url'])
for (const channel of channels) {
2021-01-30 00:26:23 +01:00
utils.appendToFile(filename, channel.toString())
2019-11-01 22:21:08 +01:00
}
}
}
2021-01-30 00:26:23 +01:00
function finish() {
console.log('Done.\n')
console.log(
2021-02-10 09:59:50 +01:00
`Countries: ${Object.keys(list.countries).length - 1}. Languages: ${
2021-01-30 00:26:23 +01:00
Object.values(list.languages).length
}. Categories: ${Object.values(list.categories).length}. Channels: ${list.all.length}.`
)
}
2019-11-01 22:08:27 +01:00
main()