iptv/scripts/format.js

188 lines
4.5 KiB
JavaScript
Raw Normal View History

2019-11-02 10:45:09 +01:00
const helper = require('./helper')
2019-04-30 15:52:38 +02:00
2019-11-03 08:56:54 +01:00
const config = {
2019-11-03 14:03:35 +01:00
debug: process.env.npm_config_debug || false,
2019-11-03 08:56:54 +01:00
country: process.env.npm_config_country,
exclude: process.env.npm_config_exclude,
2020-05-04 15:33:02 +02:00
epg: process.env.npm_config_epg || false
2019-08-07 14:40:58 +02:00
}
2019-07-20 09:06:29 +02:00
2019-11-03 17:01:04 +01:00
let updated = 0
2020-04-22 15:41:54 +02:00
let items = []
2019-11-03 14:03:35 +01:00
async function main() {
console.log(`Parsing index...`)
2019-11-03 17:01:04 +01:00
const index = parseIndex()
2020-04-11 03:34:50 +02:00
for (let item of index.items) {
2019-11-03 17:01:04 +01:00
console.log(`Processing '${item.url}'...`)
let playlist = parsePlaylist(item.url)
2020-04-22 15:41:54 +02:00
items = items.concat(playlist.items)
2020-04-11 03:34:50 +02:00
if (config.debug) {
console.log(`Sorting channels...`)
}
2019-11-03 17:01:04 +01:00
playlist = sortChannels(playlist)
2020-04-22 15:41:54 +02:00
2020-04-11 03:34:50 +02:00
if (config.debug) {
console.log(`Removing duplicates...`)
}
2019-11-03 17:01:04 +01:00
playlist = removeDuplicates(playlist)
2020-04-11 03:34:50 +02:00
if (config.epg) {
2019-11-03 17:01:04 +01:00
const tvgUrl = playlist.header.attrs['x-tvg-url']
2020-04-11 03:34:50 +02:00
if (tvgUrl) {
if (config.debug) {
console.log(`Loading EPG from '${tvgUrl}'...`)
}
2019-11-03 17:01:04 +01:00
const epg = await loadEPG(tvgUrl)
2020-04-11 03:34:50 +02:00
if (config.debug) {
console.log(`Adding the missing data from EPG...`)
}
2019-11-03 17:01:04 +01:00
playlist = addDataFromEPG(playlist, epg)
} else {
2020-04-11 03:34:50 +02:00
if (config.debug) {
console.log(`EPG source is not found`)
}
2019-11-03 17:01:04 +01:00
}
}
2019-11-03 14:03:35 +01:00
2020-04-11 03:34:50 +02:00
if (playlist.changed) {
2019-11-03 17:01:04 +01:00
updatePlaylist(item.url, playlist)
updated++
} else {
console.log('Nothing is changed')
}
2019-11-03 14:03:35 +01:00
}
2020-04-22 15:41:54 +02:00
console.log(`Processing 'channels/unsorted.m3u'...`)
filterUnsorted()
2019-11-03 17:01:04 +01:00
console.log(`Updated ${updated} playlist(s)`)
2019-11-03 14:03:35 +01:00
console.log('Done.\n')
}
2019-11-03 08:56:54 +01:00
function parseIndex() {
2019-11-03 17:01:04 +01:00
const playlist = helper.parsePlaylist('index.m3u')
playlist.items = helper.filterPlaylists(playlist.items, config.country, config.exclude)
2019-11-03 17:01:04 +01:00
2020-04-22 15:41:54 +02:00
console.log(`Found ${playlist.items.length + 1} playlist(s)`)
2019-11-03 17:01:04 +01:00
return playlist
2019-11-03 14:03:35 +01:00
}
2019-11-03 17:01:04 +01:00
function parsePlaylist(url) {
const playlist = helper.parsePlaylist(url)
2020-05-04 15:33:02 +02:00
playlist.items = playlist.items.map(item => {
2019-11-03 17:01:04 +01:00
return helper.createChannel(item)
})
return playlist
2019-11-03 08:56:54 +01:00
}
2019-04-30 15:52:38 +02:00
2019-11-03 17:01:04 +01:00
function sortChannels(playlist) {
const channels = JSON.stringify(playlist.items)
2020-05-04 15:33:02 +02:00
playlist.items = helper.sortBy(playlist.items, ['name', 'url'])
2020-04-11 03:34:50 +02:00
if (channels !== JSON.stringify(playlist.items)) {
playlist.changed = true
}
2019-11-03 17:01:04 +01:00
return playlist
}
2019-11-03 14:03:35 +01:00
2019-11-03 17:01:04 +01:00
function removeDuplicates(playlist) {
let buffer = {}
const channels = JSON.stringify(playlist.items)
2020-05-04 15:33:02 +02:00
playlist.items = playlist.items.filter(i => {
2019-11-03 17:01:04 +01:00
let result = typeof buffer[i.url] === 'undefined'
2020-04-11 03:34:50 +02:00
if (result) {
2019-11-03 17:01:04 +01:00
buffer[i.url] = true
} else {
2020-04-11 03:34:50 +02:00
if (config.debug) {
2020-05-04 15:33:02 +02:00
console.log(`Duplicate of '${i.name}' has been removed`)
2020-04-11 03:34:50 +02:00
}
2019-11-03 14:03:35 +01:00
}
2020-04-11 03:34:50 +02:00
2019-11-03 17:01:04 +01:00
return result
})
2020-04-11 03:34:50 +02:00
if (channels !== JSON.stringify(playlist.items)) {
playlist.changed = true
}
2019-11-03 17:01:04 +01:00
return playlist
}
async function loadEPG(url) {
try {
return await helper.parseEPG(url)
2020-04-11 03:34:50 +02:00
} catch (err) {
2019-11-03 17:01:04 +01:00
console.error(`Error: could not load '${url}'`)
return
2019-11-03 14:03:35 +01:00
}
}
2019-11-03 17:01:04 +01:00
function addDataFromEPG(playlist, epg) {
2020-04-11 03:34:50 +02:00
if (!epg) return playlist
2020-05-04 15:33:02 +02:00
for (let channel of playlist.items) {
if (!channel.tvg.id) continue
2019-11-03 14:03:35 +01:00
2020-05-04 15:33:02 +02:00
const epgItem = epg.channels[channel.tvg.id]
2019-11-03 17:01:04 +01:00
2020-05-04 15:33:02 +02:00
if (!epgItem) continue
2019-11-03 17:01:04 +01:00
2020-05-04 15:33:02 +02:00
if (!channel.tvg.name && epgItem.name.length) {
channel.tvg.name = epgItem.name[0].value
2019-11-03 17:01:04 +01:00
playlist.changed = true
2020-04-11 03:34:50 +02:00
if (config.debug) {
2020-05-04 15:33:02 +02:00
console.log(`Added tvg-name '${channel.tvg.name}' to '${channel.name}'`)
2020-04-11 03:34:50 +02:00
}
2019-11-03 17:01:04 +01:00
}
2020-05-04 15:33:02 +02:00
if (!channel.language.length && epgItem.name.length && epgItem.name[0].lang) {
channel.setLanguage(epgItem.name[0].lang)
2019-11-03 17:01:04 +01:00
playlist.changed = true
2020-04-11 03:34:50 +02:00
if (config.debug) {
2020-05-04 15:33:02 +02:00
console.log(`Added tvg-language '${epgItem.name[0].lang}' to '${channel.name}'`)
2020-04-11 03:34:50 +02:00
}
2019-11-03 17:01:04 +01:00
}
2020-05-04 15:33:02 +02:00
if (!channel.logo && epgItem.icon.length) {
channel.logo = epgItem.icon[0]
2019-11-03 17:01:04 +01:00
playlist.changed = true
2020-04-11 03:34:50 +02:00
if (config.debug) {
2020-05-04 15:33:02 +02:00
console.log(`Added tvg-logo '${channel.logo}' to '${channel.name}'`)
2020-04-11 03:34:50 +02:00
}
2019-11-03 14:03:35 +01:00
}
}
2019-11-03 17:01:04 +01:00
return playlist
2019-11-03 14:03:35 +01:00
}
2019-11-03 17:01:04 +01:00
function updatePlaylist(filepath, playlist) {
helper.createFile(filepath, playlist.getHeader())
2020-04-11 03:34:50 +02:00
for (let channel of playlist.items) {
2019-11-03 17:01:04 +01:00
helper.appendToFile(filepath, channel.toShortString())
2019-11-03 14:03:35 +01:00
}
2019-11-03 17:01:04 +01:00
console.log(`Playlist '${filepath}' has been updated`)
2019-08-07 14:40:58 +02:00
}
2019-04-30 15:52:38 +02:00
2020-04-22 15:41:54 +02:00
function filterUnsorted() {
2020-05-04 15:33:02 +02:00
const urls = items.map(i => i.url)
2020-04-22 15:41:54 +02:00
const unsortedPlaylist = parsePlaylist('channels/unsorted.m3u')
const before = unsortedPlaylist.items.length
2020-05-04 15:33:02 +02:00
unsortedPlaylist.items = unsortedPlaylist.items.filter(i => !urls.includes(i.url))
2020-04-22 15:41:54 +02:00
if (before !== unsortedPlaylist.items.length) {
updatePlaylist('channels/unsorted.m3u', unsortedPlaylist)
updated++
}
}
2019-11-03 08:56:54 +01:00
main()