iptv/helpers/generate.js

101 lines
2.2 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-24 11:12:15 +02:00
const types = ['full', 'country', 'content', 'sport']
2019-07-20 09:03:31 +02:00
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 14:27:40 +02:00
for(const type of types) {
util.createFile(`index.${type}.m3u`, '#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)
2019-07-20 14:27:40 +02:00
2019-07-20 09:03:31 +02:00
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 14:27:40 +02:00
for(const type of types) {
let groupTitle = ''
if(type === 'full') {
groupTitle = [ countryName, channel.group ].filter(i => i).join(';')
} else if(type === 'country') {
groupTitle = countryName
} else if(type === 'content') {
2019-07-20 14:27:40 +02:00
groupTitle = channel.group
2019-07-24 11:12:15 +02:00
} else {
groupTitle = channel.group
2019-07-20 14:27:40 +02:00
}
const info = `-1 tvg-id="${channel.id}" tvg-name="${channel.name}" tvg-logo="${channel.logo}" group-title="${groupTitle}",${channel.title}`
const data = '#EXTINF:' + info + '\n' + file + '\n'
2019-07-24 11:12:15 +02:00
if(type === 'sport') {
if(groupTitle === 'Sport') {
util.writeToFile(`index.${type}.m3u`, data)
}
} else {
util.writeToFile(`index.${type}.m3u`, data)
}
2019-07-20 14:27:40 +02:00
}
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
}