2022-06-17 20:00:23 +03:00
|
|
|
const { db, logger, file, zip } = require('../../core')
|
2022-06-16 21:58:15 +03:00
|
|
|
const { generateXMLTV, Program, Channel } = require('epg-grabber')
|
2022-01-09 16:09:19 +03:00
|
|
|
const _ = require('lodash')
|
|
|
|
|
|
2022-01-12 13:27:27 +03:00
|
|
|
const PUBLIC_DIR = process.env.PUBLIC_DIR || '.gh-pages'
|
2022-05-09 16:21:50 +03:00
|
|
|
const CURR_DATE = process.env.CURR_DATE || new Date()
|
2022-01-09 16:09:19 +03:00
|
|
|
|
|
|
|
|
async function main() {
|
2022-01-12 13:27:27 +03:00
|
|
|
logger.info(`Generating guides/...`)
|
2022-01-10 22:16:18 +03:00
|
|
|
|
2022-01-21 22:40:25 +03:00
|
|
|
logger.info('Loading "database/programs.db"...')
|
|
|
|
|
await db.programs.load()
|
|
|
|
|
|
2022-03-05 21:23:56 +03:00
|
|
|
let total = 0
|
2022-01-31 06:55:05 +03:00
|
|
|
const grouped = groupByGroup(await loadQueue())
|
2022-01-21 22:40:25 +03:00
|
|
|
for (const key in grouped) {
|
2022-01-31 20:58:15 +03:00
|
|
|
let channels = {}
|
2022-01-31 06:55:05 +03:00
|
|
|
let programs = []
|
|
|
|
|
for (const item of grouped[key]) {
|
2022-02-28 14:53:35 +03:00
|
|
|
if (item.error) continue
|
|
|
|
|
|
2022-02-01 06:17:12 +03:00
|
|
|
const itemPrograms = await loadProgramsForItem(item)
|
|
|
|
|
programs = programs.concat(itemPrograms)
|
|
|
|
|
|
2022-06-16 21:58:15 +03:00
|
|
|
if (channels[item.channel.id]) continue
|
2022-06-17 20:00:23 +03:00
|
|
|
channels[item.channel.id] = new Channel(item.channel)
|
2022-01-31 06:55:05 +03:00
|
|
|
}
|
2022-01-31 23:13:48 +03:00
|
|
|
programs = _.sortBy(programs, ['channel', 'start'])
|
2022-07-06 17:32:45 +03:00
|
|
|
programs = programs.map(p => new Program(p, channels[p.channel]))
|
2022-08-28 00:30:29 +03:00
|
|
|
programs = _.uniqBy(programs, p => p.channel + p.start)
|
2022-03-05 21:23:56 +03:00
|
|
|
total += programs.length
|
2022-07-06 17:32:45 +03:00
|
|
|
channels = Object.values(channels)
|
|
|
|
|
channels = _.sortBy(channels, 'id')
|
2022-01-31 20:58:15 +03:00
|
|
|
|
2022-08-25 00:09:02 +03:00
|
|
|
const filepath = `${PUBLIC_DIR}/guides/${key}.epg.xml.gz`
|
2022-01-31 06:55:05 +03:00
|
|
|
logger.info(`Creating "${filepath}"...`)
|
2022-08-24 22:43:58 +03:00
|
|
|
const xmltv = generateXMLTV({ channels, programs, date: CURR_DATE })
|
|
|
|
|
const compressed = await zip.compress(xmltv)
|
|
|
|
|
await file.create(filepath, compressed)
|
2022-01-14 17:13:20 +03:00
|
|
|
}
|
2022-01-15 19:40:33 +03:00
|
|
|
|
2022-03-05 21:23:56 +03:00
|
|
|
if (!total) {
|
|
|
|
|
logger.error('\nError: No programs found')
|
|
|
|
|
process.exit(1)
|
|
|
|
|
} else {
|
|
|
|
|
logger.info(`Done`)
|
|
|
|
|
}
|
2022-01-09 16:09:19 +03:00
|
|
|
}
|
|
|
|
|
|
2022-02-28 14:53:35 +03:00
|
|
|
main()
|
|
|
|
|
|
2022-01-30 21:37:48 +03:00
|
|
|
function groupByGroup(items = []) {
|
2022-01-21 22:40:25 +03:00
|
|
|
const groups = {}
|
|
|
|
|
|
2022-01-30 21:37:48 +03:00
|
|
|
items.forEach(item => {
|
|
|
|
|
item.groups.forEach(key => {
|
2022-01-21 22:40:25 +03:00
|
|
|
if (!groups[key]) {
|
|
|
|
|
groups[key] = []
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-30 21:37:48 +03:00
|
|
|
groups[key].push(item)
|
2022-01-21 22:40:25 +03:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return groups
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-30 04:02:29 +03:00
|
|
|
async function loadQueue() {
|
|
|
|
|
logger.info('Loading queue...')
|
2022-01-15 18:26:03 +03:00
|
|
|
|
2022-01-30 04:02:29 +03:00
|
|
|
await db.queue.load()
|
2022-01-15 18:26:03 +03:00
|
|
|
|
2022-01-30 21:37:48 +03:00
|
|
|
return await db.queue.find({}).sort({ xmltv_id: 1 })
|
2022-01-15 18:26:03 +03:00
|
|
|
}
|
|
|
|
|
|
2022-01-31 06:55:05 +03:00
|
|
|
async function loadProgramsForItem(item) {
|
|
|
|
|
return await db.programs.find({ _qid: item._id }).sort({ channel: 1, start: 1 })
|
2022-01-09 22:03:45 +03:00
|
|
|
}
|