Update update-guides.js

This commit is contained in:
Aleksandr Statciuk 2022-01-30 04:02:29 +03:00
parent 8baeed2247
commit da23d7e267
6 changed files with 35 additions and 13 deletions

View file

@ -1,4 +1,4 @@
const { db, logger, file } = require('../core')
const { db, logger, file, api } = require('../core')
const grabber = require('epg-grabber')
const _ = require('lodash')
@ -17,14 +17,23 @@ main()
async function generateGuides() {
logger.info(`Generating guides/...`)
const grouped = groupByGroup(await loadChannels())
const grouped = groupByGroup(await loadQueue())
logger.info('Loading "database/programs.db"...')
await db.programs.load()
for (const key in grouped) {
const filepath = `${PUBLIC_DIR}/guides/${key}.epg.xml`
const channels = grouped[key]
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)
const programs = await loadProgramsForChannels(channels)
const output = grabber.convertToXMLTV({ channels, programs })
@ -56,12 +65,12 @@ function groupByGroup(channels = []) {
return groups
}
async function loadChannels() {
logger.info('Loading channels...')
async function loadQueue() {
logger.info('Loading queue...')
await db.channels.load()
await db.queue.load()
return await db.channels.find({ programCount: { $gt: 0 } }).sort({ xmltv_id: 1 })
return await db.queue.find({ programCount: { $gt: 0 } }).sort({ xmltv_id: 1 })
}
async function loadProgramsForChannels(channels = []) {

15
scripts/core/api.js Normal file
View file

@ -0,0 +1,15 @@
class API {
constructor(filepath) {
this.collection = require(filepath)
}
get(id) {
return this.collection.find(c => c.id === id)
}
}
const api = {}
api.channels = new API('../data/channels.json')
module.exports = api

View file

@ -4,3 +4,4 @@ exports.file = require('./file')
exports.parser = require('./parser')
exports.timer = require('./timer')
exports.markdown = require('./markdown')
exports.api = require('./api')