pull/621/head
freearhey 6 years ago
parent db008d61fb
commit fc469c0568

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

@ -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

Loading…
Cancel
Save