2019-07-20 09:03:31 +02:00
|
|
|
const fs = require("fs")
|
|
|
|
const path = require('path')
|
2019-10-06 04:12:18 +02:00
|
|
|
const parser = require('iptv-playlist-parser')
|
2019-08-09 12:52:54 +02:00
|
|
|
const axios = require('axios')
|
2019-08-07 14:44:12 +02:00
|
|
|
const zlib = require("zlib")
|
2019-08-07 16:28:11 +02:00
|
|
|
const DOMParser = require('xmldom').DOMParser
|
|
|
|
const urlParser = require('url')
|
|
|
|
|
2019-09-14 02:08:20 +02:00
|
|
|
const supportedCategories = [ 'Auto','Business', 'Classic','Comedy','Documentary','Education','Entertainment', 'Family','Fashion','Food', 'General', 'Health', 'History', 'Hobby', 'Kids', 'Legislative','Lifestyle','Local', 'Movies', 'Music', 'News', 'Quiz', 'Religious','Sci-Fi', 'Shop', 'Sport', 'Travel', 'Weather', 'XXX' ]
|
2019-08-08 02:29:36 +02:00
|
|
|
|
|
|
|
const blacklist = [
|
|
|
|
'80.80.160.168', // repeats on a loop
|
|
|
|
'63.237.48.3', // not a live stream
|
|
|
|
'189.216.247.113', // not working streams
|
|
|
|
]
|
|
|
|
|
2019-08-07 16:28:11 +02:00
|
|
|
let cache = {}
|
2019-08-07 14:44:12 +02:00
|
|
|
|
|
|
|
class Playlist {
|
|
|
|
constructor(data) {
|
|
|
|
this.attrs = data.attrs
|
|
|
|
this.items = data.items
|
2019-07-20 09:03:31 +02:00
|
|
|
}
|
|
|
|
|
2019-08-07 14:44:12 +02:00
|
|
|
getHeader() {
|
|
|
|
let parts = ['#EXTM3U']
|
|
|
|
for(let key in this.attrs) {
|
|
|
|
let value = this.attrs[key]
|
|
|
|
parts.push(`${key}="${value}"`)
|
|
|
|
}
|
2019-07-20 09:03:31 +02:00
|
|
|
|
2019-08-07 14:44:12 +02:00
|
|
|
return `${parts.join(' ')}\n`
|
2019-07-20 09:03:31 +02:00
|
|
|
}
|
2019-08-07 14:44:12 +02:00
|
|
|
}
|
2019-07-20 09:03:31 +02:00
|
|
|
|
2019-08-07 14:44:12 +02:00
|
|
|
class Channel {
|
|
|
|
constructor(data) {
|
2019-08-07 15:20:56 +02:00
|
|
|
this.id = data.id || ''
|
|
|
|
this.name = data.name || ''
|
|
|
|
this.logo = data.logo || ''
|
2019-08-07 14:44:12 +02:00
|
|
|
this.group = this._getGroup(data.group)
|
|
|
|
this.url = data.url
|
|
|
|
this.title = data.title
|
2019-07-20 09:03:31 +02:00
|
|
|
}
|
|
|
|
|
2019-08-07 14:44:12 +02:00
|
|
|
_getGroup(groupTitle) {
|
2019-08-10 19:35:43 +02:00
|
|
|
if(!groupTitle) return ''
|
|
|
|
|
2019-09-14 02:08:20 +02:00
|
|
|
const groupIndex = supportedCategories.map(g => g.toLowerCase()).indexOf(groupTitle.toLowerCase())
|
2019-07-20 09:03:31 +02:00
|
|
|
|
|
|
|
if(groupIndex === -1) {
|
|
|
|
groupTitle = ''
|
|
|
|
} else {
|
2019-09-14 02:08:20 +02:00
|
|
|
groupTitle = supportedCategories[groupIndex]
|
2019-07-20 09:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return groupTitle
|
|
|
|
}
|
|
|
|
|
2019-08-07 14:44:12 +02:00
|
|
|
toString() {
|
|
|
|
const info = `-1 tvg-id="${this.id}" tvg-name="${this.name}" tvg-logo="${this.logo}" group-title="${this.group}",${this.title}`
|
|
|
|
|
|
|
|
return '#EXTINF:' + info + '\n' + this.url + '\n'
|
2019-07-20 09:03:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 15:51:34 +02:00
|
|
|
function parsePlaylist(filename) {
|
|
|
|
const content = readFile(filename)
|
2019-10-06 04:12:18 +02:00
|
|
|
const result = parser.parse(content)
|
2019-08-07 15:51:34 +02:00
|
|
|
|
2019-10-06 04:12:18 +02:00
|
|
|
console.log(result)
|
2019-08-07 15:51:34 +02:00
|
|
|
|
|
|
|
return new Playlist({
|
|
|
|
attrs: results.attrs,
|
|
|
|
items: results.segments
|
2019-08-07 14:44:12 +02:00
|
|
|
})
|
2019-07-20 09:03:31 +02:00
|
|
|
}
|
|
|
|
|
2019-08-07 15:51:34 +02:00
|
|
|
function createChannel(data) {
|
|
|
|
return new Channel({
|
|
|
|
id: data.id,
|
|
|
|
name: data.name,
|
|
|
|
logo: data.logo,
|
|
|
|
group: data.group,
|
|
|
|
url: data.url,
|
|
|
|
title: data.title
|
|
|
|
})
|
2019-08-07 14:44:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function loadEPG(url) {
|
|
|
|
const data = await getGzipped(url)
|
|
|
|
const doc = new DOMParser().parseFromString(data, 'text/xml')
|
|
|
|
const channelElements = doc.getElementsByTagName('channel')
|
|
|
|
let channels = {}
|
|
|
|
for(let i = 0; i < channelElements.length; i++) {
|
|
|
|
let channel = {}
|
|
|
|
let channelElement = channelElements[i]
|
|
|
|
channel.id = channelElement.getAttribute('id')
|
|
|
|
channel.names = []
|
|
|
|
for(let nameElement of Object.values(channelElement.getElementsByTagName('display-name'))) {
|
|
|
|
if(nameElement.firstChild) {
|
|
|
|
channel.names.push(nameElement.firstChild.nodeValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
channel.names = channel.names.filter(n => n)
|
|
|
|
const iconElements = channelElement.getElementsByTagName('icon')
|
|
|
|
if(iconElements.length) {
|
|
|
|
channel.icon = iconElements[0].getAttribute('src')
|
|
|
|
}
|
2019-07-20 09:03:31 +02:00
|
|
|
|
2019-08-07 14:44:12 +02:00
|
|
|
channels[channel.id] = channel
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve({
|
|
|
|
url,
|
|
|
|
channels
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-07 15:51:34 +02:00
|
|
|
function getGzipped(url) {
|
2019-08-16 13:53:44 +02:00
|
|
|
const supportedTypes = ['application/x-gzip', 'application/octet-stream']
|
|
|
|
|
2019-08-07 15:51:34 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
var buffer = []
|
2019-08-09 12:52:54 +02:00
|
|
|
axios({
|
|
|
|
method: 'get',
|
|
|
|
url: url,
|
|
|
|
responseType:'stream'
|
|
|
|
}).then(res => {
|
2019-08-14 23:42:09 +02:00
|
|
|
let stream
|
2019-08-16 13:53:44 +02:00
|
|
|
if(supportedTypes.indexOf(res.headers['content-type']) > -1) {
|
2019-08-14 23:42:09 +02:00
|
|
|
let gunzip = zlib.createGunzip()
|
|
|
|
res.data.pipe(gunzip)
|
|
|
|
stream = gunzip
|
2019-08-16 13:53:44 +02:00
|
|
|
} else {
|
|
|
|
stream = res.data
|
2019-08-14 23:42:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
stream.on('data', function(data) {
|
2019-08-07 15:51:34 +02:00
|
|
|
buffer.push(data.toString())
|
|
|
|
}).on("end", function() {
|
|
|
|
resolve(buffer.join(""))
|
|
|
|
}).on("error", function(e) {
|
|
|
|
reject(e)
|
|
|
|
})
|
2019-08-09 12:52:54 +02:00
|
|
|
}).catch(e => {
|
2019-08-07 15:51:34 +02:00
|
|
|
reject(e)
|
|
|
|
})
|
2019-08-07 14:44:12 +02:00
|
|
|
})
|
2019-07-20 09:03:31 +02:00
|
|
|
}
|
|
|
|
|
2019-09-07 23:51:06 +02:00
|
|
|
function byTitleAndUrl(a, b) {
|
|
|
|
var titleA = a.title.toLowerCase()
|
|
|
|
var titleB = b.title.toLowerCase()
|
|
|
|
var urlA = a.url.toLowerCase()
|
|
|
|
var urlB = b.url.toLowerCase()
|
|
|
|
|
|
|
|
if(titleA < titleB) return -1
|
|
|
|
if(titleA > titleB) return 1
|
|
|
|
|
|
|
|
if(urlA < urlB) return -1
|
|
|
|
if(urlA > urlB) return 1
|
2019-07-20 09:03:31 +02:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-09-07 23:51:06 +02:00
|
|
|
function sortByTitleAndUrl(arr) {
|
|
|
|
return arr.sort(byTitleAndUrl)
|
2019-07-20 09:03:31 +02:00
|
|
|
}
|
|
|
|
|
2019-08-07 15:51:34 +02:00
|
|
|
function readFile(filename) {
|
|
|
|
return fs.readFileSync(path.resolve(__dirname) + `/../${filename}`, { encoding: "utf8" })
|
|
|
|
}
|
|
|
|
|
2019-08-07 14:44:12 +02:00
|
|
|
function appendToFile(filename, data) {
|
2019-07-20 09:32:57 +02:00
|
|
|
fs.appendFileSync(path.resolve(__dirname) + '/../' + filename, data)
|
2019-07-20 09:03:31 +02:00
|
|
|
}
|
|
|
|
|
2019-07-20 09:32:57 +02:00
|
|
|
function createFile(filename, data) {
|
|
|
|
fs.writeFileSync(path.resolve(__dirname) + '/../' + filename, data)
|
2019-07-20 09:03:31 +02:00
|
|
|
}
|
|
|
|
|
2019-08-07 16:28:11 +02:00
|
|
|
function getBasename(filename) {
|
|
|
|
return path.basename(filename, path.extname(filename))
|
|
|
|
}
|
|
|
|
|
|
|
|
function addToCache(url) {
|
|
|
|
let id = getUrlPath(url)
|
|
|
|
|
|
|
|
cache[id] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkCache(url) {
|
|
|
|
let id = getUrlPath(url)
|
|
|
|
|
|
|
|
return cache.hasOwnProperty(id)
|
|
|
|
}
|
|
|
|
|
2019-08-08 01:41:54 +02:00
|
|
|
function clearCache() {
|
|
|
|
cache = {}
|
|
|
|
}
|
|
|
|
|
2019-08-07 16:28:11 +02:00
|
|
|
function getUrlPath(u) {
|
|
|
|
let parsed = urlParser.parse(u)
|
|
|
|
let searchQuery = parsed.search || ''
|
2019-09-07 23:51:06 +02:00
|
|
|
let path = parsed.host + parsed.pathname + searchQuery
|
2019-08-07 16:28:11 +02:00
|
|
|
|
2019-09-07 23:51:06 +02:00
|
|
|
return path.toLowerCase()
|
2019-08-07 16:28:11 +02:00
|
|
|
}
|
|
|
|
|
2019-08-08 02:29:36 +02:00
|
|
|
function validateUrl(channelUrl) {
|
|
|
|
const url = new URL(channelUrl)
|
|
|
|
const host = url.hostname
|
|
|
|
|
|
|
|
return blacklist.indexOf(host) === -1
|
|
|
|
}
|
2019-08-07 16:28:11 +02:00
|
|
|
|
2019-09-09 03:02:21 +02:00
|
|
|
function skipPlaylist(filename) {
|
|
|
|
let testCountry = process.env.npm_config_country
|
|
|
|
let excludeList = process.env.npm_config_exclude
|
|
|
|
let excludeCountries = excludeList ? excludeList.split(',') : []
|
|
|
|
|
|
|
|
if (testCountry && filename !== 'channels/' + testCountry + '.m3u') return true
|
|
|
|
|
|
|
|
for(const countryCode of excludeCountries) {
|
|
|
|
if (filename === 'channels/' + countryCode + '.m3u') return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-07-20 09:03:31 +02:00
|
|
|
module.exports = {
|
|
|
|
parsePlaylist,
|
2019-09-07 23:51:06 +02:00
|
|
|
sortByTitleAndUrl,
|
2019-08-07 14:44:12 +02:00
|
|
|
appendToFile,
|
2019-07-20 09:03:31 +02:00
|
|
|
createFile,
|
2019-08-07 14:44:12 +02:00
|
|
|
readFile,
|
|
|
|
loadEPG,
|
2019-08-07 16:28:11 +02:00
|
|
|
createChannel,
|
|
|
|
getBasename,
|
|
|
|
addToCache,
|
2019-08-08 01:41:54 +02:00
|
|
|
checkCache,
|
2019-08-08 02:29:36 +02:00
|
|
|
clearCache,
|
2019-09-09 03:02:21 +02:00
|
|
|
validateUrl,
|
2019-09-14 02:08:20 +02:00
|
|
|
skipPlaylist,
|
|
|
|
supportedCategories
|
2019-07-20 09:03:31 +02:00
|
|
|
}
|