epg-grabber/src/utils.js

287 lines
8.5 KiB
JavaScript
Raw Normal View History

2021-03-13 13:11:33 +01:00
const fs = require('fs')
const path = require('path')
const axios = require('axios').default
2021-03-13 13:11:33 +01:00
const axiosCookieJarSupport = require('axios-cookiejar-support').default
const tough = require('tough-cookie')
const convert = require('xml-js')
const merge = require('lodash.merge')
2021-03-13 13:11:33 +01:00
const dayjs = require('dayjs')
const utc = require('dayjs/plugin/utc')
dayjs.extend(utc)
axiosCookieJarSupport(axios)
2021-10-13 22:10:27 +02:00
let timeout
2021-03-13 13:11:33 +01:00
const utils = {}
2021-08-21 18:11:01 +02:00
const defaultUserAgent =
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36 Edg/79.0.309.71'
2021-03-13 14:17:50 +01:00
2021-10-06 02:52:45 +02:00
utils.loadConfig = function (config) {
2021-03-13 14:42:18 +01:00
if (!config.site) throw new Error("The required 'site' property is missing")
if (!config.url) throw new Error("The required 'url' property is missing")
if (typeof config.url !== 'function' && typeof config.url !== 'string')
throw new Error("The 'url' property should return the function or string")
2021-03-13 14:42:18 +01:00
if (!config.parser) throw new Error("The required 'parser' function is missing")
if (typeof config.parser !== 'function')
throw new Error("The 'parser' property should return the function")
2021-03-19 19:26:32 +01:00
if (config.logo && typeof config.logo !== 'function')
throw new Error("The 'logo' property should return the function")
2021-03-13 14:42:18 +01:00
const defaultConfig = {
2021-10-06 02:52:45 +02:00
days: 1,
lang: 'en',
delay: 3000,
output: 'guide.xml',
request: {
method: 'GET',
2021-04-17 12:28:53 +02:00
maxContentLength: 5 * 1024 * 1024,
2021-03-21 19:16:27 +01:00
timeout: 5000,
withCredentials: true,
jar: new tough.CookieJar(),
responseType: 'arraybuffer'
}
}
return merge(defaultConfig, config)
2021-03-13 13:11:33 +01:00
}
2021-10-14 23:09:32 +02:00
utils.parseChannels = function (xml) {
2021-03-13 13:11:33 +01:00
const result = convert.xml2js(xml)
2021-10-14 23:09:32 +02:00
const siteTag = result.elements.find(el => el.name === 'site') || {}
if (!siteTag.elements) return []
const site = siteTag.attributes.site
const channelsTag = siteTag.elements.find(el => el.name === 'channels')
if (!channelsTag.elements) return []
2021-03-13 13:11:33 +01:00
2021-10-14 23:09:32 +02:00
const channels = channelsTag.elements
2021-03-13 13:11:33 +01:00
.filter(el => el.name === 'channel')
.map(el => {
const channel = el.attributes
if (!el.elements) throw new Error(`Channel '${channel.xmltv_id}' has no valid name`)
2021-03-13 13:11:33 +01:00
channel.name = el.elements.find(el => el.type === 'text').text
2021-11-18 15:32:04 +01:00
channel.site = channel.site || site
2021-03-13 13:11:33 +01:00
return channel
})
2021-10-14 23:09:32 +02:00
2021-11-18 15:32:04 +01:00
return channels
2021-03-13 13:11:33 +01:00
}
utils.sleep = function (ms) {
2021-10-05 23:38:38 +02:00
return new Promise(resolve => setTimeout(resolve, ms))
2021-03-13 13:11:33 +01:00
}
utils.escapeString = function (string, defaultValue = '') {
if (!string) return defaultValue
2021-03-20 15:03:17 +01:00
const regex = new RegExp(
'((?:[\0-\x08\x0B\f\x0E-\x1F\uFFFD\uFFFE\uFFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]))|([\\x7F-\\x84]|[\\x86-\\x9F]|[\\uFDD0-\\uFDEF]|(?:\\uD83F[\\uDFFE\\uDFFF])|(?:\\uD87F[\\uDF' +
'FE\\uDFFF])|(?:\\uD8BF[\\uDFFE\\uDFFF])|(?:\\uD8FF[\\uDFFE\\uDFFF])|(?:\\uD93F[\\uDFFE\\uD' +
'FFF])|(?:\\uD97F[\\uDFFE\\uDFFF])|(?:\\uD9BF[\\uDFFE\\uDFFF])|(?:\\uD9FF[\\uDFFE\\uDFFF])' +
'|(?:\\uDA3F[\\uDFFE\\uDFFF])|(?:\\uDA7F[\\uDFFE\\uDFFF])|(?:\\uDABF[\\uDFFE\\uDFFF])|(?:\\' +
'uDAFF[\\uDFFE\\uDFFF])|(?:\\uDB3F[\\uDFFE\\uDFFF])|(?:\\uDB7F[\\uDFFE\\uDFFF])|(?:\\uDBBF' +
'[\\uDFFE\\uDFFF])|(?:\\uDBFF[\\uDFFE\\uDFFF])(?:[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uD7FF\\' +
'uE000-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])|' +
'(?:[^\\uD800-\\uDBFF]|^)[\\uDC00-\\uDFFF]))',
'g'
)
string = String(string || '').replace(regex, '')
2021-03-13 13:11:33 +01:00
return string
.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;')
.replace(/\n|\r/g, ' ')
.replace(/ +/g, ' ')
.trim()
}
2021-10-06 15:38:20 +02:00
utils.convertToXMLTV = function ({ channels, programs }) {
2021-03-13 13:11:33 +01:00
let output = `<?xml version="1.0" encoding="UTF-8" ?><tv>\r\n`
for (let channel of channels) {
2021-10-06 15:38:20 +02:00
const id = utils.escapeString(channel['xmltv_id'])
const displayName = utils.escapeString(channel.name)
2021-03-19 19:26:32 +01:00
output += `<channel id="${id}"><display-name>${displayName}</display-name>`
if (channel.logo) {
2021-10-06 15:38:20 +02:00
const logo = utils.escapeString(channel.logo)
output += `<icon src="${logo}"/>`
2021-03-19 19:26:32 +01:00
}
2021-10-06 15:38:20 +02:00
if (channel.site) {
const url = channel.site ? 'https://' + channel.site : null
2021-09-22 16:12:26 +02:00
output += `<url>${url}</url>`
}
2021-03-19 19:26:32 +01:00
output += `</channel>\r\n`
2021-03-13 13:11:33 +01:00
}
for (let program of programs) {
if (!program) continue
2021-10-06 15:38:20 +02:00
const channel = utils.escapeString(program.channel)
const title = utils.escapeString(program.title)
const description = utils.escapeString(program.description)
2021-10-31 00:00:01 +02:00
const categories = Array.isArray(program.category) ? program.category : [program.category]
2021-03-13 13:11:33 +01:00
const start = program.start ? dayjs.utc(program.start).format('YYYYMMDDHHmmss ZZ') : ''
const stop = program.stop ? dayjs.utc(program.stop).format('YYYYMMDDHHmmss ZZ') : ''
2021-10-06 15:38:20 +02:00
const lang = program.lang || 'en'
const icon = utils.escapeString(program.icon)
2021-03-13 13:11:33 +01:00
2021-11-05 12:45:09 +01:00
if (start && stop && title) {
output += `<programme start="${start}" stop="${stop}" channel="${channel}"><title lang="${lang}">${title}</title>`
2021-03-13 13:11:33 +01:00
if (description) {
output += `<desc lang="${lang}">${description}</desc>`
}
2021-10-31 00:00:01 +02:00
if (categories.length) {
categories.forEach(category => {
2021-11-05 12:45:09 +01:00
if (category) {
output += `<category lang="${lang}">${utils.escapeString(category)}</category>`
}
2021-10-31 00:00:01 +02:00
})
2021-03-13 13:11:33 +01:00
}
if (icon) {
output += `<icon src="${icon}"/>`
}
2021-03-13 13:11:33 +01:00
output += '</programme>\r\n'
}
}
output += '</tv>'
return output
}
2021-03-13 14:17:50 +01:00
utils.writeToFile = function (filename, data) {
const dir = path.resolve(path.dirname(filename))
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true })
2021-03-13 13:11:33 +01:00
}
2021-03-13 13:49:04 +01:00
fs.writeFileSync(path.resolve(filename), data)
2021-03-13 13:11:33 +01:00
}
2021-08-21 18:11:01 +02:00
utils.buildRequest = async function (item, config) {
2021-10-13 22:02:06 +02:00
const CancelToken = axios.CancelToken
const source = CancelToken.source()
2021-04-02 21:00:33 +02:00
const request = { ...config.request }
2021-10-13 22:10:27 +02:00
timeout = setTimeout(() => {
2021-10-13 22:02:06 +02:00
source.cancel('Connection timeout')
}, request.timeout)
2021-08-21 18:11:01 +02:00
const headers = await utils.getRequestHeaders(item, config)
request.headers = { 'User-Agent': defaultUserAgent, ...headers }
request.url = await utils.getRequestUrl(item, config)
request.data = await utils.getRequestData(item, config)
2021-10-13 22:02:06 +02:00
request.cancelToken = source.token
2021-10-06 17:23:09 +02:00
if (config.debug) {
console.log('Request:', JSON.stringify(request, null, 2))
}
2021-08-21 18:11:01 +02:00
return request
}
utils.fetchData = function (request) {
2021-10-13 22:10:27 +02:00
axios.interceptors.response.use(
function (response) {
clearTimeout(timeout)
return response
},
function (error) {
clearTimeout(timeout)
return Promise.reject(error)
}
)
2021-04-02 21:00:33 +02:00
return axios(request)
2021-03-13 13:11:33 +01:00
}
2021-08-21 18:11:01 +02:00
utils.getRequestHeaders = async function (item, config) {
if (typeof config.request.headers === 'function') {
const headers = config.request.headers(item)
2021-08-23 12:45:52 +02:00
if (this.isPromise(headers)) {
2021-08-21 18:11:01 +02:00
return await headers
}
return headers
}
2021-10-05 23:38:38 +02:00
return config.request.headers || null
2021-08-21 18:11:01 +02:00
}
utils.getRequestData = async function (item, config) {
if (typeof config.request.data === 'function') {
const data = config.request.data(item)
2021-08-23 12:45:52 +02:00
if (this.isPromise(data)) {
2021-08-21 18:11:01 +02:00
return await data
}
return data
}
2021-10-05 23:38:38 +02:00
return config.request.data || null
2021-08-21 18:11:01 +02:00
}
utils.getRequestUrl = async function (item, config) {
if (typeof config.url === 'function') {
const url = config.url(item)
2021-08-23 12:45:52 +02:00
if (this.isPromise(url)) {
2021-08-21 18:11:01 +02:00
return await url
}
return url
}
return config.url
}
2021-03-13 13:11:33 +01:00
utils.getUTCDate = function () {
2021-09-23 12:27:12 +02:00
return dayjs.utc().startOf('d')
2021-03-13 13:11:33 +01:00
}
2021-08-23 12:45:52 +02:00
utils.parseResponse = async (item, response, config) => {
2021-10-05 23:38:38 +02:00
const data = merge(item, config, {
2021-08-23 12:45:52 +02:00
content: response.data.toString(),
buffer: response.data
})
if (!item.channel.logo && config.logo) {
2021-10-05 23:38:38 +02:00
item.channel.logo = await utils.loadLogo(data, config)
2021-08-23 12:45:52 +02:00
}
2021-10-05 23:38:38 +02:00
return await utils.parsePrograms(data, config)
2021-08-23 12:45:52 +02:00
}
2021-10-05 23:38:38 +02:00
utils.parsePrograms = async function (data, config) {
let programs = config.parser(data)
2021-08-23 12:45:52 +02:00
if (this.isPromise(programs)) {
programs = await programs
}
if (!Array.isArray(programs)) {
throw new Error('Parser should return an array')
}
2021-10-05 23:38:38 +02:00
const channel = data.channel
2021-08-23 12:45:52 +02:00
return programs
.filter(i => i)
.map(program => {
program.channel = channel.xmltv_id
2021-10-06 17:30:38 +02:00
program.lang = program.lang || channel.lang || config.lang || 'en'
2021-08-23 12:45:52 +02:00
return program
})
}
utils.loadLogo = async function (options, config) {
const logo = config.logo(options)
if (this.isPromise(logo)) {
return await logo
}
return logo
}
utils.isPromise = function (promise) {
return !!promise && typeof promise.then === 'function'
}
2021-03-13 13:11:33 +01:00
module.exports = utils