|
|
|
@ -6,88 +6,48 @@ const zlib = require("zlib")
|
|
|
|
|
const epgParser = require('epg-parser')
|
|
|
|
|
const urlParser = require('url')
|
|
|
|
|
const ISO6391 = require('iso-639-1')
|
|
|
|
|
|
|
|
|
|
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' ]
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
]
|
|
|
|
|
const escapeStringRegexp = require('escape-string-regexp')
|
|
|
|
|
const markdownInclude = require('markdown-include')
|
|
|
|
|
|
|
|
|
|
let cache = {}
|
|
|
|
|
let helper = {}
|
|
|
|
|
|
|
|
|
|
class Playlist {
|
|
|
|
|
constructor(data) {
|
|
|
|
|
this.header = data.header
|
|
|
|
|
this.items = data.items
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getHeader() {
|
|
|
|
|
let parts = ['#EXTM3U']
|
|
|
|
|
for(let key in this.header.attrs) {
|
|
|
|
|
let value = this.header.attrs[key]
|
|
|
|
|
if(value) {
|
|
|
|
|
parts.push(`${key}="${value}"`)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return `${parts.join(' ')}\n`
|
|
|
|
|
}
|
|
|
|
|
helper.compileMarkdown = function(filepath) {
|
|
|
|
|
return markdownInclude.compileFiles(path.resolve(__dirname, filepath))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Channel {
|
|
|
|
|
constructor(data) {
|
|
|
|
|
this.id = data.tvg.id
|
|
|
|
|
this.name = data.tvg.name
|
|
|
|
|
this.language = this._filterLanguage(data.tvg.language)
|
|
|
|
|
this.logo = data.tvg.logo
|
|
|
|
|
this.group = this._filterGroup(data.group.title)
|
|
|
|
|
this.url = data.url
|
|
|
|
|
this.title = data.name
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_filterGroup(groupTitle) {
|
|
|
|
|
if(!groupTitle) return ''
|
|
|
|
|
|
|
|
|
|
const groupIndex = supportedCategories.map(g => g.toLowerCase()).indexOf(groupTitle.toLowerCase())
|
|
|
|
|
|
|
|
|
|
if(groupIndex === -1) {
|
|
|
|
|
groupTitle = ''
|
|
|
|
|
} else {
|
|
|
|
|
groupTitle = supportedCategories[groupIndex]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return groupTitle
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_filterLanguage(languageName) {
|
|
|
|
|
if(ISO6391.getCode(languageName) !== '') {
|
|
|
|
|
return languageName
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ''
|
|
|
|
|
}
|
|
|
|
|
helper.escapeStringRegexp = function(scring) {
|
|
|
|
|
return escapeStringRegexp(string)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
|
const info = `-1 tvg-id="${this.id}" tvg-name="${this.name}" tvg-language="${this.language}" tvg-logo="${this.logo}" group-title="${this.group}",${this.title}`
|
|
|
|
|
helper.getISO6391Name = function(code) {
|
|
|
|
|
return ISO6391.getName(code)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return '#EXTINF:' + info + '\n' + this.url + '\n'
|
|
|
|
|
}
|
|
|
|
|
helper.getISO6391Code = function(name) {
|
|
|
|
|
return ISO6391.getCode(name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function parsePlaylist(filename) {
|
|
|
|
|
helper.parsePlaylist = function(filename) {
|
|
|
|
|
const content = readFile(filename)
|
|
|
|
|
const result = parser.parse(content)
|
|
|
|
|
|
|
|
|
|
return new Playlist(result)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createChannel(data) {
|
|
|
|
|
return new Channel(data)
|
|
|
|
|
helper.createChannel = function(data) {
|
|
|
|
|
return new Channel({
|
|
|
|
|
id: data.tvg.id,
|
|
|
|
|
name: data.tvg.name,
|
|
|
|
|
language: data.tvg.language,
|
|
|
|
|
logo: data.tvg.logo,
|
|
|
|
|
group: data.group.title,
|
|
|
|
|
url: data.url,
|
|
|
|
|
title: data.name
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function loadEPG(url) {
|
|
|
|
|
helper.loadEPG = async function(url) {
|
|
|
|
|
const content = await getEPGFile(url)
|
|
|
|
|
const result = epgParser.parse(content)
|
|
|
|
|
const channels = {}
|
|
|
|
@ -101,7 +61,7 @@ async function loadEPG(url) {
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getEPGFile(url) {
|
|
|
|
|
helper.getEPGFile = function(url) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
var buffer = []
|
|
|
|
|
axios({
|
|
|
|
@ -131,58 +91,56 @@ function getEPGFile(url) {
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
helper.sortByTitleAndUrl = function(arr) {
|
|
|
|
|
return arr.sort(function(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
|
|
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
if(urlA < urlB) return -1
|
|
|
|
|
if(urlA > urlB) return 1
|
|
|
|
|
|
|
|
|
|
function sortByTitleAndUrl(arr) {
|
|
|
|
|
return arr.sort(byTitleAndUrl)
|
|
|
|
|
return 0
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function readFile(filename) {
|
|
|
|
|
helper.readFile = function(filename) {
|
|
|
|
|
return fs.readFileSync(path.resolve(__dirname) + `/../${filename}`, { encoding: "utf8" })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function appendToFile(filename, data) {
|
|
|
|
|
helper.appendToFile = function(filename, data) {
|
|
|
|
|
fs.appendFileSync(path.resolve(__dirname) + '/../' + filename, data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createFile(filename, data) {
|
|
|
|
|
helper.createFile = function(filename, data) {
|
|
|
|
|
fs.writeFileSync(path.resolve(__dirname) + '/../' + filename, data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getBasename(filename) {
|
|
|
|
|
helper.getBasename = function(filename) {
|
|
|
|
|
return path.basename(filename, path.extname(filename))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function addToCache(url) {
|
|
|
|
|
helper.addToCache = function(url) {
|
|
|
|
|
let id = getUrlPath(url)
|
|
|
|
|
|
|
|
|
|
cache[id] = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function checkCache(url) {
|
|
|
|
|
helper.checkCache = function(url) {
|
|
|
|
|
let id = getUrlPath(url)
|
|
|
|
|
|
|
|
|
|
return cache.hasOwnProperty(id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function clearCache() {
|
|
|
|
|
helper.clearCache = function() {
|
|
|
|
|
cache = {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getUrlPath(u) {
|
|
|
|
|
helper.getUrlPath = function(u) {
|
|
|
|
|
let parsed = urlParser.parse(u)
|
|
|
|
|
let searchQuery = parsed.search || ''
|
|
|
|
|
let path = parsed.host + parsed.pathname + searchQuery
|
|
|
|
@ -190,14 +148,19 @@ function getUrlPath(u) {
|
|
|
|
|
return path.toLowerCase()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function validateUrl(channelUrl) {
|
|
|
|
|
helper.validateUrl = function(channelUrl) {
|
|
|
|
|
const url = new URL(channelUrl)
|
|
|
|
|
const host = url.hostname
|
|
|
|
|
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
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
return blacklist.indexOf(host) === -1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function skipPlaylist(filename) {
|
|
|
|
|
helper.skipPlaylist = function(filename) {
|
|
|
|
|
let testCountry = process.env.npm_config_country
|
|
|
|
|
let excludeList = process.env.npm_config_exclude
|
|
|
|
|
let excludeCountries = excludeList ? excludeList.split(',') : []
|
|
|
|
@ -211,7 +174,7 @@ function skipPlaylist(filename) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function generateTable(data, options) {
|
|
|
|
|
helper.generateTable = function(data, options) {
|
|
|
|
|
let output = '<table>'
|
|
|
|
|
|
|
|
|
|
output += '<thead><tr>'
|
|
|
|
@ -240,20 +203,64 @@ function generateTable(data, options) {
|
|
|
|
|
return output
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
parsePlaylist,
|
|
|
|
|
sortByTitleAndUrl,
|
|
|
|
|
appendToFile,
|
|
|
|
|
createFile,
|
|
|
|
|
readFile,
|
|
|
|
|
loadEPG,
|
|
|
|
|
createChannel,
|
|
|
|
|
getBasename,
|
|
|
|
|
addToCache,
|
|
|
|
|
checkCache,
|
|
|
|
|
clearCache,
|
|
|
|
|
validateUrl,
|
|
|
|
|
skipPlaylist,
|
|
|
|
|
supportedCategories,
|
|
|
|
|
generateTable
|
|
|
|
|
}
|
|
|
|
|
class Playlist {
|
|
|
|
|
constructor(data) {
|
|
|
|
|
this.header = data.header
|
|
|
|
|
this.items = data.items
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getHeader() {
|
|
|
|
|
let parts = ['#EXTM3U']
|
|
|
|
|
for(let key in this.header.attrs) {
|
|
|
|
|
let value = this.header.attrs[key]
|
|
|
|
|
if(value) {
|
|
|
|
|
parts.push(`${key}="${value}"`)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return `${parts.join(' ')}\n`
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Channel {
|
|
|
|
|
constructor(data) {
|
|
|
|
|
this.id = data.id
|
|
|
|
|
this.name = data.name
|
|
|
|
|
this.language = this._filterLanguage(data.language)
|
|
|
|
|
this.logo = data.logo
|
|
|
|
|
this.group = this._filterGroup(data.group)
|
|
|
|
|
this.url = data.url
|
|
|
|
|
this.title = data.title
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_filterGroup(groupTitle) {
|
|
|
|
|
if(!groupTitle) return ''
|
|
|
|
|
|
|
|
|
|
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' ]
|
|
|
|
|
const groupIndex = supportedCategories.map(g => g.toLowerCase()).indexOf(groupTitle.toLowerCase())
|
|
|
|
|
|
|
|
|
|
if(groupIndex === -1) {
|
|
|
|
|
groupTitle = ''
|
|
|
|
|
} else {
|
|
|
|
|
groupTitle = supportedCategories[groupIndex]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return groupTitle
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_filterLanguage(languageName) {
|
|
|
|
|
if(ISO6391.getCode(languageName) !== '') {
|
|
|
|
|
return languageName
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ''
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
|
const info = `-1 tvg-id="${this.id}" tvg-name="${this.name}" tvg-language="${this.language}" tvg-logo="${this.logo}" group-title="${this.group}",${this.title}`
|
|
|
|
|
|
|
|
|
|
return '#EXTINF:' + info + '\n' + this.url + '\n'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = helper
|