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

118 lines
3.2 KiB
JavaScript
Raw Normal View History

2022-10-26 04:41:23 +03:00
const { db, api, logger, file, zip } = require('../../core')
2022-06-16 21:58:15 +03:00
const { generateXMLTV, Program, Channel } = require('epg-grabber')
2022-01-09 16:09:19 +03:00
const _ = require('lodash')
2022-01-12 13:27:27 +03:00
const PUBLIC_DIR = process.env.PUBLIC_DIR || '.gh-pages'
2022-10-26 04:41:23 +03:00
const LOGS_DIR = process.env.LOGS_DIR || 'scripts/logs'
2022-05-09 16:21:50 +03:00
const CURR_DATE = process.env.CURR_DATE || new Date()
2022-01-09 16:09:19 +03:00
2022-10-26 04:41:23 +03:00
const logPath = `${LOGS_DIR}/guides/update.log`
2023-01-10 12:41:19 +03:00
let api_channels = {}
2023-02-09 13:04:04 +03:00
let db_queue = []
2023-01-08 11:22:35 +03:00
let db_programs = []
let guides = []
2023-01-08 11:22:35 +03:00
2022-01-09 16:09:19 +03:00
async function main() {
2022-10-26 04:41:23 +03:00
logger.info(`starting...`)
2022-01-10 22:16:18 +03:00
2023-01-08 13:17:56 +03:00
logger.info('loading data/channels.json...')
2022-10-26 04:41:23 +03:00
await api.channels.load()
2022-01-21 22:40:25 +03:00
2023-01-10 12:41:19 +03:00
api.channels.all().forEach(channel => {
api_channels[channel.id] = channel
2022-10-26 04:41:23 +03:00
})
2022-02-01 06:17:12 +03:00
2023-02-09 13:04:04 +03:00
logger.info('loading database/queue.db...')
await db.queue.load()
db_queue = await db.queue.find({})
2023-02-09 13:40:24 +03:00
logger.info(`found ${db_queue.length} items`)
2023-02-09 13:04:04 +03:00
2022-10-26 04:41:23 +03:00
logger.info('loading database/programs.db...')
await db.programs.load()
2023-01-08 11:22:35 +03:00
db_programs = await db.programs.find({})
2022-10-26 04:41:23 +03:00
logger.info(`found ${db_programs.length} programs`)
2023-01-09 10:45:17 +03:00
await generate()
logger.info(`creating ${logPath}...`)
await file.create(logPath, guides.map(g => JSON.stringify(g)).join('\r\n'))
2023-01-09 10:45:17 +03:00
logger.info('finished')
2023-01-08 11:22:35 +03:00
}
main()
2023-01-09 10:45:17 +03:00
async function generate() {
2023-02-09 13:40:24 +03:00
let queue = _.uniqBy(db_queue, i => i.channel.lang + i.channel.id + i.channel.site)
queue = _.groupBy(queue, i => (i.channel ? `${i.channel.lang}/${i.channel.site}` : `_`))
2023-02-09 13:04:04 +03:00
delete queue['_']
2023-01-10 12:41:19 +03:00
let programs = _.groupBy(db_programs, p =>
p.titles.length ? `${p.titles[0].lang}/${p.site}` : `_`
)
delete programs['_']
2022-10-27 19:16:17 +03:00
2023-02-09 13:04:04 +03:00
for (let filename in queue) {
if (!queue[filename]) continue
const channels = queue[filename].map(i => {
const channelData = api_channels[i.channel.id]
channelData.site = i.channel.site
channelData.site_id = i.channel.site_id
channelData.lang = i.channel.lang
return new Channel(channelData)
})
await save(filename, channels, programs[filename])
2022-10-27 19:16:17 +03:00
for (let channel of channels) {
2023-01-10 12:41:19 +03:00
const configPath = `sites/${channel.site}/${channel.site}.config.js`
const config = require(file.resolve(configPath))
2023-01-08 13:17:56 +03:00
guides.push({
2022-10-27 19:16:17 +03:00
site: channel.site,
2023-01-10 12:41:19 +03:00
lang: channel.lang,
days: config.days,
2022-10-27 19:16:17 +03:00
channel: channel.id,
filename
2023-01-08 13:17:56 +03:00
})
2022-10-26 04:41:23 +03:00
}
}
2022-01-09 16:09:19 +03:00
}
2023-02-09 13:04:04 +03:00
async function save(filepath, channels, programs = []) {
2023-01-10 12:41:19 +03:00
let output = {
2023-02-09 13:04:04 +03:00
channels,
2023-01-10 12:41:19 +03:00
programs: [],
date: CURR_DATE
2023-01-09 10:45:17 +03:00
}
2023-01-10 12:41:19 +03:00
for (let programData of programs) {
2023-02-09 13:04:04 +03:00
let channel = channels.find(c => c.id === programData.channel)
if (!channel) continue
2023-01-08 11:22:35 +03:00
2023-01-10 12:41:19 +03:00
let program = new Program(programData, channel)
output.programs.push(program)
}
2023-01-09 10:45:17 +03:00
2023-01-10 12:41:19 +03:00
output.channels = _.sortBy(output.channels, 'id')
output.channels = _.uniqBy(output.channels, 'id')
2023-01-08 11:22:35 +03:00
2023-01-10 12:41:19 +03:00
output.programs = _.sortBy(output.programs, ['channel', 'start'])
output.programs = _.uniqBy(output.programs, p => p.channel + p.start)
2023-01-08 11:22:35 +03:00
const xmlFilepath = `${PUBLIC_DIR}/guides/${filepath}.xml`
const gzFilepath = `${PUBLIC_DIR}/guides/${filepath}.xml.gz`
logger.info(`creating ${xmlFilepath}...`)
2023-01-10 12:41:19 +03:00
const xmltv = generateXMLTV(output)
2023-01-08 11:22:35 +03:00
await file.create(xmlFilepath, xmltv)
logger.info(`creating ${gzFilepath}...`)
const compressed = await zip.compress(xmltv)
await file.create(gzFilepath, compressed)
2023-01-10 12:41:19 +03:00
return output
2023-01-08 11:22:35 +03:00
}