iptv/scripts/helper.js

406 lines
9.4 KiB
JavaScript
Raw Normal View History

2020-04-11 03:33:53 +02:00
const fs = require('fs')
2019-07-20 09:03:31 +02:00
const path = require('path')
2019-11-02 10:54:11 +01:00
const playlistParser = require('iptv-playlist-parser')
const axios = require('axios')
2020-04-11 03:33:53 +02:00
const zlib = require('zlib')
2019-10-30 16:44:26 +01:00
const epgParser = require('epg-parser')
const urlParser = require('url')
2019-11-02 10:45:09 +01:00
const escapeStringRegexp = require('escape-string-regexp')
const markdownInclude = require('markdown-include')
2020-04-18 16:16:56 +02:00
const iso6393 = require('iso-639-3')
2019-11-02 10:45:09 +01:00
let helper = {}
2020-05-06 17:01:36 +02:00
helper.code2flag = function (code) {
switch (code) {
case 'uk':
return '🇬🇧'
case 'int':
return '🌎'
2020-09-17 22:30:28 +02:00
case 'unsorted':
return ''
2020-05-06 17:01:36 +02:00
default:
return code
.toUpperCase()
.replace(/./g, char => String.fromCodePoint(char.charCodeAt(0) + 127397))
}
}
2020-04-11 03:33:53 +02:00
helper.sortBy = function (arr, fields) {
2019-11-02 13:43:41 +01:00
return arr.sort((a, b) => {
2020-04-11 03:33:53 +02:00
for (let field of fields) {
2020-05-04 15:23:03 +02:00
let propA = a[field] ? a[field].toLowerCase() : ''
let propB = b[field] ? b[field].toLowerCase() : ''
if (propA < propB) {
2020-04-11 03:33:53 +02:00
return -1
}
2020-05-04 15:23:03 +02:00
if (propA > propB) {
2020-04-11 03:33:53 +02:00
return 1
}
2019-11-02 13:43:41 +01:00
}
return 0
})
}
2020-04-11 03:33:53 +02:00
helper.createDir = function (dir) {
2019-11-02 11:26:21 +01:00
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
}
}
2020-04-11 03:33:53 +02:00
helper.compileMarkdown = function (filepath) {
2019-11-02 10:45:09 +01:00
return markdownInclude.compileFiles(path.resolve(__dirname, filepath))
}
2019-07-20 09:03:31 +02:00
2020-04-11 03:33:53 +02:00
helper.escapeStringRegexp = function (scring) {
2019-11-02 10:45:09 +01:00
return escapeStringRegexp(string)
}
2019-11-01 15:15:33 +01:00
2020-04-11 03:33:53 +02:00
helper.getISO6391Name = function (code) {
2020-05-04 15:23:03 +02:00
const lang = iso6393.find(l => l.iso6393 === code.toLowerCase())
2020-04-18 16:16:56 +02:00
return lang && lang.name ? lang.name : null
2019-11-02 10:45:09 +01:00
}
2020-04-11 03:33:53 +02:00
helper.getISO6391Code = function (name) {
2020-05-04 15:23:03 +02:00
const lang = iso6393.find(l => l.name === name)
2020-04-18 16:16:56 +02:00
return lang && lang.iso6393 ? lang.iso6393 : null
2019-07-20 09:03:31 +02:00
}
2020-04-11 03:33:53 +02: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
}
2020-04-11 03:33:53 +02:00
helper.parseEPG = async function (url) {
2021-01-11 11:51:39 +01:00
return this.getEPG(url).then(content => {
const result = epgParser.parse(content)
console.log('wo')
const channels = {}
for (let channel of result.channels) {
channels[channel.id] = channel
}
2021-01-11 11:51:39 +01:00
return { url, channels }
})
}
2020-04-11 03:33:53 +02:00
helper.getEPG = function (url) {
2019-08-07 15:51:34 +02:00
return new Promise((resolve, reject) => {
var buffer = []
axios({
method: 'get',
url: url,
2019-11-03 17:00:57 +01:00
responseType: 'stream',
2020-05-04 15:23:03 +02:00
timeout: 60000
2020-04-11 03:33:53 +02:00
})
2020-05-04 15:23:03 +02:00
.then(res => {
2020-04-11 03:33:53 +02:00
let stream
if (/\.gz$/i.test(url)) {
let gunzip = zlib.createGunzip()
res.data.pipe(gunzip)
stream = gunzip
} else {
stream = res.data
}
stream
.on('data', function (data) {
buffer.push(data.toString())
})
.on('end', function () {
resolve(buffer.join(''))
})
.on('error', function (e) {
reject(e)
})
})
2020-05-04 15:23:03 +02:00
.catch(e => {
2019-08-07 15:51:34 +02:00
reject(e)
})
})
2019-07-20 09:03:31 +02:00
}
2020-04-11 03:33:53 +02:00
helper.readFile = function (filename) {
return fs.readFileSync(path.resolve(__dirname) + `/../${filename}`, { encoding: 'utf8' })
2019-08-07 15:51:34 +02:00
}
2020-04-11 03:33:53 +02: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
}
2020-04-11 03:33:53 +02: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
}
2020-04-11 03:33:53 +02:00
helper.getBasename = function (filename) {
return path.basename(filename, path.extname(filename))
}
2020-04-11 03:33:53 +02: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()
}
2020-04-11 03:33:53 +02:00
helper.generateTable = function (data, options) {
let output = '<table>\n'
2019-10-23 08:38:32 +02:00
output += '\t<thead>\n\t\t<tr>'
2019-10-23 08:38:32 +02:00
for (let column of options.columns) {
output += `<th align="${column.align}">${column.name}</th>`
}
output += '</tr>\n\t</thead>\n'
2019-10-23 08:38:32 +02:00
output += '\t<tbody>\n'
2019-10-23 08:38:32 +02:00
for (let item of data) {
output += '\t\t<tr>'
2019-10-23 08:38:32 +02:00
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>\n'
2019-10-23 08:38:32 +02:00
}
output += '\t</tbody>\n'
2019-10-23 08:38:32 +02:00
output += '</table>'
return output
2019-10-09 04:33:52 +02:00
}
2020-04-11 03:33:53 +02:00
helper.createChannel = function (data) {
return new Channel(data)
2019-11-02 15:30:12 +01:00
}
2020-04-11 03:33:53 +02:00
helper.writeToLog = function (country, msg, url) {
2019-11-03 18:21:35 +01:00
var now = new Date()
var line = `${country}: ${msg} '${url}'`
this.appendToFile('error.log', now.toISOString() + ' ' + line + '\n')
}
helper.filterPlaylists = function (arr, include = '', exclude = '') {
if (include) {
2020-05-04 15:23:03 +02:00
const included = include.split(',').map(filename => `channels/${filename}.m3u`)
2020-05-04 15:23:03 +02:00
return arr.filter(i => included.indexOf(i.url) > -1)
}
if (exclude) {
2020-05-04 15:23:03 +02:00
const excluded = exclude.split(',').map(filename => `channels/${filename}.m3u`)
2020-05-04 15:23:03 +02:00
return arr.filter(i => excluded.indexOf(i.url) === -1)
}
return arr
}
2020-05-04 15:23:03 +02:00
helper.filterGroup = function (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
}
2021-01-19 23:02:24 +01:00
helper.sleep = function (ms) {
return function (x) {
return new Promise(resolve => setTimeout(() => resolve(x), ms))
}
}
2019-11-02 10:45:09 +01:00
class Playlist {
constructor(data) {
this.header = data.header
this.items = data.items
}
getHeader() {
let parts = ['#EXTM3U']
2020-04-11 03:33:53 +02:00
for (let key in this.header.attrs) {
2019-11-02 10:45:09 +01:00
let value = this.header.attrs[key]
2020-04-11 03:33:53 +02:00
if (value) {
2019-11-02 10:45:09 +01:00
parts.push(`${key}="${value}"`)
}
}
return `${parts.join(' ')}\n`
}
}
class Channel {
constructor(data) {
2020-05-04 15:23:03 +02:00
this.parseData(data)
}
parseData(data) {
this.logo = data.tvg.logo
2020-05-04 15:23:03 +02:00
this.category = helper.filterGroup(data.group.title)
2019-11-02 10:45:09 +01:00
this.url = data.url
2021-01-11 11:51:39 +01:00
this.name = this.parseName(data.name)
this.status = this.parseStatus(data.name)
2020-05-04 15:23:03 +02:00
this.http = data.http
this.tvg = data.tvg
this.country = {
code: null,
name: null
}
2021-01-11 11:51:39 +01:00
this.resolution = this.parseResolution(data.name)
2020-05-04 15:31:58 +02:00
this.setLanguage(data.tvg.language)
2019-11-02 10:45:09 +01:00
}
2020-05-04 15:23:03 +02:00
get ['language.name']() {
return this.language[0] ? this.language[0].name : null
}
2019-11-02 10:45:09 +01:00
2020-05-04 15:23:03 +02:00
get ['country.name']() {
return this.country.name || null
2019-11-02 10:45:09 +01:00
}
2021-01-11 11:51:39 +01:00
parseName(title) {
return title
.trim()
.split(' ')
.map(s => s.trim())
.filter(s => {
return !/\[|\]/i.test(s) && !/\((\d+)P\)/i.test(s)
})
.join(' ')
}
parseStatus(title) {
const regex = /\[(.*)\]/i
const match = title.match(regex)
return match ? match[1] : null
}
parseResolution(title) {
const regex = /\((\d+)P\)/i
const match = title.match(regex)
return {
width: null,
height: match ? parseInt(match[1]) : null
}
}
2020-05-04 15:31:58 +02:00
setLanguage(lang) {
this.language = lang
.split(';')
.map(name => {
const code = name ? helper.getISO6391Code(name) : null
if (!code) return null
return {
code,
name
}
})
.filter(l => l)
}
2019-11-02 10:45:09 +01:00
toString() {
2020-05-04 15:23:03 +02:00
const country = this.country.code ? this.country.code.toUpperCase() : ''
const tvgUrl = (this.tvg.id || this.tvg.name) && this.tvg.url ? this.tvg.url : ''
const language = this.language.map(l => l.name).join(';')
2021-01-11 11:51:39 +01:00
const resolution = this.resolution.height ? ` (${this.resolution.height}p)` : ''
const status = this.status ? ` [${this.status}]` : ''
2021-01-11 11:51:39 +01:00
let info = `-1 tvg-id="${this.tvg.id}" tvg-name="${this.tvg.name}" tvg-language="${language}" tvg-logo="${this.logo}" tvg-country="${country}" tvg-url="${tvgUrl}" group-title="${this.category}",${this.name}${resolution}${status}`
2020-05-04 15:23:03 +02:00
if (this.http['referrer']) {
info += `\n#EXTVLCOPT:http-referrer=${this.http['referrer']}`
}
2020-05-04 15:23:03 +02:00
if (this.http['user-agent']) {
info += `\n#EXTVLCOPT:http-user-agent=${this.http['user-agent']}`
}
2019-11-02 15:21:19 +01:00
return '#EXTINF:' + info + '\n' + this.url + '\n'
}
toShortString() {
2020-05-04 15:23:03 +02:00
const language = this.language.map(l => l.name).join(';')
2021-01-11 11:51:39 +01:00
const resolution = this.resolution.height ? ` (${this.resolution.height}p)` : ''
const status = this.status ? ` [${this.status}]` : ''
2020-05-04 15:23:03 +02:00
2021-01-11 11:51:39 +01:00
let info = `-1 tvg-id="${this.tvg.id}" tvg-name="${this.tvg.name}" tvg-language="${language}" tvg-logo="${this.logo}" group-title="${this.category}",${this.name}${resolution}${status}`
2020-05-04 15:23:03 +02:00
if (this.http['referrer']) {
info += `\n#EXTVLCOPT:http-referrer=${this.http['referrer']}`
}
2020-05-04 15:23:03 +02:00
if (this.http['user-agent']) {
info += `\n#EXTVLCOPT:http-user-agent=${this.http['user-agent']}`
}
2019-11-02 10:45:09 +01:00
return '#EXTINF:' + info + '\n' + this.url + '\n'
}
2020-05-04 15:23:03 +02:00
toJSON() {
return {
name: this.name,
logo: this.logo || null,
url: this.url,
category: this.category || null,
language: this.language,
country: this.country,
tvg: {
id: this.tvg.id || null,
name: this.tvg.name || null,
url: this.tvg.url || null
}
}
}
2019-11-02 10:45:09 +01:00
}
2020-04-11 03:33:53 +02:00
module.exports = helper