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

80 lines
2.0 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
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() {
2022-01-29 23:18:45 +03:00
await saveToGuidesJson(await loadGuides())
2022-01-21 20:10:45 +03:00
await saveToProgramsJson(await loadPrograms())
2022-01-09 16:09:19 +03:00
}
main()
2022-01-29 23:18:45 +03:00
async function loadGuides() {
logger.info('Loading guides from database...')
2022-01-14 16:44:06 +03:00
2022-01-21 20:10:45 +03:00
await db.channels.load()
2022-01-14 16:44:06 +03:00
2022-01-21 20:10:45 +03:00
const channels = await db.channels.find({}).sort({ xmltv_id: 1 })
2022-01-15 18:23:00 +03:00
2022-01-29 23:18:45 +03:00
const output = []
2022-01-21 20:10:45 +03:00
for (const channel of channels) {
channel.groups.forEach(group => {
if (channel.programCount) {
2022-01-29 23:18:45 +03:00
output.push({
channel: channel.xmltv_id,
display_name: channel.name,
site: channel.site,
lang: channel.lang,
url: `https://iptv-org.github.io/epg/guides/${group}.epg.xml`
})
2022-01-21 20:10:45 +03:00
}
})
}
2022-01-18 22:19:36 +03:00
2022-01-29 23:18:45 +03:00
return output
2022-01-18 22:19:36 +03:00
}
async function loadPrograms() {
logger.info('Loading programs from database...')
await db.programs.load()
2022-01-21 20:10:45 +03:00
let programs = await db.programs.find({})
2022-01-18 22:19:36 +03:00
2022-01-21 20:10:45 +03:00
programs = programs.map(item => {
2022-01-21 00:01:03 +03:00
const categories = Array.isArray(item.category) ? item.category : [item.category]
2022-01-18 22:19:36 +03:00
return {
channel: item.channel,
site: item.site,
lang: item.lang,
title: item.title,
desc: item.description || null,
2022-01-21 00:01:03 +03:00
categories: categories.filter(i => i),
2022-01-19 02:01:21 +03:00
season: item.season || null,
episode: item.episode || null,
2022-01-18 22:19:36 +03:00
image: item.icon || null,
start: item.start,
stop: item.stop
}
})
2022-01-21 20:10:45 +03:00
programs = _.sortBy(programs, ['channel', 'site', 'start'])
2022-01-09 18:15:38 +03:00
2022-01-21 20:10:45 +03:00
return programs
}
2022-01-10 00:34:05 +03:00
2022-01-29 23:18:45 +03:00
async function saveToGuidesJson(guides = []) {
2022-01-30 02:41:48 +03:00
const guidesPath = `${API_DIR}/guides.json`
logger.info(`Saving to "${guidesPath}"...`)
await file.create(guidesPath, JSON.stringify(guides))
2022-01-21 20:10:45 +03:00
}
2022-01-09 18:15:38 +03:00
2022-01-21 20:10:45 +03:00
async function saveToProgramsJson(programs = []) {
const programsPath = `${API_DIR}/programs.json`
logger.info(`Saving to "${programsPath}"...`)
await file.create(programsPath, JSON.stringify(programs))
2022-01-09 16:09:19 +03:00
}