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

38 lines
1.2 KiB
JavaScript

3 years ago
const { create: createPlaylist } = require('./playlist')
const logger = require('./logger')
const file = require('./file')
3 years ago
const generators = require('../generators')
3 years ago
const _ = require('lodash')
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 = {}
3 years ago
generator.generate = async function (name, items = []) {
if (typeof generators[name] === 'function') {
try {
3 years ago
items = _.orderBy(
items,
['channel.name', 'status.level', 'resolution.height'],
['asc', 'asc', 'desc']
)
items = _.uniqBy(items, s => s.channel_id || _.uniqueId())
3 years ago
const output = await generators[name].bind()(items)
await file.create(`${LOGS_DIR}/${name}.log`, output.map(toJSON).join('\n'))
for (const type of output) {
const playlist = createPlaylist(type.items, { public: true })
await file.create(`${PUBLIC_DIR}/${name}/${type.id}.m3u`, playlist.toString())
}
3 years ago
} catch (error) {
logger.error(`generators/${name}.js: ${error.message}`)
}
}
}
3 years ago
module.exports = generator
3 years ago
function toJSON(type) {
return JSON.stringify({ id: type.id, count: type.items.length })
}