2019-07-20 09:03:31 +02:00
|
|
|
const util = require('./util')
|
2019-04-30 15:52:38 +02:00
|
|
|
|
2019-08-07 15:51:34 +02:00
|
|
|
const debug = false
|
2019-08-08 02:32:11 +02:00
|
|
|
|
2019-08-07 14:40:58 +02:00
|
|
|
let stats = {
|
2019-08-08 02:32:11 +02:00
|
|
|
total: 0,
|
2019-08-08 01:24:15 +02:00
|
|
|
updated: 0,
|
2019-08-08 02:32:11 +02:00
|
|
|
duplicates: 0,
|
2019-08-09 23:58:11 +02:00
|
|
|
unvalid: 0
|
2019-08-07 14:40:58 +02:00
|
|
|
}
|
|
|
|
let buffer = {}
|
2019-07-20 09:06:29 +02:00
|
|
|
|
2019-08-07 14:40:58 +02:00
|
|
|
async function main() {
|
2019-08-08 02:32:11 +02:00
|
|
|
|
2019-08-07 15:51:34 +02:00
|
|
|
console.log(`Parsing 'index.m3u'...`)
|
2019-08-07 14:40:58 +02:00
|
|
|
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')
|
2019-08-08 02:32:11 +02:00
|
|
|
countries = countries.slice(0, 1)
|
|
|
|
// countries = [{ url: 'channels/ru.m3u' }, { url: 'channels/ua.m3u' }]
|
2019-07-20 09:06:29 +02:00
|
|
|
}
|
2019-04-30 15:52:38 +02:00
|
|
|
|
|
|
|
for(let country of countries) {
|
2019-08-08 01:50:53 +02:00
|
|
|
|
|
|
|
if(debug) {
|
|
|
|
console.log(`Clear cache...`)
|
|
|
|
}
|
2019-08-08 01:43:43 +02:00
|
|
|
util.clearCache()
|
|
|
|
|
2019-08-07 15:51:34 +02:00
|
|
|
console.log(`Parsing '${country.url}'...`)
|
2019-08-07 14:40:58 +02:00
|
|
|
const playlist = util.parsePlaylist(country.url)
|
|
|
|
|
2019-08-07 15:51:34 +02:00
|
|
|
if(debug) {
|
|
|
|
console.log(`Creating channels list...`)
|
|
|
|
}
|
2019-08-07 14:40:58 +02:00
|
|
|
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-08-08 01:24:15 +02:00
|
|
|
|
2019-08-08 02:32:11 +02:00
|
|
|
if(util.checkCache(channel.url)) {
|
|
|
|
stats.duplicates++
|
|
|
|
} else if(!util.validateUrl(channel.url)) {
|
|
|
|
stats.unvalid++
|
|
|
|
} else {
|
2019-08-08 01:24:15 +02:00
|
|
|
channels.push(channel)
|
|
|
|
util.addToCache(channel.url)
|
|
|
|
}
|
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]) {
|
2019-08-07 15:51:34 +02:00
|
|
|
console.log(`Loading '${epgUrl}'...`)
|
2019-08-07 14:40:58 +02:00
|
|
|
const epg = await util.loadEPG(epgUrl)
|
2019-08-07 15:51:34 +02:00
|
|
|
console.log(`Adding '${epgUrl}' to buffer...`)
|
2019-08-07 14:40:58 +02:00
|
|
|
buffer[epgUrl] = epg
|
2019-04-30 15:52:38 +02:00
|
|
|
}
|
|
|
|
|
2019-08-07 15:51:34 +02:00
|
|
|
if(buffer[epgUrl]) {
|
|
|
|
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(debug) {
|
|
|
|
console.log(`Added name '${c.names[0]}' to '${channel.id}'`)
|
|
|
|
}
|
2019-08-07 14:40:58 +02:00
|
|
|
}
|
|
|
|
|
2019-08-07 15:51:34 +02:00
|
|
|
if(!channel.logo && c.icon) {
|
|
|
|
channel.logo = c.icon
|
|
|
|
updated = true
|
|
|
|
if(debug) {
|
|
|
|
console.log(`Added logo '${c.icon}' to '${channel.id}'`)
|
|
|
|
}
|
2019-08-07 14:40:58 +02:00
|
|
|
}
|
|
|
|
|
2019-08-07 15:51:34 +02:00
|
|
|
if(updated) {
|
|
|
|
stats.updated++
|
|
|
|
}
|
2019-08-07 14:40:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 15:51:34 +02:00
|
|
|
if(debug) {
|
|
|
|
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 15:51:34 +02:00
|
|
|
console.log(`Updating '${country.url}'...`)
|
2019-08-07 14:40:58 +02:00
|
|
|
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-08 02:32:11 +02:00
|
|
|
stats.total += channels.length
|
2019-04-30 15:52:38 +02:00
|
|
|
}
|
|
|
|
|
2019-08-09 23:58:11 +02:00
|
|
|
console.log(`Total: ${stats.total}. Duplicates: ${stats.duplicates}. Unvalid: ${stats.unvalid}. Updated: ${stats.updated}.`)
|
2019-08-07 14:40:58 +02:00
|
|
|
}
|
2019-04-30 15:52:38 +02:00
|
|
|
|
2019-08-07 14:40:58 +02:00
|
|
|
main()
|