Files
epg/scripts/commands/update-guides.js

90 lines
2.0 KiB
JavaScript
Raw Normal View History

2022-01-30 04:02:29 +03:00
const { db, logger, file, api } = require('../core')
2022-01-19 16:29:17 +03:00
const grabber = require('epg-grabber')
2022-01-09 16:09:19 +03:00
const _ = require('lodash')
2022-01-09 20:14:41 +03:00
const DB_DIR = process.env.DB_DIR || 'scripts/database'
2022-01-14 20:37:21 +03:00
const LOGS_DIR = process.env.LOGS_DIR || 'scripts/logs'
2022-01-12 13:27:27 +03:00
const PUBLIC_DIR = process.env.PUBLIC_DIR || '.gh-pages'
2022-01-15 18:26:03 +03:00
const LOG_PATH = `${LOGS_DIR}/update-guides.log`
2022-01-09 16:09:19 +03:00
async function main() {
2022-01-14 20:37:21 +03:00
await setUp()
2022-01-12 13:27:27 +03:00
await generateGuides()
2022-01-09 16:09:19 +03:00
}
main()
2022-01-12 13:27:27 +03:00
async function generateGuides() {
logger.info(`Generating guides/...`)
2022-01-10 22:16:18 +03:00
2022-01-30 04:02:29 +03:00
const grouped = groupByGroup(await loadQueue())
2022-01-10 00:34:05 +03:00
2022-01-21 22:40:25 +03:00
logger.info('Loading "database/programs.db"...')
await db.programs.load()
for (const key in grouped) {
const filepath = `${PUBLIC_DIR}/guides/${key}.epg.xml`
2022-01-30 04:02:29 +03:00
const items = grouped[key]
const channels = items
.map(i => {
const channel = api.channels.get(i.xmltv_id)
i.name = channel.name
i.logo = channel.logo
return i
})
.filter(i => i)
2022-01-21 22:40:25 +03:00
const programs = await loadProgramsForChannels(channels)
const output = grabber.convertToXMLTV({ channels, programs })
2022-01-09 18:15:38 +03:00
2022-01-15 19:40:33 +03:00
logger.info(`Creating "${filepath}"...`)
2022-01-14 17:13:20 +03:00
await file.create(filepath, output)
2022-01-14 20:37:21 +03:00
await log({
2022-01-21 22:40:25 +03:00
group: key,
count: channels.length
2022-01-14 20:37:21 +03:00
})
2022-01-14 17:13:20 +03:00
}
2022-01-15 19:40:33 +03:00
logger.info(`Done`)
2022-01-09 16:09:19 +03:00
}
2022-01-21 22:40:25 +03:00
function groupByGroup(channels = []) {
const groups = {}
channels.forEach(channel => {
channel.groups.forEach(key => {
if (!groups[key]) {
groups[key] = []
}
groups[key].push(channel)
})
})
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 04:02:29 +03:00
return await db.queue.find({ programCount: { $gt: 0 } }).sort({ xmltv_id: 1 })
2022-01-15 18:26:03 +03:00
}
2022-01-21 22:40:25 +03:00
async function loadProgramsForChannels(channels = []) {
const cids = channels.map(c => c._id)
2022-01-09 20:29:18 +03:00
2022-01-21 22:40:25 +03:00
return await db.programs.find({ _cid: { $in: cids } }).sort({ channel: 1, start: 1 })
2022-01-09 22:03:45 +03:00
}
2022-01-14 20:37:21 +03:00
async function setUp() {
2022-01-15 18:26:03 +03:00
logger.info(`Creating '${LOG_PATH}'...`)
await file.create(LOG_PATH)
2022-01-14 20:37:21 +03:00
}
async function log(data) {
2022-01-15 18:26:03 +03:00
await file.append(LOG_PATH, JSON.stringify(data) + '\n')
2022-01-14 20:37:21 +03:00
}