Update format.js

This commit is contained in:
freearhey 2019-11-03 19:01:04 +03:00
parent 3e9176dede
commit 0ba0582fcf
1 changed files with 107 additions and 109 deletions

View File

@ -7,142 +7,140 @@ const config = {
epg: process.env.npm_config_epg || false epg: process.env.npm_config_epg || false
} }
let playlists = {} let updated = 0
let isChanged = false
async function main() { async function main() {
console.log(`Parsing index...`) console.log(`Parsing index...`)
parseIndex() const index = parseIndex()
console.log(`Sorting channels...`)
sortChannels()
console.log(`Removing duplicates...`)
removeDuplicates()
if(config.epg) { for(let item of index.items) {
console.log('Adding the missing data from EPG files...') console.log(`Processing '${item.url}'...`)
await addMissingData() let playlist = parsePlaylist(item.url)
if(config.debug) { console.log(`Sorting channels...`) }
playlist = sortChannels(playlist)
if(config.debug) { console.log(`Removing duplicates...`) }
playlist = removeDuplicates(playlist)
if(config.epg) {
const tvgUrl = playlist.header.attrs['x-tvg-url']
if(tvgUrl) {
if(config.debug) { console.log(`Loading EPG from '${tvgUrl}'...`) }
const epg = await loadEPG(tvgUrl)
if(config.debug) { console.log(`Adding the missing data from EPG...`) }
playlist = addDataFromEPG(playlist, epg)
} else {
if(config.debug) { console.log(`EPG source is not found`) }
}
}
if(playlist.changed) {
updatePlaylist(item.url, playlist)
updated++
} else {
console.log('Nothing is changed')
}
} }
if(isChanged) { console.log(`Updated ${updated} playlist(s)`)
console.log('Updating files...')
updateFiles()
} else {
console.log('Nothing is changed.')
}
console.log('Done.\n') console.log('Done.\n')
} }
function parseIndex() { function parseIndex() {
const root = helper.parsePlaylist('index.m3u') const playlist = helper.parsePlaylist('index.m3u')
const rootItems = helper.filterPlaylists(root.items, config.country, config.exclude) playlist.items = helper.filterPlaylists(playlist.items, config.country, config.exclude)
for(let rootItem of rootItems) { console.log(`Found ${playlist.items.length} playlist(s)`)
const playlist = helper.parsePlaylist(rootItem.url)
const tvgUrl = playlist.header.attrs['x-tvg-url']
playlists[rootItem.url] = playlist return playlist
playlists[rootItem.url].items = playlist.items.map(item => {
let channel = helper.createChannel(item)
channel.epg = tvgUrl
return channel
})
}
} }
function sortChannels() { function parsePlaylist(url) {
for(let pid in playlists) { const playlist = helper.parsePlaylist(url)
const channels = playlists[pid].items
playlists[pid].items = helper.sortBy(channels, ['title', 'url']) playlist.items = playlist.items.map(item => {
if(channels !== playlists[pid].items) { return helper.createChannel(item)
playlists[pid].changed = true })
isChanged = true
} return playlist
}
} }
function removeDuplicates() { function sortChannels(playlist) {
const channels = JSON.stringify(playlist.items)
playlist.items = helper.sortBy(playlist.items, ['title', 'url'])
if(channels !== JSON.stringify(playlist.items)) { playlist.changed = true }
return playlist
}
function removeDuplicates(playlist) {
let buffer = {} let buffer = {}
for(let pid in playlists) { const channels = JSON.stringify(playlist.items)
const channels = playlists[pid].items playlist.items = playlist.items.filter(i => {
playlists[pid].items = channels.filter(i => { let result = typeof buffer[i.url] === 'undefined'
let result = typeof buffer[i.url] === 'undefined'
if(result) { if(result) {
buffer[i.url] = true buffer[i.url] = true
} else { } else {
if(config.debug) { if(config.debug) { console.log(`Duplicate of '${i.title}' has been removed`) }
console.log(`Duplicate of '${i.title}' has been removed from '${pid}'`)
}
}
return result
})
if(channels.length !== playlists[pid].items.length) {
playlists[pid].changed = true
isChanged = true
} }
return result
})
if(channels !== JSON.stringify(playlist.items)) { playlist.changed = true }
return playlist
}
async function loadEPG(url) {
try {
return await helper.parseEPG(url)
} catch(err) {
console.error(`Error: could not load '${url}'`)
return
} }
} }
function updateFiles() { function addDataFromEPG(playlist, epg) {
for(let pid in playlists) { if(!epg) return playlist
let playlist = playlists[pid]
if(playlist.changed) {
helper.createFile(pid, playlist.getHeader())
for(let channel of playlist.items) {
helper.appendToFile(pid, channel.toShortString())
}
if(config.debug) { for(let item of playlist.items) {
console.log(`File '${pid}' has been updated`) if(!item.id) continue
}
const channel = epg.channels[item.id]
if(!channel) continue
if(!item.name && channel.name.length) {
item.name = channel.name[0].value
playlist.changed = true
if(config.debug) { console.log(`Added tvg-name '${item.name}' to '${item.title}'`) }
}
if(!item.language && channel.name.length && channel.name[0].lang) {
item.language = channel.name[0].lang
playlist.changed = true
if(config.debug) { console.log(`Added tvg-language '${item.language}' to '${item.title}'`) }
}
if(!item.logo && channel.icon.length) {
item.logo = channel.icon[0]
playlist.changed = true
if(config.debug) { console.log(`Added tvg-logo '${item.logo}' to '${item.title}'`) }
} }
} }
return playlist
} }
async function addMissingData() { function updatePlaylist(filepath, playlist) {
let guides = {} helper.createFile(filepath, playlist.getHeader())
for(let playlist of Object.values(playlists)) { for(let channel of playlist.items) {
for(let item of playlist.items) { helper.appendToFile(filepath, channel.toShortString())
if(!item.epg) continue
try {
const guide = guides[item.epg] || await helper.parseEPG(item.epg)
guides[item.epg] = guide
if(!item.id) continue
const channel = guide.channels[item.id]
if(!channel) continue
if(!item.name && channel.name.length) {
item.name = channel.name[0].value
if(config.debug) {
console.log(`Added tvg-name '${item.name}' to '${item.id}'`)
}
}
if(!item.language && channel.name.length && channel.name[0].lang) {
item.language = channel.name[0].lang
if(config.debug) {
console.log(`Added tvg-language '${item.language}' to '${item.id}'`)
}
}
if(!item.logo && channel.icon.length) {
item.logo = channel.icon[0]
if(config.debug) {
console.log(`Added tvg-logo '${item.logo}' to '${item.id}'`)
}
}
} catch(err) {
console.error(`Could not load '${item.epg}'`)
continue
}
}
} }
console.log(`Playlist '${filepath}' has been updated`)
} }
main() main()