This commit is contained in:
freearhey 2019-11-03 16:03:35 +03:00
parent 6cbce4f201
commit 11f7e4cf45
2 changed files with 120 additions and 171 deletions

View file

@ -1,21 +1,37 @@
const helper = require('./helper') const helper = require('./helper')
const config = { const config = {
debug: false, debug: process.env.npm_config_debug || false,
country: process.env.npm_config_country, country: process.env.npm_config_country,
exclude: process.env.npm_config_exclude, exclude: process.env.npm_config_exclude,
epg: process.env.npm_config_epg || false epg: process.env.npm_config_epg || false
} }
// let stats = {
// total: 0,
// updated: 0,
// duplicates: 0,
// removed: 0
// }
// let buffer = {}
let playlists = {} let playlists = {}
let isChanged = false
async function main() {
console.log(`Parsing index...`)
parseIndex()
console.log(`Sorting channels...`)
sortChannels()
console.log(`Removing duplicates...`)
removeDuplicates()
if(config.epg) {
console.log('Adding the missing data from EPG files...')
await addMissingData()
}
if(isChanged) {
console.log('Updating files...')
updateFiles()
} else {
console.log('Nothing is changed.')
}
console.log('Done.\n')
}
function parseIndex() { function parseIndex() {
const root = helper.parsePlaylist('index.m3u') const root = helper.parsePlaylist('index.m3u')
@ -23,162 +39,110 @@ function parseIndex() {
for(let rootItem of rootItems) { for(let rootItem of rootItems) {
const playlist = helper.parsePlaylist(rootItem.url) const playlist = helper.parsePlaylist(rootItem.url)
playlists[rootItem.url] = {} const tvgUrl = playlist.header.attrs['x-tvg-url']
for(let item of playlist.items) {
playlists[rootItem.url][item.url] = helper.createChannel(item) playlists[rootItem.url] = playlist
playlists[rootItem.url].items = playlist.items.map(item => {
let channel = helper.createChannel(item)
channel.epg = tvgUrl
return channel
})
}
}
function sortChannels() {
for(let pid in playlists) {
const channels = playlists[pid].items
playlists[pid].items = helper.sortBy(channels, ['title', 'url'])
if(channels !== playlists[pid].items) {
playlists[pid].changed = true
isChanged = true
} }
} }
} }
function main() { function removeDuplicates() {
console.log(`Parsing index...`) let buffer = {}
parseIndex() for(let pid in playlists) {
console.log(playlists) const channels = playlists[pid].items
// add missing data from epg (if exists) playlists[pid].items = channels.filter(i => {
// sort channels let result = typeof buffer[i.url] === 'undefined'
// save all playlists back
// display stats if(result) {
buffer[i.url] = true
} else {
if(config.debug) {
console.log(`Duplicate of '${i.title}' has been removed from '${pid}'`)
}
}
return result
})
// for(let country of countries) { if(channels.length !== playlists[pid].items.length) {
playlists[pid].changed = true
isChanged = true
}
}
}
// if (helper.skipPlaylist(country.url)) { function updateFiles() {
// continue for(let pid in playlists) {
// } let playlist = playlists[pid]
if(playlist.changed) {
// if(verbose) { helper.createFile(pid, playlist.getHeader())
// console.log(`Clear cache...`) for(let channel of playlist.items) {
// } helper.appendToFile(pid, channel.toShortString())
// helper.clearCache() }
// console.log(`Parsing '${country.url}'...`) if(config.debug) {
// const playlist = helper.parsePlaylist(country.url) console.log(`File '${pid}' has been updated`)
}
}
}
}
// if(verbose) { async function addMissingData() {
// console.log(`Creating channels list...`) let guides = {}
// } for(let playlist of Object.values(playlists)) {
// let channels = [] for(let item of playlist.items) {
// for(let item of playlist.items) { if(!item.epg) continue
// let channel = helper.createChannel(item) try {
const guide = guides[item.epg] || await helper.parseEPG(item.epg)
guides[item.epg] = guide
// if(helper.checkCache(channel.url)) { if(!item.id) continue
// stats.duplicates++
// } else if(!helper.validateUrl(channel.url)) {
// stats.unvalid++
// } else {
// channels.push(channel)
// helper.addToCache(channel.url)
// }
// if(unsorted[channel.url]) {
// 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(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(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]
// 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(`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.name.length) { const channel = guide.channels[item.id]
// channel.name = c.name[0].value if(!channel) continue
// updated = true
// if(verbose) {
// console.log(`Added name '${c.name[0].value}' to '${channel.id}'`)
// }
// }
// if(!channel.language && c.name.length && c.name[0].lang) { if(!item.name && channel.name.length) {
// let language = helper.getISO6391Name(c.name[0].lang) item.name = channel.name[0].value
// channel.language = language if(config.debug) {
// updated = true console.log(`Added tvg-name '${item.name}' to '${item.id}'`)
// if(verbose) { }
// console.log(`Added language '${language}' to '${channel.id}'`) }
// }
// }
// if(!channel.logo && c.icon.length) { if(!item.language && channel.name.length && channel.name[0].lang) {
// const icon = c.icon[0].split('|')[0] item.language = channel.name[0].lang
// channel.logo = icon if(config.debug) {
// updated = true console.log(`Added tvg-language '${item.language}' to '${item.id}'`)
// if(verbose) { }
// console.log(`Added logo '${icon}' to '${channel.id}'`) }
// }
// }
// if(updated) { if(!item.logo && channel.icon.length) {
// stats.updated++ item.logo = channel.icon[0]
// } if(config.debug) {
// } console.log(`Added tvg-logo '${item.logo}' to '${item.id}'`)
// } }
}
// if(verbose) { } catch(err) {
// console.log(`Sorting channels...`) console.error(`Could not load '${item.epg}'`)
// } continue
// 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

@ -51,8 +51,8 @@ helper.parsePlaylist = function(filename) {
return new Playlist(result) return new Playlist(result)
} }
helper.loadEPG = async function(url) { helper.parseEPG = async function(url) {
const content = await getEPGFile(url) const content = await this.getEPG(url)
const result = epgParser.parse(content) const result = epgParser.parse(content)
const channels = {} const channels = {}
for(let channel of result.channels) { for(let channel of result.channels) {
@ -65,7 +65,7 @@ helper.loadEPG = async function(url) {
}) })
} }
helper.getEPGFile = function(url) { helper.getEPG = function(url) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
var buffer = [] var buffer = []
axios({ axios({
@ -111,22 +111,6 @@ helper.getBasename = function(filename) {
return path.basename(filename, path.extname(filename)) return path.basename(filename, path.extname(filename))
} }
helper.addToCache = function(url) {
let id = this.getUrlPath(url)
cache[id] = true
}
helper.checkCache = function(url) {
let id = this.getUrlPath(url)
return cache.hasOwnProperty(id)
}
helper.clearCache = function() {
cache = {}
}
helper.getUrlPath = function(u) { helper.getUrlPath = function(u) {
let parsed = urlParser.parse(u) let parsed = urlParser.parse(u)
let searchQuery = parsed.search || '' let searchQuery = parsed.search || ''
@ -220,6 +204,7 @@ class Playlist {
constructor(data) { constructor(data) {
this.header = data.header this.header = data.header
this.items = data.items this.items = data.items
this.changed = false
} }
getHeader() { getHeader() {