This commit is contained in:
freearhey 2019-11-03 10:56:54 +03:00
parent db008d61fb
commit fc469c0568
2 changed files with 187 additions and 163 deletions

View File

@ -1,174 +1,184 @@
const helper = require('./helper') const helper = require('./helper')
const debug = false const config = {
const verbose = false debug: false,
const parseEpg = process.env.npm_config_epg || false country: process.env.npm_config_country,
exclude: process.env.npm_config_exclude,
let stats = { epg: process.env.npm_config_epg || false
total: 0,
updated: 0,
duplicates: 0,
unvalid: 0,
removed: 0
} }
let buffer = {}
let unsorted = {}
async function main() { // let stats = {
// total: 0,
// updated: 0,
// duplicates: 0,
// removed: 0
// }
// let buffer = {}
console.log(`Parsing 'index.m3u'...`) let playlists = {}
const playlist = helper.parsePlaylist('index.m3u')
const countries = playlist.items
if(debug) { function parseIndex() {
console.log('Debug mode is turn on') const root = helper.parsePlaylist('index.m3u')
} const rootItems = helper.filterPlaylists(root.items, config.country, config.exclude)
const unsortedPlaylist = helper.parsePlaylist('channels/unsorted.m3u') for(let rootItem of rootItems) {
for(const item of unsortedPlaylist.items) { const playlist = helper.parsePlaylist(rootItem.url)
unsorted[item.url] = helper.createChannel(item) playlists[rootItem.url] = {}
}
for(let country of countries) {
if (helper.skipPlaylist(country.url)) {
continue
}
if(verbose) {
console.log(`Clear cache...`)
}
helper.clearCache()
console.log(`Parsing '${country.url}'...`)
const playlist = helper.parsePlaylist(country.url)
if(verbose) {
console.log(`Creating channels list...`)
}
let channels = []
for(let item of playlist.items) { for(let item of playlist.items) {
let channel = helper.createChannel(item) playlists[rootItem.url][item.url] = helper.createChannel(item)
}
}
}
if(helper.checkCache(channel.url)) { function main() {
stats.duplicates++ console.log(`Parsing index...`)
} else if(!helper.validateUrl(channel.url)) { parseIndex()
stats.unvalid++ console.log(playlists)
} else { // add missing data from epg (if exists)
channels.push(channel) // sort channels
helper.addToCache(channel.url) // save all playlists back
} // display stats
if(unsorted[channel.url]) { // for(let country of countries) {
if(verbose) {
console.log(`Removed '${channel.url}' from 'channels/unsorted.m3u'...`)
}
delete unsorted[channel.url]
stats.removed++
stats.duplicates++
}
}
const epgUrl = playlist.header.attrs['x-tvg-url'] // if (helper.skipPlaylist(country.url)) {
if(epgUrl && !buffer[epgUrl] && parseEpg) { // continue
try { // }
console.log(`Loading '${epgUrl}'...`)
const epg = await helper.loadEPG(epgUrl)
console.log(`Adding '${epgUrl}' to buffer...`)
buffer[epgUrl] = epg
} catch(e) {
console.log(`Could not load '${epgUrl}'`)
}
}
if(buffer[epgUrl]) { // if(verbose) {
console.log('Add missing tvg-id from EPG by channel title...') // console.log(`Clear cache...`)
for(let channel of channels) { // }
for(let channelId in buffer[epgUrl].channels) { // helper.clearCache()
let c = buffer[epgUrl].channels[channelId]
for(let epgName of c.name) {
if(epgName.value) {
let escaped = helper.escapeStringRegexp(epgName.value)
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
}
}
}
}
}
}
}
if(buffer[epgUrl]) { // console.log(`Parsing '${country.url}'...`)
console.log(`Fills in missing channel's data...`) // const playlist = helper.parsePlaylist(country.url)
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.name.length) { // if(verbose) {
channel.name = c.name[0].value // console.log(`Creating channels list...`)
updated = true // }
if(verbose) { // let channels = []
console.log(`Added name '${c.name[0].value}' to '${channel.id}'`) // for(let item of playlist.items) {
} // let channel = helper.createChannel(item)
}
if(!channel.language && c.name.length && c.name[0].lang) { // if(helper.checkCache(channel.url)) {
let language = helper.getISO6391Name(c.name[0].lang) // stats.duplicates++
channel.language = language // } else if(!helper.validateUrl(channel.url)) {
updated = true // stats.unvalid++
if(verbose) { // } else {
console.log(`Added language '${language}' to '${channel.id}'`) // channels.push(channel)
} // helper.addToCache(channel.url)
} // }
if(!channel.logo && c.icon.length) { // if(unsorted[channel.url]) {
const icon = c.icon[0].split('|')[0] // if(verbose) {
channel.logo = icon // console.log(`Removed '${channel.url}' from 'channels/unsorted.m3u'...`)
updated = true // }
if(verbose) { // delete unsorted[channel.url]
console.log(`Added logo '${icon}' to '${channel.id}'`) // stats.removed++
} // stats.duplicates++
} // }
// }
if(updated) { // const epgUrl = playlist.header.attrs['x-tvg-url']
stats.updated++ // if(epgUrl && !buffer[epgUrl] && parseEpg) {
} // try {
} // console.log(`Loading '${epgUrl}'...`)
} // const epg = await helper.loadEPG(epgUrl)
// console.log(`Adding '${epgUrl}' to buffer...`)
// buffer[epgUrl] = epg
// } catch(e) {
// console.log(`Could not load '${epgUrl}'`)
// }
// }
if(verbose) { // if(buffer[epgUrl]) {
console.log(`Sorting channels...`) // console.log('Add missing tvg-id from EPG by channel title...')
} // for(let channel of channels) {
channels = helper.sortBy(channels, ['title', 'url']) // for(let channelId in buffer[epgUrl].channels) {
// let c = buffer[epgUrl].channels[channelId]
// for(let epgName of c.name) {
// if(epgName.value) {
// let escaped = helper.escapeStringRegexp(epgName.value)
// 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
// }
// }
// }
// }
// }
// }
// }
if(!debug) { // if(buffer[epgUrl]) {
console.log(`Updating '${country.url}'...`) // console.log(`Fills in missing channel's data...`)
helper.createFile(country.url, playlist.getHeader()) // for(let channel of channels) {
channels.forEach(channel => { // let channelId = channel.id
helper.appendToFile(country.url, channel.toShortString()) // if(!channelId) continue
}) // let c = buffer[epgUrl].channels[channelId]
} // if(!c) continue
// let updated = false
stats.total += channels.length // if(!channel.name && c.name.length) {
} // channel.name = c.name[0].value
// updated = true
// if(verbose) {
// console.log(`Added name '${c.name[0].value}' to '${channel.id}'`)
// }
// }
if(!debug & stats.removed > 0) { // if(!channel.language && c.name.length && c.name[0].lang) {
console.log(`Updating 'channels/unsorted.m3u'...`) // let language = helper.getISO6391Name(c.name[0].lang)
helper.createFile('channels/unsorted.m3u', playlist.getHeader()) // channel.language = language
Object.values(unsorted).forEach(channel => { // updated = true
helper.appendToFile('channels/unsorted.m3u', channel.toShortString()) // if(verbose) {
}) // console.log(`Added language '${language}' to '${channel.id}'`)
} // }
// }
console.log(`Total: ${stats.total}. Duplicates: ${stats.duplicates}. Unvalid: ${stats.unvalid}. Updated: ${stats.updated}.`) // if(!channel.logo && c.icon.length) {
// const icon = c.icon[0].split('|')[0]
// channel.logo = icon
// updated = true
// if(verbose) {
// console.log(`Added logo '${icon}' to '${channel.id}'`)
// }
// }
// if(updated) {
// stats.updated++
// }
// }
// }
// if(verbose) {
// console.log(`Sorting channels...`)
// }
// channels = helper.sortBy(channels, ['title', 'url'])
// if(!debug) {
// console.log(`Updating '${country.url}'...`)
// helper.createFile(country.url, playlist.getHeader())
// channels.forEach(channel => {
// helper.appendToFile(country.url, channel.toShortString())
// })
// }
// stats.total += channels.length
// }
// if(!debug & stats.removed > 0) {
// console.log(`Updating 'channels/unsorted.m3u'...`)
// helper.createFile('channels/unsorted.m3u', playlist.getHeader())
// Object.values(unsorted).forEach(channel => {
// helper.appendToFile('channels/unsorted.m3u', channel.toShortString())
// })
// }
// console.log(`Total: ${stats.total}. Duplicates: ${stats.duplicates}. Unvalid: ${stats.unvalid}. Updated: ${stats.updated}.`)
} }
main() main()

View File

@ -147,6 +147,20 @@ helper.validateUrl = function(channelUrl) {
return blacklist.indexOf(host) === -1 return blacklist.indexOf(host) === -1
} }
helper.filterPlaylists = function(arr, include = '', exclude = '') {
if(include) {
const included = include.split(',').map(filename => `channels/${filename}.m3u`)
return arr.filter(i => included.indexOf(i.url) > -1)
}
if(exclude) {
const excluded = exclude.split(',').map(filename => `channels/${filename}.m3u`)
return arr.filter(i => excluded.indexOf(i.url) === -1)
}
}
helper.skipPlaylist = function(filename) { helper.skipPlaylist = function(filename) {
let testCountry = process.env.npm_config_country let testCountry = process.env.npm_config_country
let excludeList = process.env.npm_config_exclude let excludeList = process.env.npm_config_exclude