iptv/helpers/format.js

93 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-07-20 09:03:31 +02:00
const util = require('./util')
2019-04-30 15:52:38 +02:00
2019-08-07 14:40:58 +02:00
const debug = true
const verbal = false
let stats = {
countries: 0,
channels: 0,
updated: 0
}
let buffer = {}
2019-07-20 09:06:29 +02:00
2019-08-07 14:40:58 +02:00
async function main() {
console.log('Parsing index.m3u...')
const playlist = util.parsePlaylist('index.m3u')
let countries = playlist.items
2019-07-20 09:06:29 +02:00
if(debug) {
2019-08-07 14:40:58 +02:00
console.log('Debug mode is turn on')
countries = countries.slice(0, 1)
// countries = [{ url: 'channels/ru.m3u' }]
2019-07-20 09:06:29 +02:00
}
2019-04-30 15:52:38 +02:00
for(let country of countries) {
2019-08-07 14:40:58 +02:00
console.log(`Parsing ${country.url}...`)
const playlist = util.parsePlaylist(country.url)
console.log(`Creating channels list...`)
let channels = []
for(let item of playlist.items) {
let channel = util.createChannel({
id: item.inf['tvg-id'],
name: item.inf['tvg-name'],
logo: item.inf['tvg-logo'],
group: item.inf['group-title'],
url: item.url,
title: item.inf.title
})
2019-04-30 15:52:38 +02:00
channels.push(channel)
2019-08-07 14:40:58 +02:00
}
2019-04-30 15:52:38 +02:00
2019-08-07 14:40:58 +02:00
const epgUrl = playlist.attrs['x-tvg-url']
if(epgUrl && !buffer[epgUrl]) {
console.log(`Loading ${epgUrl}...`)
const epg = await util.loadEPG(epgUrl)
console.log(`Adding ${epgUrl} to buffer...`)
buffer[epgUrl] = epg
2019-04-30 15:52:38 +02:00
}
2019-08-07 14:40:58 +02:00
console.log(`Fills in missing channel's data...`)
for(let channel of channels) {
let channelId = channel.id
if(!channelId) continue
let c = buffer[epgUrl].channels[channelId]
if(!c) continue
let updated = false
if(!channel.name && c.names[0]) {
channel.name = c.names[0]
updated = true
if(verbal) {
console.log(`Added name '${c.names[0]}' to '${channel.id}'`)
}
}
if(!channel.logo && c.icon) {
channel.logo = c.icon
updated = true
if(verbal) {
console.log(`Added logo '${c.icon}' to '${channel.id}'`)
}
}
if(updated) {
stats.updated++
}
}
console.log(`Sorting channels...`)
2019-07-20 09:03:31 +02:00
channels = util.sortByTitle(channels)
2019-04-30 15:52:38 +02:00
2019-08-07 14:40:58 +02:00
console.log(`Writing result to file...`)
util.createFile(country.url, playlist.getHeader())
2019-04-30 15:52:38 +02:00
channels.forEach(channel => {
2019-08-07 14:40:58 +02:00
util.appendToFile(country.url, channel.toString())
2019-07-20 09:03:31 +02:00
})
2019-04-30 15:52:38 +02:00
2019-08-07 14:40:58 +02:00
stats.countries++
stats.channels += channels.length
2019-04-30 15:52:38 +02:00
}
2019-08-07 14:40:58 +02:00
console.log(`Countries: ${stats.countries}. Channels: ${stats.channels}. Updated: ${stats.updated}.`)
}
2019-04-30 15:52:38 +02:00
2019-08-07 14:40:58 +02:00
main()