You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iptv/scripts/format.js

147 lines
3.8 KiB
JavaScript

const helper = require('./helper')
5 years ago
const config = {
5 years ago
debug: process.env.npm_config_debug || false,
5 years ago
country: process.env.npm_config_country,
exclude: process.env.npm_config_exclude,
epg: process.env.npm_config_epg || false
}
6 years ago
5 years ago
let updated = 0
5 years ago
async function main() {
console.log(`Parsing index...`)
5 years ago
const index = parseIndex()
for(let item of index.items) {
console.log(`Processing '${item.url}'...`)
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`) }
}
}
5 years ago
5 years ago
if(playlist.changed) {
updatePlaylist(item.url, playlist)
updated++
} else {
console.log('Nothing is changed')
}
5 years ago
}
5 years ago
console.log(`Updated ${updated} playlist(s)`)
5 years ago
console.log('Done.\n')
}
5 years ago
function parseIndex() {
5 years ago
const playlist = helper.parsePlaylist('index.m3u')
playlist.items = helper.filterPlaylists(playlist.items, config.country, config.exclude)
console.log(`Found ${playlist.items.length} playlist(s)`)
return playlist
5 years ago
}
5 years ago
function parsePlaylist(url) {
const playlist = helper.parsePlaylist(url)
playlist.items = playlist.items.map(item => {
return helper.createChannel(item)
})
return playlist
5 years ago
}
5 years ago
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
}
5 years ago
5 years ago
function removeDuplicates(playlist) {
let buffer = {}
const channels = JSON.stringify(playlist.items)
playlist.items = playlist.items.filter(i => {
let result = typeof buffer[i.url] === 'undefined'
if(result) {
buffer[i.url] = true
} else {
if(config.debug) { console.log(`Duplicate of '${i.title}' has been removed`) }
5 years ago
}
5 years ago
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
5 years ago
}
}
5 years ago
function addDataFromEPG(playlist, epg) {
if(!epg) return playlist
5 years ago
5 years ago
for(let item of playlist.items) {
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}'`) }
5 years ago
}
}
5 years ago
return playlist
5 years ago
}
5 years ago
function updatePlaylist(filepath, playlist) {
helper.createFile(filepath, playlist.getHeader())
for(let channel of playlist.items) {
helper.appendToFile(filepath, channel.toShortString())
5 years ago
}
5 years ago
console.log(`Playlist '${filepath}' has been updated`)
}
5 years ago
main()