iptv/scripts/core/generator.js

34 lines
1.0 KiB
JavaScript
Raw Permalink Normal View History

2022-02-06 22:19:09 +01:00
const { create: createPlaylist } = require('./playlist')
2022-02-12 01:11:55 +01:00
const generators = require('../generators')
2022-02-06 22:19:09 +01:00
const logger = require('./logger')
2021-12-12 05:10:18 +01:00
const file = require('./file')
2022-02-06 22:19:09 +01:00
const PUBLIC_DIR = process.env.PUBLIC_DIR || '.gh-pages'
2022-02-06 22:04:56 +01:00
const LOGS_DIR = process.env.LOGS_DIR || 'scripts/logs/generators'
2021-12-12 05:10:18 +01:00
2022-02-06 22:04:56 +01:00
const generator = {}
2022-02-05 06:12:17 +01:00
2022-02-07 23:11:47 +01:00
generator.generate = async function (name, streams = []) {
2022-02-06 22:04:56 +01:00
if (typeof generators[name] === 'function') {
try {
2022-02-07 23:11:47 +01:00
let output = await generators[name].bind()(streams)
2022-02-07 00:27:15 +01:00
output = Array.isArray(output) ? output : [output]
2022-02-06 22:19:09 +01:00
for (const type of output) {
const playlist = createPlaylist(type.items, { public: true })
2022-02-07 00:27:15 +01:00
await file.create(`${PUBLIC_DIR}/${type.filepath}`, playlist.toString())
2022-02-06 22:19:09 +01:00
}
2022-02-07 00:27:15 +01:00
await file.create(`${LOGS_DIR}/${name}.log`, output.map(toJSON).join('\n'))
2022-02-06 22:04:56 +01:00
} catch (error) {
logger.error(`generators/${name}.js: ${error.message}`)
2022-02-05 06:12:17 +01:00
}
2021-12-12 05:10:18 +01:00
}
}
2022-02-06 22:04:56 +01:00
module.exports = generator
2021-12-12 05:10:18 +01:00
2022-02-06 22:19:09 +01:00
function toJSON(type) {
2022-02-07 00:27:15 +01:00
type.count = type.items.length
delete type.items
return JSON.stringify(type)
2021-12-12 05:10:18 +01:00
}