iptv/scripts/generate.js

231 lines
6.3 KiB
JavaScript
Raw Normal View History

2021-01-30 02:26:23 +03:00
const utils = require('./utils')
const parser = require('./parser')
2019-11-02 13:26:21 +03:00
const ROOT_DIR = './.gh-pages'
2019-11-02 00:08:27 +03:00
let list = {
all: [],
countries: {},
languages: {},
categories: {}
2019-07-20 10:03:31 +03:00
}
2019-11-02 00:27:05 +03:00
function main() {
parseIndex()
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-01-30 02:26:23 +03:00
console.log('Creating root directory...')
utils.createDir(ROOT_DIR)
2019-11-02 13:26:21 +03:00
}
2019-11-13 17:27:59 +03:00
function createNoJekyllFile() {
2021-01-30 02:26:23 +03:00
console.log('Creating .nojekyll...')
utils.createFile(`${ROOT_DIR}/.nojekyll`)
2019-11-13 17:27:59 +03:00
}
2019-11-02 00:08:27 +03:00
function parseIndex() {
2021-01-30 02:26:23 +03:00
console.log(`Parsing index...`)
const items = parser.parseIndex()
2021-01-30 06:18:21 +03:00
for (const category of utils.supportedCategories) {
list.categories[category.id] = []
}
list.categories['other'] = []
2021-01-30 02:26:23 +03:00
for (let item of items) {
const playlist = parser.parsePlaylist(item.url)
for (let channel of playlist.channels) {
2019-11-02 00:08:27 +03:00
// all
list.all.push(channel)
2019-11-02 00:08:27 +03:00
// country
2021-01-29 21:30:58 +03: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-02 00:08:27 +03:00
// language
2021-01-29 22:16:33 +03:00
if (!channel.languages.length) {
2020-05-04 16:23:36 +03:00
const languageCode = 'undefined'
2021-01-29 21:30:58 +03:00
if (!list.languages[languageCode]) {
list.languages[languageCode] = []
}
2021-01-29 21:30:58 +03:00
list.languages[languageCode].push(channel)
2020-05-04 16:23:36 +03:00
} else {
2021-01-29 22:16:33 +03:00
for (let language of channel.languages) {
2020-05-04 16:23:36 +03:00
const languageCode = language.code || 'undefined'
2021-01-29 21:30:58 +03:00
if (!list.languages[languageCode]) {
list.languages[languageCode] = []
2020-05-04 16:23:36 +03:00
}
2021-01-29 21:30:58 +03:00
list.languages[languageCode].push(channel)
2020-05-04 16:23:36 +03:00
}
}
2019-11-02 00:08:27 +03:00
// category
2021-01-30 06:18:21 +03:00
const categoryId = channel.category.toLowerCase()
if (!list.categories[categoryId]) {
list.categories['other'].push(channel)
} else {
list.categories[categoryId].push(channel)
2019-10-31 15:21:08 +03:00
}
}
2019-11-02 00:08:27 +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-01-30 02:26:23 +03:00
const channels = utils.sortBy(list.all, ['name', 'url'])
2020-04-11 04:34:50 +03:00
for (let 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-01-30 02:26:23 +03:00
const sorted = utils.sortBy(list.all, ['name', 'url'])
const channels = utils.filterNSFW(sorted)
2021-01-27 18:54:08 +03:00
for (let 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-01-30 02:26:23 +03:00
const sorted = utils.sortBy(list.all, ['name', 'url'])
2020-05-04 16:23:36 +03:00
const channels = sorted.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-01-30 02:26:23 +03:00
const channels = utils.sortBy(list.all, ['tvgCountry', 'name', 'url'])
2020-04-11 04:34:50 +03:00
for (let channel of channels) {
2020-05-04 16:23:36 +03:00
const category = channel.category
2021-01-30 02:26:23 +03:00
channel.category = channel.tvgCountry
utils.appendToFile(filename, channel.toString())
2020-05-04 16:23:36 +03:00
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-01-30 02:26:23 +03:00
const channels = utils.sortBy(list.all, ['tvgLanguage', 'name', 'url'])
2020-04-11 04:34:50 +03:00
for (let channel of channels) {
2020-05-04 16:23:36 +03:00
const category = channel.category
2021-01-30 02:26:23 +03:00
channel.category = channel.tvgLanguage
utils.appendToFile(filename, channel.toString())
2020-05-04 16:23:36 +03:00
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-01-30 02:26:23 +03:00
const channels = utils.sortBy(list.all, ['category', 'name', 'url'])
2020-04-11 04:34:50 +03:00
for (let channel of channels) {
2021-01-30 02:26:23 +03:00
utils.appendToFile(filename, channel.toString())
}
}
2019-11-02 13:50:56 +03:00
function generateCountries() {
2021-01-30 02:26:23 +03:00
console.log('Generating /countries...')
2019-11-02 13:50:56 +03:00
const outputDir = `${ROOT_DIR}/countries`
2021-01-30 02:26:23 +03:00
utils.createDir(outputDir)
2019-11-02 13:50:56 +03:00
2021-01-30 06:18:21 +03:00
for (const countryId in list.countries) {
const filename = `${outputDir}/${countryId}.m3u`
2021-01-30 02:26:23 +03:00
utils.createFile(filename, '#EXTM3U\n')
2019-11-02 15:43:41 +03:00
2021-01-30 06:18:21 +03:00
let channels = Object.values(list.countries[countryId])
channels = utils.sortBy(channels, ['name', 'url'])
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-29 21:30:58 +03:00
function generateLanguages() {
2021-01-30 02:26:23 +03:00
console.log('Generating /languages...')
2021-01-29 21:30:58 +03:00
const outputDir = `${ROOT_DIR}/languages`
2021-01-30 02:26:23 +03:00
utils.createDir(outputDir)
2019-11-02 13:26:21 +03:00
2021-01-30 06:18:21 +03:00
for (const languageId in list.languages) {
const filename = `${outputDir}/${languageId}.m3u`
2021-01-30 02:26:23 +03:00
utils.createFile(filename, '#EXTM3U\n')
2019-11-02 15:43:41 +03:00
2021-01-30 06:18:21 +03:00
let channels = Object.values(list.languages[languageId])
channels = utils.sortBy(channels, ['name', 'url'])
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-01-29 21:30:58 +03:00
function generateCategories() {
2021-01-30 02:26:23 +03:00
console.log('Generating /categories...')
2021-01-29 21:30:58 +03:00
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-01-30 06:18:21 +03:00
for (const category of utils.supportedCategories) {
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-01-30 06:18:21 +03:00
let channels = Object.values(list.categories[category.id])
channels = utils.sortBy(channels, ['name', 'url'])
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-01-30 02:26:23 +03: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-02 00:08:27 +03:00
main()