iptv/scripts/core/generator.js

42 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-02-06 22:19:09 +01:00
const { create: createPlaylist } = require('./playlist')
const logger = require('./logger')
2021-12-12 05:10:18 +01:00
const file = require('./file')
2022-02-06 22:04:56 +01:00
const generators = require('../generators')
2022-02-06 23:22:20 +01:00
const _ = require('lodash')
2022-02-07 23:11:47 +01:00
const { orderBy } = require('natural-orderby')
2021-12-12 05:10:18 +01:00
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
streams = orderBy(
streams,
2022-02-07 00:27:15 +01:00
['channel_name', 'status.level', 'resolution.height'],
2022-02-06 23:22:20 +01:00
['asc', 'asc', 'desc']
)
2022-02-07 23:11:47 +01:00
streams = _.uniqBy(streams, stream => stream.channel_id || _.uniqueId())
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
}