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

150 lines
3.7 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-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-30 21:37:48 +03:00
const GUIDES_PATH = `${LOGS_DIR}/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-21 22:40:25 +03:00
logger.info('Loading "database/programs.db"...')
await db.programs.load()
2022-01-30 05:50:38 +03:00
await api.channels.load()
2022-01-21 22:40:25 +03:00
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) {
const filepath = `${PUBLIC_DIR}/guides/${key}.epg.xml`
2022-01-31 06:55:05 +03:00
const criticalErrors = []
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-01 06:17:12 +03:00
const itemPrograms = await loadProgramsForItem(item)
programs = programs.concat(itemPrograms)
2022-01-31 20:58:15 +03:00
if (channels[item.channel.xmltv_id]) continue
2022-01-30 21:37:48 +03:00
if (item.error) {
const error = {
2022-01-31 02:10:09 +03:00
xmltv_id: item.channel.xmltv_id,
2022-01-31 03:01:05 +03:00
site: item.channel.site,
2022-01-31 02:10:09 +03:00
site_id: item.channel.site_id,
lang: item.channel.lang,
2022-01-30 21:37:48 +03:00
date: item.date,
error: item.error
}
2022-01-31 06:55:05 +03:00
criticalErrors.push(error)
await logError(key, error)
} else {
if (!itemPrograms.length) {
await logError(key, {
xmltv_id: item.channel.xmltv_id,
site: item.channel.site,
site_id: item.channel.site_id,
lang: item.channel.lang,
date: item.date,
error: 'Programs not found'
})
continue
}
2022-01-09 18:15:38 +03:00
2022-01-31 06:55:05 +03:00
const channel = api.channels.find({ id: item.channel.xmltv_id })
if (!channel) {
await logError(key, {
xmltv_id: item.channel.xmltv_id,
site: item.channel.site,
site_id: item.channel.site_id,
lang: item.channel.lang,
date: item.date,
error: 'The channel has the wrong xmltv_id'
})
continue
}
2022-01-31 02:10:09 +03:00
2022-01-31 20:58:15 +03:00
channels[channel.id] = {
2022-01-31 05:59:07 +03:00
xmltv_id: channel.id,
2022-02-04 21:03:05 +03:00
name: item.channel.display_name,
2022-01-31 05:59:07 +03:00
logo: channel.logo,
2022-01-31 06:55:05 +03:00
site: item.channel.site
2022-01-31 20:58:15 +03:00
}
2022-01-31 06:55:05 +03:00
}
}
2022-01-31 04:20:14 +03:00
2022-01-31 20:58:15 +03:00
channels = Object.values(channels)
2022-01-31 23:13:48 +03:00
channels = _.sortBy(channels, 'xmltv_id')
programs = _.sortBy(programs, ['channel', 'start'])
2022-01-31 20:58:15 +03:00
2022-01-31 06:55:05 +03:00
logger.info(`Creating "${filepath}"...`)
2022-01-31 02:10:09 +03:00
const output = grabber.convertToXMLTV({ channels, programs })
2022-01-14 17:13:20 +03:00
await file.create(filepath, output)
2022-01-14 20:37:21 +03:00
2022-01-31 04:28:59 +03:00
let status = 0
2022-01-31 06:55:05 +03:00
if (criticalErrors.length > 0 || !channels.length) {
2022-01-31 04:28:59 +03:00
status = 1
}
2022-01-30 21:37:48 +03:00
await logGuide({
2022-01-21 22:40:25 +03:00
group: key,
2022-01-31 04:28:59 +03:00
count: channels.length,
status
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-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
}
2022-01-14 20:37:21 +03:00
async function setUp() {
2022-01-30 21:37:48 +03:00
logger.info(`Creating '${GUIDES_PATH}'...`)
await file.create(GUIDES_PATH)
2022-01-31 04:38:13 +03:00
await file.createDir(`${LOGS_DIR}/errors`)
2022-01-30 21:37:48 +03:00
}
async function logGuide(data) {
await file.append(GUIDES_PATH, JSON.stringify(data) + '\r\n')
2022-01-14 20:37:21 +03:00
}
2022-01-31 06:55:05 +03:00
async function logError(key, data) {
const filepath = `${LOGS_DIR}/errors/${key}.log`
if (!(await file.exists(filepath))) {
await file.create(filepath)
}
2022-01-31 04:38:13 +03:00
2022-01-31 06:55:05 +03:00
await file.append(filepath, JSON.stringify(data) + '\r\n')
2022-01-14 20:37:21 +03:00
}