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

118 lines
2.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`
const ERRORS_PATH = `${LOGS_DIR}/errors.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()
2022-01-30 05:50:38 +03:00
await api.channels.load()
2022-01-21 22:40:25 +03:00
for (const key in grouped) {
const filepath = `${PUBLIC_DIR}/guides/${key}.epg.xml`
2022-01-30 21:37:48 +03:00
let items = grouped[key]
2022-01-30 04:02:29 +03:00
2022-01-30 21:37:48 +03:00
const errors = []
for (const item of items) {
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
}
errors.push(error)
await logError(error)
}
}
2022-01-31 02:10:09 +03:00
const programs = await loadProgramsForItems(items)
2022-01-09 18:15:38 +03:00
2022-01-15 19:40:33 +03:00
logger.info(`Creating "${filepath}"...`)
2022-01-31 02:10:09 +03:00
const channels = items
.map(item => {
const channel = api.channels.find({ id: item.channel.xmltv_id })
if (!channel) return null
return {
id: channel.id,
display_name: channel.name,
2022-01-31 03:01:05 +03:00
url: item.channel.site,
2022-01-31 02:10:09 +03:00
icon: channel.logo
}
})
.filter(i => i)
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-30 21:37:48 +03:00
await logGuide({
2022-01-21 22:40:25 +03:00
group: key,
2022-01-30 21:37:48 +03:00
count: items.length,
status: errors.length > 0 ? 1 : 0
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 02:10:09 +03:00
async function loadProgramsForItems(items = []) {
const qids = items.map(i => i._id)
2022-01-09 20:29:18 +03:00
2022-01-31 02:10:09 +03:00
return await db.programs.find({ _qid: { $in: qids } }).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)
await file.create(ERRORS_PATH)
}
async function logGuide(data) {
await file.append(GUIDES_PATH, JSON.stringify(data) + '\r\n')
2022-01-14 20:37:21 +03:00
}
2022-01-30 21:37:48 +03:00
async function logError(data) {
await file.append(ERRORS_PATH, JSON.stringify(data) + '\r\n')
2022-01-14 20:37:21 +03:00
}