wip
This commit is contained in:
parent
9332dcc592
commit
482e8c705e
|
@ -2,15 +2,13 @@
|
|||
|
||||
const { Command } = require('commander')
|
||||
const program = new Command()
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const EPGGrabber = require('../src/index')
|
||||
const utils = require('../src/utils')
|
||||
const { name, version, description } = require('../package.json')
|
||||
const { merge } = require('lodash')
|
||||
const { gzip } = require('node-gzip')
|
||||
const { createLogger, format, transports } = require('winston')
|
||||
const { combine, timestamp, printf } = format
|
||||
const file = require('../src/file')
|
||||
const EPGGrabber = require('../src/index')
|
||||
const { create: createLogger } = require('../src/logger')
|
||||
const { parseInteger, getUTCDate } = require('../src/utils')
|
||||
const { name, version, description } = require('../package.json')
|
||||
|
||||
program
|
||||
.name(name)
|
||||
|
@ -36,39 +34,13 @@ program
|
|||
.parse(process.argv)
|
||||
|
||||
const options = program.opts()
|
||||
|
||||
const fileFormat = printf(({ level, message, timestamp }) => {
|
||||
return `[${timestamp}] ${level.toUpperCase()}: ${message}`
|
||||
})
|
||||
|
||||
const consoleFormat = printf(({ level, message, timestamp }) => {
|
||||
if (level === 'error') return ` Error: ${message}`
|
||||
|
||||
return message
|
||||
})
|
||||
|
||||
const t = [new transports.Console({ format: consoleFormat })]
|
||||
|
||||
if (options.log) {
|
||||
t.push(
|
||||
new transports.File({
|
||||
filename: path.resolve(options.log),
|
||||
format: combine(timestamp(), fileFormat),
|
||||
options: { flags: 'w' }
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
const logger = createLogger({
|
||||
level: options.logLevel,
|
||||
transports: t
|
||||
})
|
||||
const logger = createLogger(options)
|
||||
|
||||
async function main() {
|
||||
logger.info('Starting...')
|
||||
|
||||
logger.info(`Loading '${options.config}'...`)
|
||||
let config = require(path.resolve(options.config))
|
||||
let config = require(file.resolve(options.config))
|
||||
config = merge(config, {
|
||||
days: options.days,
|
||||
debug: options.debug,
|
||||
|
@ -83,22 +55,27 @@ async function main() {
|
|||
if (options.cacheTtl) config.request.cache.ttl = options.cacheTtl
|
||||
if (options.channels) config.channels = options.channels
|
||||
else if (config.channels)
|
||||
config.channels = path.join(path.dirname(options.config), config.channels)
|
||||
config.channels = file.join(file.dirname(options.config), config.channels)
|
||||
else throw new Error("The required 'channels' property is missing")
|
||||
|
||||
if (!config.channels) return logger.error('Path to [site].channels.xml is missing')
|
||||
logger.info(`Loading '${config.channels}'...`)
|
||||
const channelsXML = fs.readFileSync(path.resolve(config.channels), { encoding: 'utf-8' })
|
||||
const { channels } = utils.parseChannels(channelsXML)
|
||||
const grabber = new EPGGrabber(config)
|
||||
|
||||
const channelsXML = file.read(config.channels)
|
||||
const { channels } = grabber.parseChannels(channelsXML)
|
||||
|
||||
let programs = []
|
||||
let i = 1
|
||||
let days = options.days || 1
|
||||
const total = channels.length * days
|
||||
const utcDate = utils.getUTCDate()
|
||||
const utcDate = getUTCDate()
|
||||
const dates = Array.from({ length: config.days }, (_, i) => utcDate.add(i, 'd'))
|
||||
const grabber = new EPGGrabber(config)
|
||||
for (let channel of channels) {
|
||||
if (!channel.logo && config.logo) {
|
||||
channel.logo = await grabber.loadLogo(channel)
|
||||
}
|
||||
|
||||
for (let date of dates) {
|
||||
await grabber
|
||||
.grab(channel, date, (data, err) => {
|
||||
|
@ -118,15 +95,15 @@ async function main() {
|
|||
}
|
||||
}
|
||||
|
||||
const xml = utils.convertToXMLTV({ config, channels, programs })
|
||||
const xml = grabber.generateXMLTV({ config, channels, programs })
|
||||
let outputPath = options.output || config.output
|
||||
if (options.gzip) {
|
||||
outputPath = outputPath || 'guide.xml.gz'
|
||||
const compressed = await gzip(xml)
|
||||
utils.writeToFile(outputPath, compressed)
|
||||
file.write(outputPath, compressed)
|
||||
} else {
|
||||
outputPath = outputPath || 'guide.xml'
|
||||
utils.writeToFile(outputPath, xml)
|
||||
file.write(outputPath, xml)
|
||||
}
|
||||
|
||||
logger.info(`File '${outputPath}' successfully saved`)
|
||||
|
@ -134,7 +111,3 @@ async function main() {
|
|||
}
|
||||
|
||||
main()
|
||||
|
||||
function parseInteger(val) {
|
||||
return val ? parseInt(val) : null
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,26 @@
|
|||
const convert = require('xml-js')
|
||||
|
||||
module.exports.parse = parse
|
||||
|
||||
function parse(xml) {
|
||||
const result = convert.xml2js(xml)
|
||||
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 []
|
||||
|
||||
const channels = channelsTag.elements
|
||||
.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`)
|
||||
channel.name = el.elements.find(el => el.type === 'text').text
|
||||
channel.site = channel.site || site
|
||||
|
||||
return channel
|
||||
})
|
||||
|
||||
return { site, channels }
|
||||
}
|
|
@ -0,0 +1,143 @@
|
|||
const { merge } = require('lodash')
|
||||
const { CurlGenerator } = require('curl-generator')
|
||||
const axios = require('axios').default
|
||||
const axiosCookieJarSupport = require('axios-cookiejar-support').default
|
||||
const { setupCache } = require('axios-cache-interceptor')
|
||||
const { isPromise, getUTCDate } = require('./utils')
|
||||
|
||||
axiosCookieJarSupport(axios)
|
||||
|
||||
module.exports.create = create
|
||||
module.exports.buildRequest = buildRequest
|
||||
module.exports.parseResponse = parseResponse
|
||||
|
||||
let timeout
|
||||
|
||||
class Client {
|
||||
constructor() {}
|
||||
}
|
||||
|
||||
function create(config) {
|
||||
const client = setupCache(
|
||||
axios.create({
|
||||
headers: {
|
||||
'User-Agent':
|
||||
'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'
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
client.interceptors.request.use(
|
||||
function (request) {
|
||||
if (config.debug) {
|
||||
console.log('Request:', JSON.stringify(request, null, 2))
|
||||
}
|
||||
return request
|
||||
},
|
||||
function (error) {
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
client.interceptors.response.use(
|
||||
function (response) {
|
||||
if (config.debug) {
|
||||
const data =
|
||||
isObject(response.data) || Array.isArray(response.data)
|
||||
? JSON.stringify(response.data)
|
||||
: response.data.toString()
|
||||
console.log(
|
||||
'Response:',
|
||||
JSON.stringify(
|
||||
{
|
||||
headers: response.headers,
|
||||
data,
|
||||
cached: response.cached
|
||||
},
|
||||
null,
|
||||
2
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
clearTimeout(timeout)
|
||||
return response
|
||||
},
|
||||
function (error) {
|
||||
clearTimeout(timeout)
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
async function buildRequest({ channel, date, config }) {
|
||||
date = typeof date === 'string' ? getUTCDate(date) : date
|
||||
const CancelToken = axios.CancelToken
|
||||
const source = CancelToken.source()
|
||||
const request = { ...config.request }
|
||||
timeout = setTimeout(() => {
|
||||
source.cancel('Connection timeout')
|
||||
}, request.timeout)
|
||||
request.headers = await getRequestHeaders({ channel, date, config })
|
||||
request.url = await getRequestUrl({ channel, date, config })
|
||||
request.data = await getRequestData({ channel, date, config })
|
||||
request.cancelToken = source.token
|
||||
|
||||
if (config.curl) {
|
||||
const curl = CurlGenerator({
|
||||
url: request.url,
|
||||
method: request.method,
|
||||
headers: request.headers,
|
||||
body: request.data
|
||||
})
|
||||
console.log(curl)
|
||||
}
|
||||
|
||||
return request
|
||||
}
|
||||
|
||||
function parseResponse(response) {
|
||||
return {
|
||||
content: response.data.toString(),
|
||||
buffer: response.data,
|
||||
headers: response.headers,
|
||||
request: response.request,
|
||||
cached: response.cached
|
||||
}
|
||||
}
|
||||
|
||||
async function getRequestHeaders({ channel, date, config }) {
|
||||
if (typeof config.request.headers === 'function') {
|
||||
const headers = config.request.headers({ channel, date })
|
||||
if (isPromise(headers)) {
|
||||
return await headers
|
||||
}
|
||||
return headers
|
||||
}
|
||||
|
||||
return config.request.headers || null
|
||||
}
|
||||
|
||||
async function getRequestData({ channel, date, config }) {
|
||||
if (typeof config.request.data === 'function') {
|
||||
const data = config.request.data({ channel, date })
|
||||
if (isPromise(data)) {
|
||||
return await data
|
||||
}
|
||||
return data
|
||||
}
|
||||
return config.request.data || null
|
||||
}
|
||||
|
||||
async function getRequestUrl({ channel, date, config }) {
|
||||
if (typeof config.url === 'function') {
|
||||
const url = config.url({ channel, date })
|
||||
if (isPromise(url)) {
|
||||
return await url
|
||||
}
|
||||
return url
|
||||
}
|
||||
return config.url
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
const tough = require('tough-cookie')
|
||||
const { merge } = require('lodash')
|
||||
|
||||
module.exports.load = load
|
||||
|
||||
function load(config) {
|
||||
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")
|
||||
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")
|
||||
if (config.logo && typeof config.logo !== 'function')
|
||||
throw new Error("The 'logo' property should return the function")
|
||||
|
||||
const defaultConfig = {
|
||||
days: 1,
|
||||
lang: 'en',
|
||||
delay: 3000,
|
||||
output: 'guide.xml',
|
||||
request: {
|
||||
method: 'GET',
|
||||
maxContentLength: 5 * 1024 * 1024,
|
||||
timeout: 5000,
|
||||
withCredentials: true,
|
||||
jar: new tough.CookieJar(),
|
||||
responseType: 'arraybuffer',
|
||||
cache: false
|
||||
}
|
||||
}
|
||||
|
||||
return merge(defaultConfig, config)
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
|
||||
module.exports.read = read
|
||||
module.exports.write = write
|
||||
module.exports.resolve = resolve
|
||||
module.exports.join = join
|
||||
module.exports.dirname = dirname
|
||||
|
||||
function read(filepath) {
|
||||
return fs.readFileSync(path.resolve(filepath), { encoding: 'utf-8' })
|
||||
}
|
||||
|
||||
function write(filepath, data) {
|
||||
const dir = path.resolve(path.dirname(filepath))
|
||||
if (!fs.existsSync(dir)) {
|
||||
fs.mkdirSync(dir, { recursive: true })
|
||||
}
|
||||
|
||||
fs.writeFileSync(path.resolve(filepath), data)
|
||||
}
|
||||
|
||||
function resolve(filepath) {
|
||||
return path.resolve(filepath)
|
||||
}
|
||||
|
||||
function join(path1, path2) {
|
||||
return path.join(path1, path2)
|
||||
}
|
||||
|
||||
function dirname(filepath) {
|
||||
return path.dirname(filepath)
|
||||
}
|
61
src/index.js
61
src/index.js
|
@ -1,41 +1,48 @@
|
|||
const utils = require('./utils')
|
||||
const { merge } = require('lodash')
|
||||
const { create: createClient, buildRequest, parseResponse } = require('./client')
|
||||
const { generate: generateXMLTV } = require('./xmltv')
|
||||
const { parse: parseChannels } = require('./channels')
|
||||
const { parse: parsePrograms } = require('./programs')
|
||||
const { load: loadConfig } = require('./config')
|
||||
const { sleep, isPromise } = require('./utils')
|
||||
|
||||
class EPGGrabber {
|
||||
constructor(config = {}) {
|
||||
this.config = utils.loadConfig(config)
|
||||
this.client = utils.createClient(config)
|
||||
this.config = loadConfig(config)
|
||||
this.client = createClient(config)
|
||||
}
|
||||
|
||||
async loadLogo(channel) {
|
||||
const logo = this.config.logo({ channel })
|
||||
if (isPromise(logo)) {
|
||||
return await logo
|
||||
}
|
||||
return logo
|
||||
}
|
||||
|
||||
async grab(channel, date, cb = () => {}) {
|
||||
date = typeof date === 'string' ? utils.getUTCDate(date) : date
|
||||
channel.lang = channel.lang || this.config.lang || null
|
||||
await sleep(this.config.delay)
|
||||
|
||||
let programs = []
|
||||
const item = { date, channel }
|
||||
await utils
|
||||
.buildRequest(item, this.config)
|
||||
.then(request => utils.fetchData(this.client, request))
|
||||
.then(response => utils.parseResponse(item, response, this.config))
|
||||
.then(results => {
|
||||
item.programs = results
|
||||
cb(item, null)
|
||||
programs = programs.concat(results)
|
||||
})
|
||||
.catch(error => {
|
||||
item.programs = []
|
||||
if (this.config.debug) {
|
||||
console.log('Error:', JSON.stringify(error, null, 2))
|
||||
}
|
||||
cb(item, error)
|
||||
})
|
||||
|
||||
await utils.sleep(this.config.delay)
|
||||
return buildRequest({ channel, date, config: this.config })
|
||||
.then(this.client)
|
||||
.then(parseResponse)
|
||||
.then(data => merge({ channel, date, config: this.config }, data))
|
||||
.then(parsePrograms)
|
||||
.then(programs => {
|
||||
cb({ channel, date, programs })
|
||||
|
||||
return programs
|
||||
})
|
||||
.catch(err => {
|
||||
if (this.config.debug) console.log('Error:', JSON.stringify(err, null, 2))
|
||||
cb({ channel, date, programs: [] }, err)
|
||||
|
||||
return []
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
EPGGrabber.convertToXMLTV = utils.convertToXMLTV
|
||||
EPGGrabber.parseChannels = utils.parseChannels
|
||||
EPGGrabber.prototype.generateXMLTV = generateXMLTV
|
||||
EPGGrabber.prototype.parseChannels = parseChannels
|
||||
|
||||
module.exports = EPGGrabber
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
const { createLogger, format, transports } = require('winston')
|
||||
const { combine, timestamp, printf } = format
|
||||
|
||||
module.exports.create = create
|
||||
|
||||
function create(options) {
|
||||
const fileFormat = printf(({ level, message, timestamp }) => {
|
||||
return `[${timestamp}] ${level.toUpperCase()}: ${message}`
|
||||
})
|
||||
|
||||
const consoleFormat = printf(({ level, message, timestamp }) => {
|
||||
if (level === 'error') return ` Error: ${message}`
|
||||
|
||||
return message
|
||||
})
|
||||
|
||||
const t = [new transports.Console({ format: consoleFormat })]
|
||||
|
||||
if (options.log) {
|
||||
t.push(
|
||||
new transports.File({
|
||||
filename: path.resolve(options.log),
|
||||
format: combine(timestamp(), fileFormat),
|
||||
options: { flags: 'w' }
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
return createLogger({
|
||||
level: options.logLevel,
|
||||
transports: t
|
||||
})
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
const dayjs = require('dayjs')
|
||||
const { isPromise } = require('./utils')
|
||||
|
||||
module.exports.parse = parse
|
||||
|
||||
async function parse(data) {
|
||||
const { config, channel } = data
|
||||
let programs = config.parser(data)
|
||||
|
||||
if (isPromise(programs)) {
|
||||
programs = await programs
|
||||
}
|
||||
|
||||
if (!Array.isArray(programs)) {
|
||||
throw new Error('Parser should return an array')
|
||||
}
|
||||
|
||||
return programs
|
||||
.filter(i => i)
|
||||
.map(program => {
|
||||
program.channel = channel.xmltv_id
|
||||
|
||||
return program
|
||||
})
|
||||
}
|
||||
|
||||
function toBaseObject(data) {
|
||||
return data
|
||||
}
|
509
src/utils.js
509
src/utils.js
|
@ -1,127 +1,34 @@
|
|||
const fs = require('fs')
|
||||
const { padStart } = require('lodash')
|
||||
const path = require('path')
|
||||
const axios = require('axios').default
|
||||
const axiosCookieJarSupport = require('axios-cookiejar-support').default
|
||||
const { setupCache } = require('axios-cache-interceptor')
|
||||
const tough = require('tough-cookie')
|
||||
const convert = require('xml-js')
|
||||
const { merge } = require('lodash')
|
||||
const dayjs = require('dayjs')
|
||||
const utc = require('dayjs/plugin/utc')
|
||||
const { CurlGenerator } = require('curl-generator')
|
||||
|
||||
dayjs.extend(utc)
|
||||
axiosCookieJarSupport(axios)
|
||||
|
||||
let timeout
|
||||
const utils = {}
|
||||
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'
|
||||
module.exports.sleep = sleep
|
||||
module.exports.getUTCDate = getUTCDate
|
||||
module.exports.isPromise = isPromise
|
||||
module.exports.isObject = isObject
|
||||
module.exports.escapeString = escapeString
|
||||
module.exports.parseInteger = parseInteger
|
||||
|
||||
utils.loadConfig = function (config) {
|
||||
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")
|
||||
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")
|
||||
if (config.logo && typeof config.logo !== 'function')
|
||||
throw new Error("The 'logo' property should return the function")
|
||||
|
||||
const defaultConfig = {
|
||||
days: 1,
|
||||
lang: 'en',
|
||||
delay: 3000,
|
||||
output: 'guide.xml',
|
||||
request: {
|
||||
method: 'GET',
|
||||
maxContentLength: 5 * 1024 * 1024,
|
||||
timeout: 5000,
|
||||
withCredentials: true,
|
||||
jar: new tough.CookieJar(),
|
||||
responseType: 'arraybuffer',
|
||||
cache: false
|
||||
}
|
||||
}
|
||||
|
||||
return merge(defaultConfig, config)
|
||||
}
|
||||
|
||||
utils.createClient = function (config) {
|
||||
const client = setupCache(axios.create())
|
||||
client.interceptors.request.use(
|
||||
function (request) {
|
||||
if (config.debug) {
|
||||
console.log('Request:', JSON.stringify(request, null, 2))
|
||||
}
|
||||
return request
|
||||
},
|
||||
function (error) {
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
client.interceptors.response.use(
|
||||
function (response) {
|
||||
if (config.debug) {
|
||||
const data =
|
||||
utils.isObject(response.data) || Array.isArray(response.data)
|
||||
? JSON.stringify(response.data)
|
||||
: response.data.toString()
|
||||
console.log(
|
||||
'Response:',
|
||||
JSON.stringify(
|
||||
{
|
||||
headers: response.headers,
|
||||
data,
|
||||
cached: response.cached
|
||||
},
|
||||
null,
|
||||
2
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
clearTimeout(timeout)
|
||||
return response
|
||||
},
|
||||
function (error) {
|
||||
clearTimeout(timeout)
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
utils.parseChannels = function (xml) {
|
||||
const result = convert.xml2js(xml)
|
||||
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 []
|
||||
|
||||
const channels = channelsTag.elements
|
||||
.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`)
|
||||
channel.name = el.elements.find(el => el.type === 'text').text
|
||||
channel.site = channel.site || site
|
||||
|
||||
return channel
|
||||
})
|
||||
|
||||
return { site, channels }
|
||||
}
|
||||
|
||||
utils.sleep = function (ms) {
|
||||
function sleep(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms))
|
||||
}
|
||||
|
||||
utils.escapeString = function (string, defaultValue = '') {
|
||||
function isObject(a) {
|
||||
return !!a && a.constructor === Object
|
||||
}
|
||||
|
||||
function isPromise(promise) {
|
||||
return !!promise && typeof promise.then === 'function'
|
||||
}
|
||||
|
||||
function getUTCDate(d = null) {
|
||||
if (typeof d === 'string') return dayjs.utc(d).startOf('d')
|
||||
|
||||
return dayjs.utc().startOf('d')
|
||||
}
|
||||
|
||||
function escapeString(string, defaultValue = '') {
|
||||
if (!string) return defaultValue
|
||||
|
||||
const regex = new RegExp(
|
||||
|
@ -149,372 +56,6 @@ utils.escapeString = function (string, defaultValue = '') {
|
|||
.trim()
|
||||
}
|
||||
|
||||
utils.convertToXMLTV = function ({ channels, programs, date = dayjs.utc() }) {
|
||||
let output = `<?xml version="1.0" encoding="UTF-8" ?><tv date="${dayjs(date).format(
|
||||
'YYYYMMDD'
|
||||
)}">\r\n`
|
||||
for (let channel of channels) {
|
||||
const id = utils.escapeString(channel['xmltv_id'])
|
||||
const displayName = utils.escapeString(channel.name)
|
||||
output += `<channel id="${id}"><display-name>${displayName}</display-name>`
|
||||
if (channel.logo) {
|
||||
const logo = utils.escapeString(channel.logo)
|
||||
output += `<icon src="${logo}"/>`
|
||||
}
|
||||
if (channel.site) {
|
||||
const url = channel.site ? 'https://' + channel.site : null
|
||||
output += `<url>${url}</url>`
|
||||
}
|
||||
output += `</channel>\r\n`
|
||||
}
|
||||
|
||||
for (let program of programs) {
|
||||
if (!program) continue
|
||||
|
||||
const channel = utils.escapeString(program.channel)
|
||||
const title = utils.escapeString(program.title)
|
||||
const description = utils.escapeString(program.description)
|
||||
const categories = Array.isArray(program.category) ? program.category : [program.category]
|
||||
const start = program.start ? dayjs.unix(program.start).utc().format('YYYYMMDDHHmmss ZZ') : ''
|
||||
const stop = program.stop ? dayjs.unix(program.stop).utc().format('YYYYMMDDHHmmss ZZ') : ''
|
||||
const lang = program.lang || 'en'
|
||||
const xmltv_ns = createXMLTVNS(program.season, program.episode)
|
||||
const onscreen = createOnScreen(program.season, program.episode)
|
||||
const date = program.date || ''
|
||||
const credits = createCredits({
|
||||
director: program.director,
|
||||
actor: program.actor,
|
||||
writer: program.writer,
|
||||
adapter: program.adapter,
|
||||
producer: program.producer,
|
||||
composer: program.composer,
|
||||
editor: program.editor,
|
||||
presenter: program.presenter,
|
||||
commentator: program.commentator,
|
||||
guest: program.guest
|
||||
})
|
||||
const icon = utils.escapeString(program.icon)
|
||||
const sub_title = utils.escapeString(program.sub_title)
|
||||
const url = program.url ? createURL(program.url, channel) : ''
|
||||
|
||||
if (start && stop && title) {
|
||||
output += `<programme start="${start}" stop="${stop}" channel="${channel}"><title lang="${lang}">${title}</title>`
|
||||
|
||||
if (sub_title) {
|
||||
output += `<sub-title>${sub_title}</sub-title>`
|
||||
}
|
||||
|
||||
if (description) {
|
||||
output += `<desc lang="${lang}">${description}</desc>`
|
||||
}
|
||||
|
||||
if (categories.length) {
|
||||
categories.forEach(category => {
|
||||
if (category) {
|
||||
output += `<category lang="${lang}">${utils.escapeString(category)}</category>`
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (url) {
|
||||
output += url
|
||||
}
|
||||
|
||||
if (xmltv_ns) {
|
||||
output += `<episode-num system="xmltv_ns">${xmltv_ns}</episode-num>`
|
||||
}
|
||||
|
||||
if (onscreen) {
|
||||
output += `<episode-num system="onscreen">${onscreen}</episode-num>`
|
||||
}
|
||||
if (date) {
|
||||
output += `<date>${date}</date>`
|
||||
}
|
||||
|
||||
if (icon) {
|
||||
output += `<icon src="${icon}"/>`
|
||||
}
|
||||
|
||||
if (credits) {
|
||||
output += `<credits>${credits}</credits>`
|
||||
}
|
||||
|
||||
output += '</programme>\r\n'
|
||||
}
|
||||
}
|
||||
|
||||
output += '</tv>'
|
||||
|
||||
function createXMLTVNS(s, e) {
|
||||
if (!e) return null
|
||||
s = s || 1
|
||||
|
||||
return `${s - 1}.${e - 1}.0/1`
|
||||
}
|
||||
|
||||
function createOnScreen(s, e) {
|
||||
if (!e) return null
|
||||
s = s || 1
|
||||
|
||||
s = padStart(s, 2, '0')
|
||||
e = padStart(e, 2, '0')
|
||||
|
||||
return `S${s}E${e}`
|
||||
}
|
||||
|
||||
function createURL(urlObj, channel = '') {
|
||||
const urls = Array.isArray(urlObj) ? urlObj : [urlObj]
|
||||
let output = ''
|
||||
for (let url of urls) {
|
||||
if (typeof url === 'string' || url instanceof String) {
|
||||
url = { value: url }
|
||||
}
|
||||
|
||||
let attr = url.system ? ` system="${url.system}"` : ''
|
||||
if (url.value.includes('http')) {
|
||||
output += `<url${attr}>${url.value}</url>`
|
||||
} else if (channel) {
|
||||
let chan = channels.find(c => c.xmltv_id.localeCompare(channel) === 0)
|
||||
if (chan && chan.site) {
|
||||
output += `<url${attr}>https://${chan.site}${url.value}</url>`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
function createImage(imgObj, channel = '') {
|
||||
const imgs = Array.isArray(imgObj) ? imgObj : [imgObj]
|
||||
let output = ''
|
||||
for (let img of imgs) {
|
||||
if (typeof img === 'string' || img instanceof String) {
|
||||
img = { value: img }
|
||||
}
|
||||
|
||||
const imageTypes = ['poster', 'backdrop', 'still', 'person', 'character']
|
||||
const imageSizes = ['1', '2', '3']
|
||||
const imageOrients = ['P', 'L']
|
||||
|
||||
let attr = ''
|
||||
|
||||
if (img.type && imageTypes.some(el => img.type.includes(el))) {
|
||||
attr += ` type="${img.type}"`
|
||||
}
|
||||
|
||||
if (img.size && imageSizes.some(el => img.size.includes(el))) {
|
||||
attr += ` size="${img.size}"`
|
||||
}
|
||||
|
||||
if (img.orient && imageOrients.some(el => img.orient.includes(el))) {
|
||||
attr += ` orient="${img.orient}"`
|
||||
}
|
||||
|
||||
if (img.system) {
|
||||
attr += ` system="${img.system}"`
|
||||
}
|
||||
|
||||
if (img.value.includes('http')) {
|
||||
output += `<image${attr}>${img.value}</image>`
|
||||
} else if (channel) {
|
||||
let chan = channels.find(c => c.xmltv_id.localeCompare(channel) === 0)
|
||||
if (chan && chan.site) {
|
||||
output += `<image${attr}>https://${chan.site}${img.value}</image>`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
function createCredits(obj) {
|
||||
let cast = Object.entries(obj)
|
||||
.filter(x => x[1])
|
||||
.map(([name, value]) => ({ name, value }))
|
||||
|
||||
let output = ''
|
||||
for (let type of cast) {
|
||||
const r = Array.isArray(type.value) ? type.value : [type.value]
|
||||
for (let person of r) {
|
||||
if (typeof person === 'string' || person instanceof String) {
|
||||
person = { value: person }
|
||||
}
|
||||
|
||||
let attr = ''
|
||||
if (type.name.localeCompare('actor') === 0 && type.value.role) {
|
||||
attr += ` role="${type.value.role}"`
|
||||
}
|
||||
if (type.name.localeCompare('actor') === 0 && type.value.guest) {
|
||||
attr += ` guest="${type.value.guest}"`
|
||||
}
|
||||
output += `<${type.name}${attr}>${person.value}`
|
||||
|
||||
if (person.url) {
|
||||
output += createURL(person.url)
|
||||
}
|
||||
if (person.image) {
|
||||
output += createImage(person.image)
|
||||
}
|
||||
|
||||
output += `</${type.name}>`
|
||||
}
|
||||
}
|
||||
return output
|
||||
}
|
||||
return output
|
||||
function parseInteger(val) {
|
||||
return val ? parseInt(val) : null
|
||||
}
|
||||
|
||||
utils.writeToFile = function (filename, data) {
|
||||
const dir = path.resolve(path.dirname(filename))
|
||||
if (!fs.existsSync(dir)) {
|
||||
fs.mkdirSync(dir, { recursive: true })
|
||||
}
|
||||
|
||||
fs.writeFileSync(path.resolve(filename), data)
|
||||
}
|
||||
|
||||
utils.buildRequest = async function (item, config) {
|
||||
const CancelToken = axios.CancelToken
|
||||
const source = CancelToken.source()
|
||||
const request = { ...config.request }
|
||||
timeout = setTimeout(() => {
|
||||
source.cancel('Connection timeout')
|
||||
}, request.timeout)
|
||||
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)
|
||||
request.cancelToken = source.token
|
||||
|
||||
if (config.curl) {
|
||||
const curl = CurlGenerator({
|
||||
url: request.url,
|
||||
method: request.method,
|
||||
headers: request.headers,
|
||||
body: request.data
|
||||
})
|
||||
console.log(curl)
|
||||
}
|
||||
|
||||
return request
|
||||
}
|
||||
|
||||
utils.fetchData = function (client, request) {
|
||||
return client(request)
|
||||
}
|
||||
|
||||
utils.getRequestHeaders = async function (item, config) {
|
||||
if (typeof config.request.headers === 'function') {
|
||||
const headers = config.request.headers(item)
|
||||
if (this.isPromise(headers)) {
|
||||
return await headers
|
||||
}
|
||||
return headers
|
||||
}
|
||||
return config.request.headers || null
|
||||
}
|
||||
|
||||
utils.getRequestData = async function (item, config) {
|
||||
if (typeof config.request.data === 'function') {
|
||||
const data = config.request.data(item)
|
||||
if (this.isPromise(data)) {
|
||||
return await data
|
||||
}
|
||||
return data
|
||||
}
|
||||
return config.request.data || null
|
||||
}
|
||||
|
||||
utils.getRequestUrl = async function (item, config) {
|
||||
if (typeof config.url === 'function') {
|
||||
const url = config.url(item)
|
||||
if (this.isPromise(url)) {
|
||||
return await url
|
||||
}
|
||||
return url
|
||||
}
|
||||
return config.url
|
||||
}
|
||||
|
||||
utils.getUTCDate = function (d = null) {
|
||||
if (typeof d === 'string') return dayjs.utc(d).startOf('d')
|
||||
|
||||
return dayjs.utc().startOf('d')
|
||||
}
|
||||
|
||||
utils.parseResponse = async (item, response, config) => {
|
||||
const data = merge(item, config, {
|
||||
content: response.data.toString(),
|
||||
buffer: response.data,
|
||||
headers: response.headers,
|
||||
request: response.request,
|
||||
cached: response.cached
|
||||
})
|
||||
|
||||
if (!item.channel.logo && config.logo) {
|
||||
data.channel.logo = await utils.loadLogo(data, config)
|
||||
}
|
||||
|
||||
return await utils.parsePrograms(data, config)
|
||||
}
|
||||
|
||||
utils.parsePrograms = async function (data, config) {
|
||||
let programs = config.parser(data)
|
||||
|
||||
if (this.isPromise(programs)) {
|
||||
programs = await programs
|
||||
}
|
||||
|
||||
if (!Array.isArray(programs)) {
|
||||
throw new Error('Parser should return an array')
|
||||
}
|
||||
|
||||
const channel = data.channel
|
||||
return programs
|
||||
.filter(i => i)
|
||||
.map(program => {
|
||||
return {
|
||||
title: program.title,
|
||||
description: program.description || null,
|
||||
category: program.category || null,
|
||||
season: program.season || null,
|
||||
episode: program.episode || null,
|
||||
sub_title: program.sub_title || null,
|
||||
url: program.url || null,
|
||||
icon: program.icon || null,
|
||||
channel: channel.xmltv_id,
|
||||
lang: program.lang || channel.lang || config.lang || 'en',
|
||||
start: program.start ? dayjs(program.start).unix() : null,
|
||||
stop: program.stop ? dayjs(program.stop).unix() : null,
|
||||
date: program.date || null,
|
||||
director: program.director || null,
|
||||
actor: program.actor || null,
|
||||
writer: program.writer || null,
|
||||
adapter: program.adapter || null,
|
||||
producer: program.producer || null,
|
||||
composer: program.composer || null,
|
||||
editor: program.editor || null,
|
||||
presenter: program.presenter || null,
|
||||
commentator: program.commentator || null,
|
||||
guest: program.guest || null
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
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'
|
||||
}
|
||||
|
||||
utils.isObject = function (a) {
|
||||
return !!a && a.constructor === Object
|
||||
}
|
||||
|
||||
module.exports = utils
|
||||
|
|
|
@ -0,0 +1,224 @@
|
|||
const { padStart } = require('lodash')
|
||||
const { escapeString, getUTCDate } = require('./utils')
|
||||
const dayjs = require('dayjs')
|
||||
const utc = require('dayjs/plugin/utc')
|
||||
|
||||
dayjs.extend(utc)
|
||||
|
||||
module.exports.generate = generate
|
||||
|
||||
function generate({ channels, programs, date = getUTCDate() }) {
|
||||
let output = `<?xml version="1.0" encoding="UTF-8" ?><tv date="${dayjs(date).format(
|
||||
'YYYYMMDD'
|
||||
)}">\r\n`
|
||||
for (let channel of channels) {
|
||||
const id = escapeString(channel['xmltv_id'])
|
||||
const displayName = escapeString(channel.name)
|
||||
output += `<channel id="${id}"><display-name>${displayName}</display-name>`
|
||||
if (channel.logo) {
|
||||
const logo = escapeString(channel.logo)
|
||||
output += `<icon src="${logo}"/>`
|
||||
}
|
||||
if (channel.site) {
|
||||
const url = channel.site ? 'https://' + channel.site : null
|
||||
output += `<url>${url}</url>`
|
||||
}
|
||||
output += `</channel>\r\n`
|
||||
}
|
||||
|
||||
for (let program of programs) {
|
||||
if (!program) continue
|
||||
|
||||
const channel = escapeString(program.channel)
|
||||
const title = escapeString(program.title)
|
||||
const description = escapeString(program.description)
|
||||
const categories = Array.isArray(program.category) ? program.category : [program.category]
|
||||
const start = program.start ? dayjs.unix(program.start).utc().format('YYYYMMDDHHmmss ZZ') : ''
|
||||
const stop = program.stop ? dayjs.unix(program.stop).utc().format('YYYYMMDDHHmmss ZZ') : ''
|
||||
const lang = program.lang || 'en'
|
||||
const xmltv_ns = createXMLTVNS(program.season, program.episode)
|
||||
const onscreen = createOnScreen(program.season, program.episode)
|
||||
const date = program.date || ''
|
||||
const credits = createCredits({
|
||||
director: program.director,
|
||||
actor: program.actor,
|
||||
writer: program.writer,
|
||||
adapter: program.adapter,
|
||||
producer: program.producer,
|
||||
composer: program.composer,
|
||||
editor: program.editor,
|
||||
presenter: program.presenter,
|
||||
commentator: program.commentator,
|
||||
guest: program.guest
|
||||
})
|
||||
const icon = escapeString(program.icon)
|
||||
const sub_title = escapeString(program.sub_title)
|
||||
const url = program.url ? createURL(program.url, channel) : ''
|
||||
|
||||
if (start && stop && title) {
|
||||
output += `<programme start="${start}" stop="${stop}" channel="${channel}"><title lang="${lang}">${title}</title>`
|
||||
|
||||
if (sub_title) {
|
||||
output += `<sub-title>${sub_title}</sub-title>`
|
||||
}
|
||||
|
||||
if (description) {
|
||||
output += `<desc lang="${lang}">${description}</desc>`
|
||||
}
|
||||
|
||||
if (categories.length) {
|
||||
categories.forEach(category => {
|
||||
if (category) {
|
||||
output += `<category lang="${lang}">${escapeString(category)}</category>`
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (url) {
|
||||
output += url
|
||||
}
|
||||
|
||||
if (xmltv_ns) {
|
||||
output += `<episode-num system="xmltv_ns">${xmltv_ns}</episode-num>`
|
||||
}
|
||||
|
||||
if (onscreen) {
|
||||
output += `<episode-num system="onscreen">${onscreen}</episode-num>`
|
||||
}
|
||||
if (date) {
|
||||
output += `<date>${date}</date>`
|
||||
}
|
||||
|
||||
if (icon) {
|
||||
output += `<icon src="${icon}"/>`
|
||||
}
|
||||
|
||||
if (credits) {
|
||||
output += `<credits>${credits}</credits>`
|
||||
}
|
||||
|
||||
output += '</programme>\r\n'
|
||||
}
|
||||
}
|
||||
|
||||
output += '</tv>'
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
function createXMLTVNS(s, e) {
|
||||
if (!e) return null
|
||||
s = s || 1
|
||||
|
||||
return `${s - 1}.${e - 1}.0/1`
|
||||
}
|
||||
|
||||
function createOnScreen(s, e) {
|
||||
if (!e) return null
|
||||
s = s || 1
|
||||
|
||||
s = padStart(s, 2, '0')
|
||||
e = padStart(e, 2, '0')
|
||||
|
||||
return `S${s}E${e}`
|
||||
}
|
||||
|
||||
function createURL(urlObj, channel = '') {
|
||||
const urls = Array.isArray(urlObj) ? urlObj : [urlObj]
|
||||
let output = ''
|
||||
for (let url of urls) {
|
||||
if (typeof url === 'string' || url instanceof String) {
|
||||
url = { value: url }
|
||||
}
|
||||
|
||||
let attr = url.system ? ` system="${url.system}"` : ''
|
||||
if (url.value.includes('http')) {
|
||||
output += `<url${attr}>${url.value}</url>`
|
||||
} else if (channel) {
|
||||
let chan = channels.find(c => c.xmltv_id.localeCompare(channel) === 0)
|
||||
if (chan && chan.site) {
|
||||
output += `<url${attr}>https://${chan.site}${url.value}</url>`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
function createImage(imgObj, channel = '') {
|
||||
const imgs = Array.isArray(imgObj) ? imgObj : [imgObj]
|
||||
let output = ''
|
||||
for (let img of imgs) {
|
||||
if (typeof img === 'string' || img instanceof String) {
|
||||
img = { value: img }
|
||||
}
|
||||
|
||||
const imageTypes = ['poster', 'backdrop', 'still', 'person', 'character']
|
||||
const imageSizes = ['1', '2', '3']
|
||||
const imageOrients = ['P', 'L']
|
||||
|
||||
let attr = ''
|
||||
|
||||
if (img.type && imageTypes.some(el => img.type.includes(el))) {
|
||||
attr += ` type="${img.type}"`
|
||||
}
|
||||
|
||||
if (img.size && imageSizes.some(el => img.size.includes(el))) {
|
||||
attr += ` size="${img.size}"`
|
||||
}
|
||||
|
||||
if (img.orient && imageOrients.some(el => img.orient.includes(el))) {
|
||||
attr += ` orient="${img.orient}"`
|
||||
}
|
||||
|
||||
if (img.system) {
|
||||
attr += ` system="${img.system}"`
|
||||
}
|
||||
|
||||
if (img.value.includes('http')) {
|
||||
output += `<image${attr}>${img.value}</image>`
|
||||
} else if (channel) {
|
||||
let chan = channels.find(c => c.xmltv_id.localeCompare(channel) === 0)
|
||||
if (chan && chan.site) {
|
||||
output += `<image${attr}>https://${chan.site}${img.value}</image>`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
function createCredits(obj) {
|
||||
let cast = Object.entries(obj)
|
||||
.filter(x => x[1])
|
||||
.map(([name, value]) => ({ name, value }))
|
||||
|
||||
let output = ''
|
||||
for (let type of cast) {
|
||||
const r = Array.isArray(type.value) ? type.value : [type.value]
|
||||
for (let person of r) {
|
||||
if (typeof person === 'string' || person instanceof String) {
|
||||
person = { value: person }
|
||||
}
|
||||
|
||||
let attr = ''
|
||||
if (type.name.localeCompare('actor') === 0 && type.value.role) {
|
||||
attr += ` role="${type.value.role}"`
|
||||
}
|
||||
if (type.name.localeCompare('actor') === 0 && type.value.guest) {
|
||||
attr += ` guest="${type.value.guest}"`
|
||||
}
|
||||
output += `<${type.name}${attr}>${person.value}`
|
||||
|
||||
if (person.url) {
|
||||
output += createURL(person.url)
|
||||
}
|
||||
if (person.image) {
|
||||
output += createImage(person.image)
|
||||
}
|
||||
|
||||
output += `</${type.name}>`
|
||||
}
|
||||
}
|
||||
return output
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
import { parse as parseChannels } from '../src/channels'
|
||||
import path from 'path'
|
||||
import fs from 'fs'
|
||||
|
||||
it('can parse valid channels.xml', () => {
|
||||
const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' })
|
||||
const { channels } = parseChannels(file)
|
||||
expect(channels).toEqual([
|
||||
{
|
||||
name: '1 TV',
|
||||
xmltv_id: '1TV.com',
|
||||
site_id: '1',
|
||||
site: 'example.com',
|
||||
lang: 'fr',
|
||||
logo: 'https://example.com/logos/1TV.png'
|
||||
},
|
||||
{
|
||||
name: '2 TV',
|
||||
xmltv_id: '2TV.com',
|
||||
site_id: '2',
|
||||
site: 'example.com',
|
||||
lang: undefined,
|
||||
logo: undefined
|
||||
}
|
||||
])
|
||||
})
|
|
@ -0,0 +1,47 @@
|
|||
import { buildRequest, create as createClient } from '../src/client'
|
||||
|
||||
const config = {
|
||||
days: 1,
|
||||
lang: 'en',
|
||||
delay: 3000,
|
||||
output: 'guide.xml',
|
||||
request: {
|
||||
method: 'POST',
|
||||
maxContentLength: 5 * 1024 * 1024,
|
||||
timeout: 5000,
|
||||
withCredentials: true,
|
||||
responseType: 'arraybuffer',
|
||||
cache: false,
|
||||
data: { accountID: '123' },
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Cookie: 'abc=123',
|
||||
'User-Agent':
|
||||
'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'
|
||||
}
|
||||
},
|
||||
url: 'http://example.com/20210319/1tv.json'
|
||||
}
|
||||
|
||||
it('can build request', done => {
|
||||
buildRequest({ config })
|
||||
.then(request => {
|
||||
expect(request).toMatchObject({
|
||||
data: { accountID: '123' },
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Cookie: 'abc=123',
|
||||
'User-Agent':
|
||||
'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'
|
||||
},
|
||||
maxContentLength: 5242880,
|
||||
method: 'POST',
|
||||
responseType: 'arraybuffer',
|
||||
timeout: 5000,
|
||||
url: 'http://example.com/20210319/1tv.json',
|
||||
withCredentials: true
|
||||
})
|
||||
done()
|
||||
})
|
||||
.catch(done)
|
||||
})
|
|
@ -0,0 +1,26 @@
|
|||
import { load as loadConfig } from '../src/config'
|
||||
import path from 'path'
|
||||
import fs from 'fs'
|
||||
|
||||
it('can load config', () => {
|
||||
const config = loadConfig(require(path.resolve('./tests/input/example.com.config.js')))
|
||||
expect(config).toMatchObject({
|
||||
days: 1,
|
||||
delay: 3000,
|
||||
lang: 'en',
|
||||
site: 'example.com'
|
||||
})
|
||||
expect(config.request).toMatchObject({
|
||||
timeout: 5000,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Cookie: 'abc=123'
|
||||
}
|
||||
})
|
||||
expect(typeof config.request.data).toEqual('function')
|
||||
expect(typeof config.url).toEqual('function')
|
||||
expect(typeof config.logo).toEqual('function')
|
||||
expect(config.request.data()).toEqual({ accountID: '123' })
|
||||
expect(config.url()).toEqual('http://example.com/20210319/1tv.json')
|
||||
expect(config.logo()).toEqual('http://example.com/logos/1TV.png?x=шеллы&sid=777')
|
||||
})
|
|
@ -0,0 +1,66 @@
|
|||
import { parse as parsePrograms } from '../src/programs'
|
||||
|
||||
const channel = { xmltv_id: '1tv', lang: 'en' }
|
||||
|
||||
it('can parse programs', done => {
|
||||
const config = {
|
||||
parser: () => [
|
||||
{
|
||||
title: 'Title',
|
||||
description: 'Description',
|
||||
lang: 'en',
|
||||
category: ['Category1', 'Category2'],
|
||||
icon: 'https://example.com/image.jpg',
|
||||
season: 9,
|
||||
episode: 238,
|
||||
start: 1640995200,
|
||||
stop: 1640998800
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
parsePrograms({ channel, config })
|
||||
.then(programs => {
|
||||
expect(programs).toMatchObject([
|
||||
{
|
||||
title: 'Title',
|
||||
description: 'Description',
|
||||
lang: 'en',
|
||||
category: ['Category1', 'Category2'],
|
||||
icon: 'https://example.com/image.jpg',
|
||||
season: 9,
|
||||
episode: 238,
|
||||
start: 1640995200,
|
||||
stop: 1640998800,
|
||||
channel: '1tv'
|
||||
}
|
||||
])
|
||||
done()
|
||||
})
|
||||
.catch(done)
|
||||
})
|
||||
|
||||
it('can parse programs async', done => {
|
||||
const config = {
|
||||
parser: async () => [
|
||||
{
|
||||
title: 'Title',
|
||||
description: 'Description',
|
||||
lang: 'en',
|
||||
category: ['Category1', 'Category2'],
|
||||
icon: 'https://example.com/image.jpg',
|
||||
season: 9,
|
||||
episode: 238,
|
||||
start: 1640995200,
|
||||
stop: 1640998800
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
parsePrograms({ channel, config })
|
||||
.then(programs => {
|
||||
expect(programs.length).toBe(1)
|
||||
done()
|
||||
})
|
||||
.catch(done)
|
||||
})
|
|
@ -1,401 +1,11 @@
|
|||
import mockAxios from 'jest-mock-axios'
|
||||
import utils from '../src/utils'
|
||||
import axios from 'axios'
|
||||
import path from 'path'
|
||||
import fs from 'fs'
|
||||
|
||||
jest.useFakeTimers('modern').setSystemTime(new Date('2022-05-05'))
|
||||
|
||||
it('can load valid config.js', () => {
|
||||
const config = utils.loadConfig(require(path.resolve('./tests/input/example.com.config.js')))
|
||||
expect(config).toMatchObject({
|
||||
days: 1,
|
||||
delay: 3000,
|
||||
lang: 'en',
|
||||
site: 'example.com'
|
||||
})
|
||||
expect(config.request).toMatchObject({
|
||||
timeout: 5000,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Cookie: 'abc=123'
|
||||
}
|
||||
})
|
||||
expect(typeof config.request.data).toEqual('function')
|
||||
expect(typeof config.url).toEqual('function')
|
||||
expect(typeof config.logo).toEqual('function')
|
||||
expect(config.request.data()).toEqual({ accountID: '123' })
|
||||
expect(config.url()).toEqual('http://example.com/20210319/1tv.json')
|
||||
expect(config.logo()).toEqual('http://example.com/logos/1TV.png?x=шеллы&sid=777')
|
||||
})
|
||||
|
||||
it('can parse valid channels.xml', () => {
|
||||
const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' })
|
||||
const { channels } = utils.parseChannels(file)
|
||||
expect(channels).toEqual([
|
||||
{
|
||||
name: '1 TV',
|
||||
xmltv_id: '1TV.com',
|
||||
site_id: '1',
|
||||
site: 'example.com',
|
||||
lang: 'fr',
|
||||
logo: 'https://example.com/logos/1TV.png'
|
||||
},
|
||||
{
|
||||
name: '2 TV',
|
||||
xmltv_id: '2TV.com',
|
||||
site_id: '2',
|
||||
site: 'example.com',
|
||||
lang: undefined,
|
||||
logo: undefined
|
||||
}
|
||||
])
|
||||
})
|
||||
|
||||
it('can convert object to xmltv string', () => {
|
||||
const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' })
|
||||
const { channels } = utils.parseChannels(file)
|
||||
const programs = [
|
||||
{
|
||||
title: 'Program 1',
|
||||
sub_title: 'Sub-title & 1',
|
||||
description: 'Description for Program 1',
|
||||
url: 'http://example.com/title.html',
|
||||
start: 1616133600,
|
||||
stop: 1616135400,
|
||||
category: 'Test',
|
||||
season: 9,
|
||||
episode: 239,
|
||||
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
|
||||
channel: '1TV.com',
|
||||
lang: 'it',
|
||||
date: '20220505',
|
||||
director: {
|
||||
value: 'Director 1',
|
||||
url: { value: 'http://example.com/director1.html', system: 'TestSystem' }
|
||||
},
|
||||
actor: ['Actor 1', 'Actor 2'],
|
||||
writer: 'Writer 1'
|
||||
}
|
||||
]
|
||||
const output = utils.convertToXMLTV({ channels, programs })
|
||||
expect(output).toBe(
|
||||
'<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">\r\n<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/><url>https://example.com</url></channel>\r\n<channel id="2TV.com"><display-name>2 TV</display-name><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title><sub-title>Sub-title & 1</sub-title><desc lang="it">Description for Program 1</desc><category lang="it">Test</category><url>http://example.com/title.html</url><episode-num system="xmltv_ns">8.238.0/1</episode-num><episode-num system="onscreen">S09E239</episode-num><date>20220505</date><icon src="https://example.com/images/Program1.png?x=шеллы&sid=777"/><credits><director>Director 1<url system="TestSystem">http://example.com/director1.html</url></director><actor>Actor 1</actor><actor>Actor 2</actor><writer>Writer 1</writer></credits></programme>\r\n</tv>'
|
||||
)
|
||||
})
|
||||
|
||||
it('can convert object to xmltv string without season number', () => {
|
||||
const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' })
|
||||
const { channels } = utils.parseChannels(file)
|
||||
const programs = [
|
||||
{
|
||||
title: 'Program 1',
|
||||
description: 'Description for Program 1',
|
||||
start: 1616133600,
|
||||
stop: 1616135400,
|
||||
category: 'Test',
|
||||
episode: 239,
|
||||
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
|
||||
channel: '1TV.com',
|
||||
lang: 'it'
|
||||
}
|
||||
]
|
||||
const output = utils.convertToXMLTV({ channels, programs })
|
||||
expect(output).toBe(
|
||||
'<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">\r\n<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/><url>https://example.com</url></channel>\r\n<channel id="2TV.com"><display-name>2 TV</display-name><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title><desc lang="it">Description for Program 1</desc><category lang="it">Test</category><episode-num system="xmltv_ns">0.238.0/1</episode-num><episode-num system="onscreen">S01E239</episode-num><icon src="https://example.com/images/Program1.png?x=шеллы&sid=777"/></programme>\r\n</tv>'
|
||||
)
|
||||
})
|
||||
|
||||
it('can convert object to xmltv string without episode number', () => {
|
||||
const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' })
|
||||
const { channels } = utils.parseChannels(file)
|
||||
const programs = [
|
||||
{
|
||||
title: 'Program 1',
|
||||
description: 'Description for Program 1',
|
||||
start: 1616133600,
|
||||
stop: 1616135400,
|
||||
category: 'Test',
|
||||
season: 1,
|
||||
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
|
||||
channel: '1TV.com',
|
||||
lang: 'it'
|
||||
}
|
||||
]
|
||||
const output = utils.convertToXMLTV({ channels, programs })
|
||||
expect(output).toBe(
|
||||
'<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">\r\n<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/><url>https://example.com</url></channel>\r\n<channel id="2TV.com"><display-name>2 TV</display-name><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title><desc lang="it">Description for Program 1</desc><category lang="it">Test</category><icon src="https://example.com/images/Program1.png?x=шеллы&sid=777"/></programme>\r\n</tv>'
|
||||
)
|
||||
})
|
||||
|
||||
it('can convert object to xmltv string without categories', () => {
|
||||
const channels = [
|
||||
{
|
||||
name: '1 TV',
|
||||
xmltv_id: '1TV.com',
|
||||
site_id: '1',
|
||||
site: 'example.com',
|
||||
lang: 'fr',
|
||||
logo: 'https://example.com/logos/1TV.png'
|
||||
}
|
||||
]
|
||||
const programs = [
|
||||
{
|
||||
title: 'Program 1',
|
||||
start: 1616133600,
|
||||
stop: 1616135400,
|
||||
channel: '1TV.com',
|
||||
lang: 'it'
|
||||
}
|
||||
]
|
||||
const config = { site: 'example.com' }
|
||||
const output = utils.convertToXMLTV({ config, channels, programs })
|
||||
expect(output).toBe(
|
||||
'<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">\r\n<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title></programme>\r\n</tv>'
|
||||
)
|
||||
})
|
||||
|
||||
it('can convert object to xmltv string with multiple categories', () => {
|
||||
const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' })
|
||||
const { channels } = utils.parseChannels(file)
|
||||
const programs = [
|
||||
{
|
||||
title: 'Program 1',
|
||||
description: 'Description for Program 1',
|
||||
start: 1616133600,
|
||||
stop: 1616135400,
|
||||
category: ['Test1', 'Test2'],
|
||||
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
|
||||
channel: '1TV.com',
|
||||
lang: 'it'
|
||||
}
|
||||
]
|
||||
const output = utils.convertToXMLTV({ channels, programs })
|
||||
expect(output).toBe(
|
||||
'<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">\r\n<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/><url>https://example.com</url></channel>\r\n<channel id="2TV.com"><display-name>2 TV</display-name><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title><desc lang="it">Description for Program 1</desc><category lang="it">Test1</category><category lang="it">Test2</category><icon src="https://example.com/images/Program1.png?x=шеллы&sid=777"/></programme>\r\n</tv>'
|
||||
)
|
||||
})
|
||||
|
||||
it('can convert object to xmltv string with multiple urls', () => {
|
||||
const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' })
|
||||
const { channels } = utils.parseChannels(file)
|
||||
const programs = [
|
||||
{
|
||||
title: 'Program 1',
|
||||
description: 'Description for Program 1',
|
||||
start: 1616133600,
|
||||
stop: 1616135400,
|
||||
category: ['Test1', 'Test2'],
|
||||
url: [
|
||||
'https://example.com/noattr.html',
|
||||
{ value: 'https://example.com/attr.html', system: 'TestSystem' }
|
||||
],
|
||||
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
|
||||
channel: '1TV.com',
|
||||
lang: 'it'
|
||||
}
|
||||
]
|
||||
const output = utils.convertToXMLTV({ channels, programs })
|
||||
expect(output).toBe(
|
||||
'<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">\r\n<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/><url>https://example.com</url></channel>\r\n<channel id="2TV.com"><display-name>2 TV</display-name><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title><desc lang="it">Description for Program 1</desc><category lang="it">Test1</category><category lang="it">Test2</category><url>https://example.com/noattr.html</url><url system="TestSystem">https://example.com/attr.html</url><icon src="https://example.com/images/Program1.png?x=шеллы&sid=777"/></programme>\r\n</tv>'
|
||||
)
|
||||
})
|
||||
|
||||
it('can convert object to xmltv string with multiple images', () => {
|
||||
const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' })
|
||||
const { channels } = utils.parseChannels(file)
|
||||
const programs = [
|
||||
{
|
||||
title: 'Program 1',
|
||||
description: 'Description for Program 1',
|
||||
start: 1616133600,
|
||||
stop: 1616135400,
|
||||
category: ['Test1', 'Test2'],
|
||||
url: [
|
||||
'https://example.com/noattr.html',
|
||||
{ value: 'https://example.com/attr.html', system: 'TestSystem' }
|
||||
],
|
||||
actor: {
|
||||
value: 'Actor 1',
|
||||
image: [
|
||||
'https://example.com/image1.jpg',
|
||||
{
|
||||
value: 'https://example.com/image2.jpg',
|
||||
type: 'person',
|
||||
size: '2',
|
||||
system: 'TestSystem',
|
||||
orient: 'P'
|
||||
}
|
||||
]
|
||||
},
|
||||
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
|
||||
channel: '1TV.com',
|
||||
lang: 'it'
|
||||
}
|
||||
]
|
||||
const output = utils.convertToXMLTV({ channels, programs })
|
||||
expect(output).toBe(
|
||||
'<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">\r\n<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/><url>https://example.com</url></channel>\r\n<channel id="2TV.com"><display-name>2 TV</display-name><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title><desc lang="it">Description for Program 1</desc><category lang="it">Test1</category><category lang="it">Test2</category><url>https://example.com/noattr.html</url><url system="TestSystem">https://example.com/attr.html</url><icon src="https://example.com/images/Program1.png?x=шеллы&sid=777"/><credits><actor>Actor 1<image>https://example.com/image1.jpg</image><image type="person" size="2" orient="P" system="TestSystem">https://example.com/image2.jpg</image></actor></credits></programme>\r\n</tv>'
|
||||
)
|
||||
})
|
||||
|
||||
it('can convert object to xmltv string with multiple credits member', () => {
|
||||
const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' })
|
||||
const { channels } = utils.parseChannels(file)
|
||||
const programs = [
|
||||
{
|
||||
title: 'Program 1',
|
||||
sub_title: 'Sub-title 1',
|
||||
description: 'Description for Program 1',
|
||||
url: 'http://example.com/title.html',
|
||||
start: 1616133600,
|
||||
stop: 1616135400,
|
||||
category: 'Test',
|
||||
season: 9,
|
||||
episode: 239,
|
||||
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
|
||||
channel: '1TV.com',
|
||||
lang: 'it',
|
||||
date: '20220505',
|
||||
director: {
|
||||
value: 'Director 1',
|
||||
url: { value: 'http://example.com/director1.html', system: 'TestSystem' }
|
||||
},
|
||||
actor: {
|
||||
value: 'Actor 1',
|
||||
role: 'Manny',
|
||||
guest: 'yes',
|
||||
url: { value: 'http://example.com/actor1.html', system: 'TestSystem' }
|
||||
},
|
||||
writer: [
|
||||
{ value: 'Writer 1', url: { value: 'http://example.com/w1.html', system: 'TestSystem' } },
|
||||
{ value: 'Writer 2', url: { value: 'http://example.com/w2.html', system: 'TestSystem' } }
|
||||
]
|
||||
}
|
||||
]
|
||||
const output = utils.convertToXMLTV({ channels, programs })
|
||||
expect(output).toBe(
|
||||
'<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">\r\n<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/><url>https://example.com</url></channel>\r\n<channel id="2TV.com"><display-name>2 TV</display-name><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title><sub-title>Sub-title 1</sub-title><desc lang="it">Description for Program 1</desc><category lang="it">Test</category><url>http://example.com/title.html</url><episode-num system="xmltv_ns">8.238.0/1</episode-num><episode-num system="onscreen">S09E239</episode-num><date>20220505</date><icon src="https://example.com/images/Program1.png?x=шеллы&sid=777"/><credits><director>Director 1<url system="TestSystem">http://example.com/director1.html</url></director><actor role="Manny" guest="yes">Actor 1<url system="TestSystem">http://example.com/actor1.html</url></actor><writer>Writer 1<url system="TestSystem">http://example.com/w1.html</url></writer><writer>Writer 2<url system="TestSystem">http://example.com/w2.html</url></writer></credits></programme>\r\n</tv>'
|
||||
)
|
||||
})
|
||||
import { escapeString } from '../src/utils'
|
||||
|
||||
it('can escape string', () => {
|
||||
const string = 'Música тест dun. &<>"\'\r\n'
|
||||
expect(utils.escapeString(string)).toBe('Música тест dun. &<>"'')
|
||||
expect(escapeString(string)).toBe('Música тест dun. &<>"'')
|
||||
})
|
||||
|
||||
it('can escape url', () => {
|
||||
const string = 'http://example.com/logos/1TV.png?param1=val¶m2=val'
|
||||
expect(utils.escapeString(string)).toBe(
|
||||
'http://example.com/logos/1TV.png?param1=val&param2=val'
|
||||
)
|
||||
})
|
||||
|
||||
it('can fetch data', () => {
|
||||
const request = {
|
||||
data: { accountID: '123' },
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Cookie: 'abc=123',
|
||||
'User-Agent':
|
||||
'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'
|
||||
},
|
||||
maxContentLength: 5242880,
|
||||
method: 'POST',
|
||||
responseType: 'arraybuffer',
|
||||
timeout: 5000,
|
||||
url: 'http://example.com/20210319/1tv.json',
|
||||
withCredentials: true
|
||||
}
|
||||
utils.fetchData(mockAxios, request).then(jest.fn).catch(jest.fn)
|
||||
expect(mockAxios).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
data: { accountID: '123' },
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Cookie: 'abc=123',
|
||||
'User-Agent':
|
||||
'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'
|
||||
},
|
||||
method: 'POST',
|
||||
responseType: 'arraybuffer',
|
||||
timeout: 5000,
|
||||
url: 'http://example.com/20210319/1tv.json',
|
||||
withCredentials: true
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it('can build request async', done => {
|
||||
const config = utils.loadConfig(require(path.resolve('./tests/input/async.config.js')))
|
||||
|
||||
utils
|
||||
.buildRequest({}, config)
|
||||
.then(request => {
|
||||
expect(request).toMatchObject({
|
||||
data: { accountID: '123' },
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Cookie: 'abc=123',
|
||||
'User-Agent':
|
||||
'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'
|
||||
},
|
||||
maxContentLength: 5242880,
|
||||
method: 'POST',
|
||||
responseType: 'arraybuffer',
|
||||
timeout: 5000,
|
||||
url: 'http://example.com/20210319/1tv.json',
|
||||
withCredentials: true
|
||||
})
|
||||
done()
|
||||
})
|
||||
.catch(done)
|
||||
})
|
||||
|
||||
it('can load logo async', done => {
|
||||
const config = utils.loadConfig(require(path.resolve('./tests/input/async.config.js')))
|
||||
|
||||
utils
|
||||
.loadLogo({}, config)
|
||||
.then(logo => {
|
||||
expect(logo).toBe('http://example.com/logos/1TV.png?x=шеллы&sid=777')
|
||||
done()
|
||||
})
|
||||
.catch(done)
|
||||
})
|
||||
|
||||
it('can parse programs', done => {
|
||||
const config = utils.loadConfig(require(path.resolve('./tests/input/example.com.config.js')))
|
||||
|
||||
utils
|
||||
.parsePrograms({ channel: { xmltv_id: '1tv', lang: 'en' } }, config)
|
||||
.then(programs => {
|
||||
expect(programs).toMatchObject([
|
||||
{
|
||||
title: 'Title',
|
||||
description: 'Description',
|
||||
lang: 'en',
|
||||
category: ['Category1', 'Category2'],
|
||||
icon: 'https://example.com/image.jpg',
|
||||
season: 9,
|
||||
episode: 238,
|
||||
start: 1640995200,
|
||||
stop: 1640998800
|
||||
}
|
||||
])
|
||||
done()
|
||||
})
|
||||
.catch(done)
|
||||
})
|
||||
|
||||
it('can parse programs async', done => {
|
||||
const config = utils.loadConfig(require(path.resolve('./tests/input/async.config.js')))
|
||||
|
||||
utils
|
||||
.parsePrograms({ channel: { xmltv_id: '1tv', lang: 'en' } }, config)
|
||||
.then(programs => {
|
||||
expect(programs.length).toBe(0)
|
||||
done()
|
||||
})
|
||||
.catch(done)
|
||||
expect(escapeString(string)).toBe('http://example.com/logos/1TV.png?param1=val&param2=val')
|
||||
})
|
||||
|
|
|
@ -0,0 +1,220 @@
|
|||
import xmltv from '../src/xmltv'
|
||||
|
||||
jest.useFakeTimers('modern').setSystemTime(new Date('2022-05-05'))
|
||||
|
||||
const channels = [
|
||||
{
|
||||
xmltv_id: '1TV.com',
|
||||
name: '1 TV',
|
||||
logo: 'https://example.com/logos/1TV.png',
|
||||
site: 'example.com'
|
||||
},
|
||||
{
|
||||
xmltv_id: '2TV.com',
|
||||
name: '2 TV',
|
||||
site: 'example.com'
|
||||
}
|
||||
]
|
||||
|
||||
it('can generate xmltv', () => {
|
||||
const programs = [
|
||||
{
|
||||
title: 'Program 1',
|
||||
sub_title: 'Sub-title & 1',
|
||||
description: 'Description for Program 1',
|
||||
url: 'http://example.com/title.html',
|
||||
start: 1616133600,
|
||||
stop: 1616135400,
|
||||
category: 'Test',
|
||||
season: 9,
|
||||
episode: 239,
|
||||
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
|
||||
channel: '1TV.com',
|
||||
lang: 'it',
|
||||
date: '20220505',
|
||||
director: {
|
||||
value: 'Director 1',
|
||||
url: { value: 'http://example.com/director1.html', system: 'TestSystem' }
|
||||
},
|
||||
actor: ['Actor 1', 'Actor 2'],
|
||||
writer: 'Writer 1'
|
||||
}
|
||||
]
|
||||
const output = xmltv.generate({ channels, programs })
|
||||
expect(output).toBe(
|
||||
'<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">\r\n<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/><url>https://example.com</url></channel>\r\n<channel id="2TV.com"><display-name>2 TV</display-name><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title><sub-title>Sub-title & 1</sub-title><desc lang="it">Description for Program 1</desc><category lang="it">Test</category><url>http://example.com/title.html</url><episode-num system="xmltv_ns">8.238.0/1</episode-num><episode-num system="onscreen">S09E239</episode-num><date>20220505</date><icon src="https://example.com/images/Program1.png?x=шеллы&sid=777"/><credits><director>Director 1<url system="TestSystem">http://example.com/director1.html</url></director><actor>Actor 1</actor><actor>Actor 2</actor><writer>Writer 1</writer></credits></programme>\r\n</tv>'
|
||||
)
|
||||
})
|
||||
|
||||
it('can generate xmltv without season number', () => {
|
||||
const programs = [
|
||||
{
|
||||
title: 'Program 1',
|
||||
description: 'Description for Program 1',
|
||||
start: 1616133600,
|
||||
stop: 1616135400,
|
||||
category: 'Test',
|
||||
episode: 239,
|
||||
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
|
||||
channel: '1TV.com',
|
||||
lang: 'it'
|
||||
}
|
||||
]
|
||||
const output = xmltv.generate({ channels, programs })
|
||||
expect(output).toBe(
|
||||
'<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">\r\n<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/><url>https://example.com</url></channel>\r\n<channel id="2TV.com"><display-name>2 TV</display-name><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title><desc lang="it">Description for Program 1</desc><category lang="it">Test</category><episode-num system="xmltv_ns">0.238.0/1</episode-num><episode-num system="onscreen">S01E239</episode-num><icon src="https://example.com/images/Program1.png?x=шеллы&sid=777"/></programme>\r\n</tv>'
|
||||
)
|
||||
})
|
||||
|
||||
it('can generate xmltv without episode number', () => {
|
||||
const programs = [
|
||||
{
|
||||
title: 'Program 1',
|
||||
description: 'Description for Program 1',
|
||||
start: 1616133600,
|
||||
stop: 1616135400,
|
||||
category: 'Test',
|
||||
season: 1,
|
||||
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
|
||||
channel: '1TV.com',
|
||||
lang: 'it'
|
||||
}
|
||||
]
|
||||
const output = xmltv.generate({ channels, programs })
|
||||
expect(output).toBe(
|
||||
'<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">\r\n<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/><url>https://example.com</url></channel>\r\n<channel id="2TV.com"><display-name>2 TV</display-name><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title><desc lang="it">Description for Program 1</desc><category lang="it">Test</category><icon src="https://example.com/images/Program1.png?x=шеллы&sid=777"/></programme>\r\n</tv>'
|
||||
)
|
||||
})
|
||||
|
||||
it('can generate xmltv without categories', () => {
|
||||
const programs = [
|
||||
{
|
||||
title: 'Program 1',
|
||||
start: 1616133600,
|
||||
stop: 1616135400,
|
||||
channel: '1TV.com',
|
||||
lang: 'it'
|
||||
}
|
||||
]
|
||||
const config = { site: 'example.com' }
|
||||
const output = xmltv.generate({ config, channels, programs })
|
||||
expect(output).toBe(
|
||||
'<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">\r\n<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/><url>https://example.com</url></channel>\r\n<channel id="2TV.com"><display-name>2 TV</display-name><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title></programme>\r\n</tv>'
|
||||
)
|
||||
})
|
||||
|
||||
it('can generate xmltv with multiple categories', () => {
|
||||
const programs = [
|
||||
{
|
||||
title: 'Program 1',
|
||||
description: 'Description for Program 1',
|
||||
start: 1616133600,
|
||||
stop: 1616135400,
|
||||
category: ['Test1', 'Test2'],
|
||||
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
|
||||
channel: '1TV.com',
|
||||
lang: 'it'
|
||||
}
|
||||
]
|
||||
const output = xmltv.generate({ channels, programs })
|
||||
expect(output).toBe(
|
||||
'<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">\r\n<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/><url>https://example.com</url></channel>\r\n<channel id="2TV.com"><display-name>2 TV</display-name><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title><desc lang="it">Description for Program 1</desc><category lang="it">Test1</category><category lang="it">Test2</category><icon src="https://example.com/images/Program1.png?x=шеллы&sid=777"/></programme>\r\n</tv>'
|
||||
)
|
||||
})
|
||||
|
||||
it('can generate xmltv with multiple urls', () => {
|
||||
const programs = [
|
||||
{
|
||||
title: 'Program 1',
|
||||
description: 'Description for Program 1',
|
||||
start: 1616133600,
|
||||
stop: 1616135400,
|
||||
category: ['Test1', 'Test2'],
|
||||
url: [
|
||||
'https://example.com/noattr.html',
|
||||
{ value: 'https://example.com/attr.html', system: 'TestSystem' }
|
||||
],
|
||||
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
|
||||
channel: '1TV.com',
|
||||
lang: 'it'
|
||||
}
|
||||
]
|
||||
const output = xmltv.generate({ channels, programs })
|
||||
expect(output).toBe(
|
||||
'<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">\r\n<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/><url>https://example.com</url></channel>\r\n<channel id="2TV.com"><display-name>2 TV</display-name><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title><desc lang="it">Description for Program 1</desc><category lang="it">Test1</category><category lang="it">Test2</category><url>https://example.com/noattr.html</url><url system="TestSystem">https://example.com/attr.html</url><icon src="https://example.com/images/Program1.png?x=шеллы&sid=777"/></programme>\r\n</tv>'
|
||||
)
|
||||
})
|
||||
|
||||
it('can generate xmltv with multiple images', () => {
|
||||
const programs = [
|
||||
{
|
||||
title: 'Program 1',
|
||||
description: 'Description for Program 1',
|
||||
start: 1616133600,
|
||||
stop: 1616135400,
|
||||
category: ['Test1', 'Test2'],
|
||||
url: [
|
||||
'https://example.com/noattr.html',
|
||||
{ value: 'https://example.com/attr.html', system: 'TestSystem' }
|
||||
],
|
||||
actor: {
|
||||
value: 'Actor 1',
|
||||
image: [
|
||||
'https://example.com/image1.jpg',
|
||||
{
|
||||
value: 'https://example.com/image2.jpg',
|
||||
type: 'person',
|
||||
size: '2',
|
||||
system: 'TestSystem',
|
||||
orient: 'P'
|
||||
}
|
||||
]
|
||||
},
|
||||
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
|
||||
channel: '1TV.com',
|
||||
lang: 'it'
|
||||
}
|
||||
]
|
||||
const output = xmltv.generate({ channels, programs })
|
||||
expect(output).toBe(
|
||||
'<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">\r\n<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/><url>https://example.com</url></channel>\r\n<channel id="2TV.com"><display-name>2 TV</display-name><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title><desc lang="it">Description for Program 1</desc><category lang="it">Test1</category><category lang="it">Test2</category><url>https://example.com/noattr.html</url><url system="TestSystem">https://example.com/attr.html</url><icon src="https://example.com/images/Program1.png?x=шеллы&sid=777"/><credits><actor>Actor 1<image>https://example.com/image1.jpg</image><image type="person" size="2" orient="P" system="TestSystem">https://example.com/image2.jpg</image></actor></credits></programme>\r\n</tv>'
|
||||
)
|
||||
})
|
||||
|
||||
it('can generate xmltv with multiple credits member', () => {
|
||||
const programs = [
|
||||
{
|
||||
title: 'Program 1',
|
||||
sub_title: 'Sub-title 1',
|
||||
description: 'Description for Program 1',
|
||||
url: 'http://example.com/title.html',
|
||||
start: 1616133600,
|
||||
stop: 1616135400,
|
||||
category: 'Test',
|
||||
season: 9,
|
||||
episode: 239,
|
||||
icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
|
||||
channel: '1TV.com',
|
||||
lang: 'it',
|
||||
date: '20220505',
|
||||
director: {
|
||||
value: 'Director 1',
|
||||
url: { value: 'http://example.com/director1.html', system: 'TestSystem' }
|
||||
},
|
||||
actor: {
|
||||
value: 'Actor 1',
|
||||
role: 'Manny',
|
||||
guest: 'yes',
|
||||
url: { value: 'http://example.com/actor1.html', system: 'TestSystem' }
|
||||
},
|
||||
writer: [
|
||||
{ value: 'Writer 1', url: { value: 'http://example.com/w1.html', system: 'TestSystem' } },
|
||||
{ value: 'Writer 2', url: { value: 'http://example.com/w2.html', system: 'TestSystem' } }
|
||||
]
|
||||
}
|
||||
]
|
||||
const output = xmltv.generate({ channels, programs })
|
||||
expect(output).toBe(
|
||||
'<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">\r\n<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/><url>https://example.com</url></channel>\r\n<channel id="2TV.com"><display-name>2 TV</display-name><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title><sub-title>Sub-title 1</sub-title><desc lang="it">Description for Program 1</desc><category lang="it">Test</category><url>http://example.com/title.html</url><episode-num system="xmltv_ns">8.238.0/1</episode-num><episode-num system="onscreen">S09E239</episode-num><date>20220505</date><icon src="https://example.com/images/Program1.png?x=шеллы&sid=777"/><credits><director>Director 1<url system="TestSystem">http://example.com/director1.html</url></director><actor role="Manny" guest="yes">Actor 1<url system="TestSystem">http://example.com/actor1.html</url></actor><writer>Writer 1<url system="TestSystem">http://example.com/w1.html</url></writer><writer>Writer 2<url system="TestSystem">http://example.com/w2.html</url></writer></credits></programme>\r\n</tv>'
|
||||
)
|
||||
})
|
Loading…
Reference in New Issue