iptv/scripts/generate.js

232 lines
6.3 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: [],
countries: {},
languages: {},
categories: {}
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)
2019-11-01 22:08:27 +01:00
// country
2021-01-29 19:30:58 +01:00
if (!channel.countries.length) {
const countryCode = 'undefined'
if (!list.countries[countryCode]) {
list.countries[countryCode] = []
}
list.countries[countryCode].push(channel)
} else {
for (let country of channel.countries) {
const countryCode = country.code || 'undefined'
if (!list.countries[countryCode]) {
list.countries[countryCode] = []
}
list.countries[countryCode].push(channel)
}
}
2019-11-01 22:08:27 +01:00
// language
2021-01-29 20:16:33 +01:00
if (!channel.languages.length) {
2020-05-04 15:23:36 +02:00
const languageCode = 'undefined'
2021-01-29 19:30:58 +01:00
if (!list.languages[languageCode]) {
list.languages[languageCode] = []
}
2021-01-29 19:30:58 +01:00
list.languages[languageCode].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
}
}
2019-11-01 22:08:27 +01:00
// category
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
}
}
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-01-30 00:26:23 +01:00
const channels = utils.sortBy(list.all, ['tvgCountry', '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.tvgCountry
utils.appendToFile(filename, channel.toString())
2020-05-04 15:23:36 +02:00
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-01-30 04:18:21 +01:00
for (const countryId in list.countries) {
const filename = `${outputDir}/${countryId}.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.countries[countryId])
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(
`Countries: ${Object.values(list.countries).length}. Languages: ${
Object.values(list.languages).length
}. Categories: ${Object.values(list.categories).length}. Channels: ${list.all.length}.`
)
}
2019-11-01 22:08:27 +01:00
main()