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

141 lines
4.1 KiB
JavaScript
Raw Normal View History

2022-01-10 00:34:05 +03:00
const { db, logger, file, xml } = require('../core')
2022-01-09 16:09:19 +03:00
const _ = require('lodash')
2022-01-10 17:08:08 +03:00
let sources = {}
2022-01-09 16:09:19 +03:00
2022-01-09 20:14:41 +03:00
const DB_DIR = process.env.DB_DIR || 'scripts/database'
2022-01-12 13:27:27 +03:00
const PUBLIC_DIR = process.env.PUBLIC_DIR || '.gh-pages'
2022-01-09 16:09:19 +03:00
async function main() {
2022-01-12 13:27:27 +03:00
await generateEpgXML()
await generateGuides()
2022-01-09 16:09:19 +03:00
}
main()
2022-01-12 13:27:27 +03:00
async function generateEpgXML() {
2022-01-10 18:27:22 +03:00
logger.info(`Generating epg.xml...`)
2022-01-12 13:27:27 +03:00
const channels = await loadChannels()
const programs = await loadPrograms()
2022-01-10 18:27:22 +03:00
const output = {}
const filteredChannels = Object.keys(programs)
2022-01-10 22:42:13 +03:00
output.channels = _.flatten(Object.values(channels))
2022-01-10 18:27:22 +03:00
.filter(c => filteredChannels.includes(c.id))
.map(c => {
c.site = sources[c.id]
return c
})
output.programs = _.flatten(Object.values(programs))
2022-01-12 13:27:27 +03:00
await file.create(`${PUBLIC_DIR}/epg.xml`, xml.create(output))
2022-01-10 18:27:22 +03:00
}
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-12 13:27:27 +03:00
let channels = await db.channels.find({}).sort({ xmltv_id: 1 })
const programs = await db.programs.find({}).sort({ channel: 1, start: 1 })
const grouped = _.groupBy(programs, i => `${i.country.toLowerCase()}/${i.site}`)
2022-01-10 22:16:18 +03:00
2022-01-12 13:27:27 +03:00
for (let groupId in grouped) {
const filepath = `${PUBLIC_DIR}/guides/${groupId}.epg.xml`
const groupProgs = grouped[groupId]
const groupChannels = Object.keys(_.groupBy(groupProgs, 'channel')).map(key => {
let [_, site] = groupId.split('/')
return channels.find(i => i.xmltv_id === key && i.site === site)
})
const output = xml.create({ channels: groupChannels, programs: groupProgs })
await file.create(filepath, output)
2022-01-10 22:16:18 +03:00
}
}
2022-01-09 22:03:45 +03:00
async function loadChannels() {
let items = await db.channels.find({}).sort({ xmltv_id: 1 })
2022-01-09 18:15:38 +03:00
2022-01-09 21:43:57 +03:00
let output = {}
2022-01-10 00:34:05 +03:00
items.forEach(item => {
if (!output[item.xmltv_id]) {
output[item.xmltv_id] = {
id: item.xmltv_id,
name: [item.name],
logo: item.logo || null,
2022-01-11 00:11:24 +03:00
country: item.country
2022-01-09 18:15:38 +03:00
}
} else {
2022-01-10 00:34:05 +03:00
output[item.xmltv_id].logo = output[item.xmltv_id].logo || item.logo
output[item.xmltv_id].name.push(item.name)
2022-01-09 18:15:38 +03:00
}
2022-01-10 00:34:05 +03:00
output[item.xmltv_id].name = _.uniq(output[item.xmltv_id].name)
2022-01-09 18:15:38 +03:00
})
2022-01-10 22:42:13 +03:00
return output
2022-01-09 16:09:19 +03:00
}
2022-01-09 22:03:45 +03:00
async function loadPrograms() {
2022-01-12 13:27:27 +03:00
let programs = await db.programs.find({})
programs = programs.map(program => {
return {
title: program.title ? [{ lang: program.lang, value: program.title }] : [],
description: program.description ? [{ lang: program.lang, value: program.description }] : [],
categories: program.category ? [{ lang: program.lang, value: program.category }] : [],
icon: program.icon,
channel: program.channel,
lang: program.lang,
start: program.start,
stop: program.stop,
site: program.site,
country: program.country,
_id: program._id
2022-01-09 20:29:18 +03:00
}
2022-01-12 13:27:27 +03:00
})
2022-01-09 20:29:18 +03:00
2022-01-12 13:27:27 +03:00
programs = _.sortBy(programs, ['channel', 'start'])
programs = _.groupBy(programs, 'channel')
// for (let channel in items) {
// let channelPrograms = items[channel]
// channelPrograms = Object.values(_.groupBy(channelPrograms, i => i.site))[0]
// let slots = _.groupBy(channelPrograms, i => `${i.start}_${i.stop}`)
// for (let slotId in slots) {
// let program = {
// channel,
// title: [],
// description: [],
// categories: [],
// image: null,
// start: null,
// stop: null
// }
// slots[slotId].forEach(item => {
// if (item.title) program.title.push({ lang: item.lang, value: item.title })
// if (item.description)
// program.description.push({
// lang: item.lang,
// value: item.description
// })
// if (item.category) program.categories.push({ lang: item.lang, value: item.category })
// program.image = program.image || item.icon
// program.start = item.start
// program.stop = item.stop
// sources[channel] = item.site
// })
// program.title = _.uniqBy(program.title, 'lang')
// program.description = _.uniqBy(program.description, 'lang')
// program.categories = _.uniqBy(program.categories, 'lang')
// slots[slotId] = program
// }
// items[channel] = Object.values(slots)
// }
return programs
2022-01-09 22:03:45 +03:00
}