iptv/helpers/generate.js

78 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-07-20 09:03:31 +02:00
const util = require('./util')
2019-04-30 14:45:02 +02:00
const urlParser = require('url')
2019-07-20 09:03:31 +02:00
const outputFile = 'index.full.m3u'
const debug = false
let cache = {}
2019-07-20 09:03:31 +02:00
let stats = {
total: 0,
duplicates: 0
}
function init() {
2019-07-20 09:03:31 +02:00
let countries = util.parsePlaylist('index.m3u')
if(debug) {
countries = countries.slice(0, 1)
}
2019-07-20 09:32:57 +02:00
util.createFile(outputFile, '#EXTM3U\n')
for(let country of countries) {
2019-07-20 09:03:31 +02:00
const countryName = util.getTitle(country.title)
const countryCode = util.getBasename(country.file).toUpperCase()
const playlist = util.parsePlaylist(country.file)
for(let item of playlist) {
2019-07-20 09:03:31 +02:00
const channel = util.parseChannelData(item)
const groupTitle = [ countryName, channel.group ].filter(i => i).join(';')
2019-07-20 09:03:31 +02:00
const info = `-1 tvg-id="${channel.id}" tvg-name="${channel.name}" tvg-logo="${channel.logo}" group-title="${groupTitle}",${channel.title}`
const file = channel.file
2019-07-20 09:03:31 +02:00
if(checkCache(file)) {
2019-07-20 09:03:31 +02:00
stats.duplicates++
} else {
2019-07-20 09:32:57 +02:00
const data = '#EXTINF:' + info + '\n' + file + '\n'
util.writeToFile(outputFile, data)
2019-07-20 09:03:31 +02:00
addToCache(file)
}
2019-07-20 09:03:31 +02:00
stats.total++
}
}
}
init()
2019-07-20 09:03:31 +02:00
console.log(`Total: ${stats.total}. Duplicates: ${stats.duplicates}. Unique: ${stats.total - stats.duplicates}`)
function addToCache(url) {
2019-04-30 14:45:02 +02:00
let id = getUrlPath(url)
cache[id] = true
}
function checkCache(url) {
2019-04-30 14:45:02 +02:00
let id = getUrlPath(url)
return cache.hasOwnProperty(id)
2019-04-30 14:45:02 +02:00
}
function getUrlPath(u) {
let parsed = urlParser.parse(u)
let searchQuery = parsed.search || ''
return parsed.host + parsed.pathname + searchQuery
}