2019-11-02 10:45:09 +01:00
|
|
|
const helper = require('./helper')
|
2019-04-30 15:52:38 +02:00
|
|
|
|
2019-08-07 15:51:34 +02:00
|
|
|
const debug = false
|
2019-08-17 13:58:28 +02:00
|
|
|
const verbose = false
|
2019-08-11 23:28:51 +02:00
|
|
|
const parseEpg = process.env.npm_config_epg || 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-19 16:14:55 +02:00
|
|
|
unvalid: 0,
|
|
|
|
removed: 0
|
2019-08-07 14:40:58 +02:00
|
|
|
}
|
|
|
|
let buffer = {}
|
2019-08-19 16:14:55 +02:00
|
|
|
let unsorted = {}
|
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-11-02 10:45:09 +01:00
|
|
|
const playlist = helper.parsePlaylist('index.m3u')
|
2019-09-22 15:47:49 +02:00
|
|
|
const countries = playlist.items
|
2019-08-19 16:14:55 +02:00
|
|
|
|
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-07-20 09:06:29 +02:00
|
|
|
}
|
2019-04-30 15:52:38 +02:00
|
|
|
|
2019-11-02 10:45:09 +01:00
|
|
|
const unsortedPlaylist = helper.parsePlaylist('channels/unsorted.m3u')
|
2019-08-19 16:14:55 +02:00
|
|
|
for(const item of unsortedPlaylist.items) {
|
2019-11-02 10:45:09 +01:00
|
|
|
unsorted[item.url] = helper.createChannel(item)
|
2019-08-19 16:14:55 +02:00
|
|
|
}
|
|
|
|
|
2019-04-30 15:52:38 +02:00
|
|
|
for(let country of countries) {
|
2019-08-12 14:36:42 +02:00
|
|
|
|
2019-11-02 10:45:09 +01:00
|
|
|
if (helper.skipPlaylist(country.url)) {
|
2019-08-12 14:36:42 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-08-08 01:50:53 +02:00
|
|
|
|
2019-08-17 13:58:28 +02:00
|
|
|
if(verbose) {
|
2019-08-08 01:50:53 +02:00
|
|
|
console.log(`Clear cache...`)
|
|
|
|
}
|
2019-11-02 10:45:09 +01:00
|
|
|
helper.clearCache()
|
2019-08-08 01:43:43 +02:00
|
|
|
|
2019-08-07 15:51:34 +02:00
|
|
|
console.log(`Parsing '${country.url}'...`)
|
2019-11-02 10:45:09 +01:00
|
|
|
const playlist = helper.parsePlaylist(country.url)
|
2019-08-07 14:40:58 +02:00
|
|
|
|
2019-08-17 13:58:28 +02:00
|
|
|
if(verbose) {
|
2019-08-07 15:51:34 +02:00
|
|
|
console.log(`Creating channels list...`)
|
|
|
|
}
|
2019-08-07 14:40:58 +02:00
|
|
|
let channels = []
|
|
|
|
for(let item of playlist.items) {
|
2019-11-02 10:45:09 +01:00
|
|
|
let channel = helper.createChannel(item)
|
2019-08-08 01:24:15 +02:00
|
|
|
|
2019-11-02 10:45:09 +01:00
|
|
|
if(helper.checkCache(channel.url)) {
|
2019-08-08 02:32:11 +02:00
|
|
|
stats.duplicates++
|
2019-11-02 10:45:09 +01:00
|
|
|
} else if(!helper.validateUrl(channel.url)) {
|
2019-08-08 02:32:11 +02:00
|
|
|
stats.unvalid++
|
|
|
|
} else {
|
2019-08-08 01:24:15 +02:00
|
|
|
channels.push(channel)
|
2019-11-02 10:45:09 +01:00
|
|
|
helper.addToCache(channel.url)
|
2019-08-08 01:24:15 +02:00
|
|
|
}
|
2019-08-19 16:14:55 +02:00
|
|
|
|
|
|
|
if(unsorted[channel.url]) {
|
|
|
|
if(verbose) {
|
|
|
|
console.log(`Removed '${channel.url}' from 'channels/unsorted.m3u'...`)
|
|
|
|
}
|
|
|
|
delete unsorted[channel.url]
|
|
|
|
stats.removed++
|
2019-08-22 16:52:06 +02:00
|
|
|
stats.duplicates++
|
2019-08-19 16:14:55 +02:00
|
|
|
}
|
2019-08-07 14:40:58 +02:00
|
|
|
}
|
2019-04-30 15:52:38 +02:00
|
|
|
|
2019-10-07 03:15:27 +02:00
|
|
|
const epgUrl = playlist.header.attrs['x-tvg-url']
|
2019-08-11 23:28:51 +02:00
|
|
|
if(epgUrl && !buffer[epgUrl] && parseEpg) {
|
2019-10-25 22:37:42 +02:00
|
|
|
try {
|
|
|
|
console.log(`Loading '${epgUrl}'...`)
|
2019-11-02 10:45:09 +01:00
|
|
|
const epg = await helper.loadEPG(epgUrl)
|
2019-10-25 22:37:42 +02:00
|
|
|
console.log(`Adding '${epgUrl}' to buffer...`)
|
|
|
|
buffer[epgUrl] = epg
|
|
|
|
} catch(e) {
|
|
|
|
console.log(`Could not load '${epgUrl}'`)
|
|
|
|
}
|
2019-04-30 15:52:38 +02:00
|
|
|
}
|
|
|
|
|
2019-10-23 12:15:46 +02:00
|
|
|
if(buffer[epgUrl]) {
|
|
|
|
console.log('Add missing tvg-id from EPG by channel title...')
|
|
|
|
for(let channel of channels) {
|
|
|
|
for(let channelId in buffer[epgUrl].channels) {
|
|
|
|
let c = buffer[epgUrl].channels[channelId]
|
2019-10-30 16:44:26 +01:00
|
|
|
for(let epgName of c.name) {
|
|
|
|
if(epgName.value) {
|
2019-11-02 10:45:09 +01:00
|
|
|
let escaped = helper.escapeStringRegexp(epgName.value)
|
2019-10-30 16:44:26 +01:00
|
|
|
channelTitle = channel.title.replace(/(fhd|hd|sd|高清)$/i, '').trim()
|
|
|
|
let regexp = new RegExp(`^${escaped}$`, 'i')
|
|
|
|
if(regexp.test(channelTitle)) {
|
|
|
|
if(!channel.id) {
|
|
|
|
channel.id = c.id
|
|
|
|
continue
|
|
|
|
}
|
2019-10-23 12:15:46 +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
|
|
|
|
|
2019-10-30 16:44:26 +01:00
|
|
|
if(!channel.name && c.name.length) {
|
|
|
|
channel.name = c.name[0].value
|
2019-08-07 15:51:34 +02:00
|
|
|
updated = true
|
2019-08-17 13:58:28 +02:00
|
|
|
if(verbose) {
|
2019-10-30 16:44:26 +01:00
|
|
|
console.log(`Added name '${c.name[0].value}' to '${channel.id}'`)
|
2019-08-07 15:51:34 +02:00
|
|
|
}
|
2019-08-07 14:40:58 +02:00
|
|
|
}
|
|
|
|
|
2019-10-30 18:19:54 +01:00
|
|
|
if(!channel.language && c.name.length && c.name[0].lang) {
|
2019-11-02 10:45:09 +01:00
|
|
|
let language = helper.getISO6391Name(c.name[0].lang)
|
2019-10-30 18:19:54 +01:00
|
|
|
channel.language = language
|
|
|
|
updated = true
|
|
|
|
if(verbose) {
|
|
|
|
console.log(`Added language '${language}' to '${channel.id}'`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-30 16:44:26 +01:00
|
|
|
if(!channel.logo && c.icon.length) {
|
|
|
|
const icon = c.icon[0].split('|')[0]
|
2019-08-17 13:58:28 +02:00
|
|
|
channel.logo = icon
|
2019-08-07 15:51:34 +02:00
|
|
|
updated = true
|
2019-08-17 13:58:28 +02:00
|
|
|
if(verbose) {
|
|
|
|
console.log(`Added logo '${icon}' to '${channel.id}'`)
|
2019-08-07 15:51:34 +02:00
|
|
|
}
|
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-17 13:58:28 +02:00
|
|
|
if(verbose) {
|
2019-08-07 15:51:34 +02:00
|
|
|
console.log(`Sorting channels...`)
|
|
|
|
}
|
2019-11-02 13:43:41 +01:00
|
|
|
channels = helper.sortBy(channels, ['title', 'url'])
|
2019-04-30 15:52:38 +02:00
|
|
|
|
2019-08-19 16:14:55 +02:00
|
|
|
if(!debug) {
|
|
|
|
console.log(`Updating '${country.url}'...`)
|
2019-11-02 10:45:09 +01:00
|
|
|
helper.createFile(country.url, playlist.getHeader())
|
2019-08-19 16:14:55 +02:00
|
|
|
channels.forEach(channel => {
|
2019-11-02 15:21:19 +01:00
|
|
|
helper.appendToFile(country.url, channel.toShortString())
|
2019-08-19 16:14:55 +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-19 16:14:55 +02:00
|
|
|
if(!debug & stats.removed > 0) {
|
|
|
|
console.log(`Updating 'channels/unsorted.m3u'...`)
|
2019-11-02 10:45:09 +01:00
|
|
|
helper.createFile('channels/unsorted.m3u', playlist.getHeader())
|
2019-08-19 16:14:55 +02:00
|
|
|
Object.values(unsorted).forEach(channel => {
|
2019-11-02 15:21:19 +01:00
|
|
|
helper.appendToFile('channels/unsorted.m3u', channel.toShortString())
|
2019-08-19 16:14:55 +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()
|