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() for(let item of index.items) {
console.log(`Removing duplicates...`) console.log(`Processing '${item.url}'...`)
removeDuplicates() 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) { if(config.epg) {
console.log('Adding the missing data from EPG files...') const tvgUrl = playlist.header.attrs['x-tvg-url']
await addMissingData() 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(isChanged) { if(playlist.changed) {
console.log('Updating files...') updatePlaylist(item.url, playlist)
updateFiles() updated++
} else { } else {
console.log('Nothing is changed.') console.log('Nothing is changed')
} }
}
console.log(`Updated ${updated} playlist(s)`)
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 parsePlaylist(url) {
const playlist = helper.parsePlaylist(url)
playlist.items = playlist.items.map(item => {
return helper.createChannel(item)
}) })
}
return playlist
} }
function sortChannels() { function sortChannels(playlist) {
for(let pid in playlists) { const channels = JSON.stringify(playlist.items)
const channels = playlists[pid].items playlist.items = helper.sortBy(playlist.items, ['title', 'url'])
playlists[pid].items = helper.sortBy(channels, ['title', 'url']) if(channels !== JSON.stringify(playlist.items)) { playlist.changed = true }
if(channels !== playlists[pid].items) {
playlists[pid].changed = true return playlist
isChanged = true
}
}
} }
function removeDuplicates() { 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 return result
}) })
if(channels.length !== playlists[pid].items.length) { if(channels !== JSON.stringify(playlist.items)) { playlist.changed = true }
playlists[pid].changed = true
isChanged = true return playlist
}
}
} }
function updateFiles() { async function loadEPG(url) {
for(let pid in playlists) {
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) {
console.log(`File '${pid}' has been updated`)
}
}
}
}
async function addMissingData() {
let guides = {}
for(let playlist of Object.values(playlists)) {
for(let item of playlist.items) {
if(!item.epg) continue
try { try {
const guide = guides[item.epg] || await helper.parseEPG(item.epg) return await helper.parseEPG(url)
guides[item.epg] = guide } catch(err) {
console.error(`Error: could not load '${url}'`)
return
}
}
function addDataFromEPG(playlist, epg) {
if(!epg) return playlist
for(let item of playlist.items) {
if(!item.id) continue if(!item.id) continue
const channel = guide.channels[item.id] const channel = epg.channels[item.id]
if(!channel) continue if(!channel) continue
if(!item.name && channel.name.length) { if(!item.name && channel.name.length) {
item.name = channel.name[0].value item.name = channel.name[0].value
if(config.debug) { playlist.changed = true
console.log(`Added tvg-name '${item.name}' to '${item.id}'`) if(config.debug) { console.log(`Added tvg-name '${item.name}' to '${item.title}'`) }
}
} }
if(!item.language && channel.name.length && channel.name[0].lang) { if(!item.language && channel.name.length && channel.name[0].lang) {
item.language = channel.name[0].lang item.language = channel.name[0].lang
if(config.debug) { playlist.changed = true
console.log(`Added tvg-language '${item.language}' to '${item.id}'`) if(config.debug) { console.log(`Added tvg-language '${item.language}' to '${item.title}'`) }
}
} }
if(!item.logo && channel.icon.length) { if(!item.logo && channel.icon.length) {
item.logo = channel.icon[0] item.logo = channel.icon[0]
if(config.debug) { playlist.changed = true
console.log(`Added tvg-logo '${item.logo}' to '${item.id}'`) if(config.debug) { console.log(`Added tvg-logo '${item.logo}' to '${item.title}'`) }
}
}
} catch(err) {
console.error(`Could not load '${item.epg}'`)
continue
} }
} }
return playlist
}
function updatePlaylist(filepath, playlist) {
helper.createFile(filepath, playlist.getHeader())
for(let channel of playlist.items) {
helper.appendToFile(filepath, channel.toShortString())
} }
console.log(`Playlist '${filepath}' has been updated`)
} }
main() main()