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

108 lines
2.7 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-09 20:14:41 +03:00
let channels = []
let programs = []
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-10 18:27:22 +03:00
const API_DIR = process.env.API_DIR || '.gh-pages/api'
2022-01-09 16:09:19 +03:00
async function main() {
await setUp()
2022-01-09 20:14:41 +03:00
await generateChannelsJson()
2022-01-09 20:29:18 +03:00
await generateProgramsJson()
2022-01-09 16:09:19 +03:00
}
main()
2022-01-09 20:14:41 +03:00
async function setUp() {
2022-01-09 22:03:45 +03:00
channels = await loadChannels()
programs = await loadPrograms()
2022-01-09 20:14:41 +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-10 22:16:18 +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-09 22:03:45 +03:00
return Object.values(output)
2022-01-09 16:09:19 +03:00
}
2022-01-09 22:03:45 +03:00
async function loadPrograms() {
let items = await db.programs.find({})
2022-01-09 20:29:18 +03:00
items = _.sortBy(items, ['channel', 'start'])
items = _.groupBy(items, '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: [],
2022-01-09 21:43:57 +03:00
image: null,
2022-01-09 20:29:18 +03:00
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 })
2022-01-09 21:43:57 +03:00
program.image = program.image || item.icon
2022-01-09 20:29:18 +03:00
program.start = item.start
program.stop = item.stop
2022-01-10 17:08:08 +03:00
sources[channel] = item.site
2022-01-09 20:29:18 +03:00
})
2022-01-09 21:43:57 +03:00
program.title = _.uniqBy(program.title, 'lang')
program.description = _.uniqBy(program.description, 'lang')
program.categories = _.uniqBy(program.categories, 'lang')
2022-01-09 20:29:18 +03:00
slots[slotId] = program
}
items[channel] = Object.values(slots)
}
2022-01-09 22:03:45 +03:00
return items
}
async function generateChannelsJson() {
logger.info('Generating channels.json...')
2022-01-10 18:27:22 +03:00
await file.create(`${API_DIR}/channels.json`, JSON.stringify(channels))
2022-01-09 22:03:45 +03:00
}
async function generateProgramsJson() {
logger.info('Generating programs.json...')
2022-01-10 18:27:22 +03:00
await file.create(`${API_DIR}/programs.json`, JSON.stringify(programs))
2022-01-10 00:34:05 +03:00
}