iptv/scripts/helper.js

273 lines
6.7 KiB
JavaScript
Raw Normal View History

2019-07-20 09:03:31 +02:00
const fs = require("fs")
const path = require('path')
2019-11-02 10:54:11 +01:00
const playlistParser = require('iptv-playlist-parser')
const axios = require('axios')
const zlib = require("zlib")
2019-10-30 16:44:26 +01:00
const epgParser = require('epg-parser')
const urlParser = require('url')
2019-11-01 15:15:33 +01:00
const ISO6391 = require('iso-639-1')
2019-11-02 10:45:09 +01:00
const escapeStringRegexp = require('escape-string-regexp')
const markdownInclude = require('markdown-include')
let cache = {}
2019-11-02 10:45:09 +01:00
let helper = {}
2019-11-02 13:43:41 +01:00
helper.sortBy = function(arr, fields) {
return arr.sort((a, b) => {
for(let field of fields) {
if(a[field].toLowerCase() < b[field].toLowerCase()) { return -1 }
if(a[field].toLowerCase() > b[field].toLowerCase()) { return 1 }
}
return 0
})
}
2019-11-02 11:26:21 +01:00
helper.createDir = function(dir) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
}
}
2019-11-02 10:45:09 +01:00
helper.compileMarkdown = function(filepath) {
return markdownInclude.compileFiles(path.resolve(__dirname, filepath))
}
2019-07-20 09:03:31 +02:00
2019-11-02 10:45:09 +01:00
helper.escapeStringRegexp = function(scring) {
return escapeStringRegexp(string)
}
2019-11-01 15:15:33 +01:00
2019-11-02 10:45:09 +01:00
helper.getISO6391Name = function(code) {
return ISO6391.getName(code)
}
2019-11-02 10:45:09 +01:00
helper.getISO6391Code = function(name) {
return ISO6391.getCode(name)
2019-07-20 09:03:31 +02:00
}
2019-11-02 10:45:09 +01:00
helper.parsePlaylist = function(filename) {
2019-11-02 10:54:11 +01:00
const content = this.readFile(filename)
const result = playlistParser.parse(content)
2019-08-07 15:51:34 +02:00
2019-10-07 01:45:06 +02:00
return new Playlist(result)
2019-07-20 09:03:31 +02:00
}
2019-11-02 10:45:09 +01:00
helper.loadEPG = async function(url) {
2019-10-30 16:44:26 +01:00
const content = await getEPGFile(url)
const result = epgParser.parse(content)
const channels = {}
for(let channel of result.channels) {
channels[channel.id] = channel
}
return Promise.resolve({
url,
2019-10-30 16:44:26 +01:00
channels
})
}
2019-11-02 10:45:09 +01:00
helper.getEPGFile = function(url) {
2019-08-07 15:51:34 +02:00
return new Promise((resolve, reject) => {
var buffer = []
axios({
method: 'get',
url: url,
responseType:'stream'
}).then(res => {
let stream
2019-10-30 16:44:26 +01:00
if(/\.gz$/i.test(url)) {
let gunzip = zlib.createGunzip()
res.data.pipe(gunzip)
stream = gunzip
} else {
stream = res.data
}
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)
})
}).catch(e => {
2019-08-07 15:51:34 +02:00
reject(e)
})
})
2019-07-20 09:03:31 +02:00
}
2019-11-02 10:45:09 +01:00
helper.readFile = function(filename) {
2019-08-07 15:51:34 +02:00
return fs.readFileSync(path.resolve(__dirname) + `/../${filename}`, { encoding: "utf8" })
}
2019-11-02 10:45:09 +01:00
helper.appendToFile = function(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-11-02 10:45:09 +01:00
helper.createFile = function(filename, data) {
2019-07-20 09:32:57 +02:00
fs.writeFileSync(path.resolve(__dirname) + '/../' + filename, data)
2019-07-20 09:03:31 +02:00
}
2019-11-02 10:45:09 +01:00
helper.getBasename = function(filename) {
return path.basename(filename, path.extname(filename))
}
2019-11-02 10:45:09 +01:00
helper.addToCache = function(url) {
2019-11-02 10:54:11 +01:00
let id = this.getUrlPath(url)
cache[id] = true
}
2019-11-02 10:45:09 +01:00
helper.checkCache = function(url) {
2019-11-02 10:54:11 +01:00
let id = this.getUrlPath(url)
return cache.hasOwnProperty(id)
}
2019-11-02 10:45:09 +01:00
helper.clearCache = function() {
2019-08-08 01:41:54 +02:00
cache = {}
}
2019-11-02 10:45:09 +01:00
helper.getUrlPath = function(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-09-07 23:51:06 +02:00
return path.toLowerCase()
}
2019-11-02 10:45:09 +01:00
helper.validateUrl = function(channelUrl) {
const url = new URL(channelUrl)
const host = url.hostname
2019-11-02 10:45:09 +01: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
]
return blacklist.indexOf(host) === -1
}
2019-11-02 10:45:09 +01:00
helper.skipPlaylist = function(filename) {
2019-09-09 03:02:21 +02:00
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-11-02 10:45:09 +01:00
helper.generateTable = function(data, options) {
2019-10-23 08:38:32 +02:00
let output = '<table>'
output += '<thead><tr>'
for (let column of options.columns) {
output += `<th align="${column.align}">${column.name}</th>`
}
output += '</tr></thead>'
output += '<tbody>'
for (let item of data) {
output += '<tr>'
let i = 0
for (let prop in item) {
const column = options.columns[i]
let nowrap = column.nowrap
let align = column.align
output += `<td align="${align}"${nowrap ? ' nowrap' : ''}>${item[prop]}</td>`
i++
}
output += '</tr>'
}
output += '</tbody>'
output += '</table>'
return output
2019-10-09 04:33:52 +02:00
}
2019-11-02 15:30:12 +01:00
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
})
}
2019-11-02 10:45:09 +01:00
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() {
2019-11-02 15:21:19 +01:00
const country = this.countryCode.toUpperCase()
2019-11-02 15:30:12 +01:00
const epg = (this.id && this.epg) ? this.epg : ''
const info = `-1 tvg-id="${this.id}" tvg-name="${this.name}" tvg-language="${this.language}" tvg-logo="${this.logo}" tvg-country="${country}" tvg-url="${epg}" group-title="${this.group}",${this.title}`
2019-11-02 15:21:19 +01:00
return '#EXTINF:' + info + '\n' + this.url + '\n'
}
toShortString() {
2019-11-02 10:45:09 +01:00
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