iptv/scripts/format.js

188 lines
4.4 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,
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)
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)
playlist.items = helper.sortBy(playlist.items, ['title', '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)
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) {
console.log(`Duplicate of '${i.title}' has been removed`)
}
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
for (let item of playlist.items) {
if (!item.id) continue
2019-11-03 14:03:35 +01:00
2019-11-03 17:01:04 +01:00
const channel = epg.channels[item.id]
2020-04-11 03:34:50 +02:00
if (!channel) continue
2019-11-03 17:01:04 +01:00
2020-04-11 03:34:50 +02:00
if (!item.name && channel.name.length) {
2019-11-03 17:01:04 +01:00
item.name = channel.name[0].value
playlist.changed = true
2020-04-11 03:34:50 +02:00
if (config.debug) {
console.log(`Added tvg-name '${item.name}' to '${item.title}'`)
}
2019-11-03 17:01:04 +01:00
}
2020-04-11 03:34:50 +02:00
if (!item.language && channel.name.length && channel.name[0].lang) {
2019-11-03 17:01:04 +01:00
item.language = channel.name[0].lang
playlist.changed = true
2020-04-11 03:34:50 +02:00
if (config.debug) {
console.log(`Added tvg-language '${item.language}' to '${item.title}'`)
}
2019-11-03 17:01:04 +01:00
}
2020-04-11 03:34:50 +02:00
if (!item.logo && channel.icon.length) {
2019-11-03 17:01:04 +01:00
item.logo = channel.icon[0]
playlist.changed = true
2020-04-11 03:34:50 +02:00
if (config.debug) {
console.log(`Added tvg-logo '${item.logo}' to '${item.title}'`)
}
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() {
const urls = items.map((i) => i.url)
const unsortedPlaylist = parsePlaylist('channels/unsorted.m3u')
const before = unsortedPlaylist.items.length
unsortedPlaylist.items = unsortedPlaylist.items.filter((i) => !urls.includes(i.url))
if (before !== unsortedPlaylist.items.length) {
updatePlaylist('channels/unsorted.m3u', unsortedPlaylist)
updated++
}
}
2019-11-03 08:56:54 +01:00
main()