You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iptv/scripts/core/generator.js

34 lines
1.0 KiB
JavaScript

3 years ago
const { create: createPlaylist } = require('./playlist')
const generators = require('../generators')
3 years ago
const logger = require('./logger')
const file = require('./file')
3 years ago
const PUBLIC_DIR = process.env.PUBLIC_DIR || '.gh-pages'
3 years ago
const LOGS_DIR = process.env.LOGS_DIR || 'scripts/logs/generators'
3 years ago
const generator = {}
generator.generate = async function (name, streams = []) {
3 years ago
if (typeof generators[name] === 'function') {
try {
let output = await generators[name].bind()(streams)
3 years ago
output = Array.isArray(output) ? output : [output]
3 years ago
for (const type of output) {
const playlist = createPlaylist(type.items, { public: true })
3 years ago
await file.create(`${PUBLIC_DIR}/${type.filepath}`, playlist.toString())
3 years ago
}
3 years ago
await file.create(`${LOGS_DIR}/${name}.log`, output.map(toJSON).join('\n'))
3 years ago
} catch (error) {
logger.error(`generators/${name}.js: ${error.message}`)
}
}
}
3 years ago
module.exports = generator
3 years ago
function toJSON(type) {
3 years ago
type.count = type.items.length
delete type.items
return JSON.stringify(type)
}