mirror of
https://github.com/iptv-org/epg.git
synced 2025-05-10 09:00:07 -04:00
113 lines
2.5 KiB
JavaScript
113 lines
2.5 KiB
JavaScript
const { db, logger, file, api } = require('../core')
|
|
const grabber = require('epg-grabber')
|
|
const _ = require('lodash')
|
|
|
|
const LOGS_DIR = process.env.LOGS_DIR || 'scripts/logs'
|
|
const PUBLIC_DIR = process.env.PUBLIC_DIR || '.gh-pages'
|
|
const GUIDES_PATH = `${LOGS_DIR}/guides.log`
|
|
const ERRORS_PATH = `${LOGS_DIR}/errors.log`
|
|
|
|
async function main() {
|
|
await setUp()
|
|
await generateGuides()
|
|
}
|
|
|
|
main()
|
|
|
|
async function generateGuides() {
|
|
logger.info(`Generating guides/...`)
|
|
|
|
const grouped = groupByGroup(await loadQueue())
|
|
|
|
logger.info('Loading "database/programs.db"...')
|
|
await db.programs.load()
|
|
await api.channels.load()
|
|
|
|
for (const key in grouped) {
|
|
const filepath = `${PUBLIC_DIR}/guides/${key}.epg.xml`
|
|
let items = grouped[key]
|
|
items = items
|
|
.map(i => {
|
|
const channel = api.channels.find({ id: i.xmltv_id })
|
|
i.name = channel.name
|
|
i.logo = channel.logo
|
|
|
|
return i
|
|
})
|
|
.filter(i => i)
|
|
|
|
const errors = []
|
|
for (const item of items) {
|
|
if (item.error) {
|
|
const error = {
|
|
xmltv_id: item.xmltv_id,
|
|
site: item.site,
|
|
site_id: item.site_id,
|
|
lang: item.lang,
|
|
date: item.date,
|
|
error: item.error
|
|
}
|
|
errors.push(error)
|
|
await logError(error)
|
|
}
|
|
}
|
|
|
|
const programs = await loadProgramsForChannels(items)
|
|
|
|
logger.info(`Creating "${filepath}"...`)
|
|
const output = grabber.convertToXMLTV({ channels: items, programs })
|
|
await file.create(filepath, output)
|
|
|
|
await logGuide({
|
|
group: key,
|
|
count: items.length,
|
|
status: errors.length > 0 ? 1 : 0
|
|
})
|
|
}
|
|
|
|
logger.info(`Done`)
|
|
}
|
|
|
|
function groupByGroup(items = []) {
|
|
const groups = {}
|
|
|
|
items.forEach(item => {
|
|
item.groups.forEach(key => {
|
|
if (!groups[key]) {
|
|
groups[key] = []
|
|
}
|
|
|
|
groups[key].push(item)
|
|
})
|
|
})
|
|
|
|
return groups
|
|
}
|
|
|
|
async function loadQueue() {
|
|
logger.info('Loading queue...')
|
|
|
|
await db.queue.load()
|
|
|
|
return await db.queue.find({}).sort({ xmltv_id: 1 })
|
|
}
|
|
|
|
async function loadProgramsForChannels(channels = []) {
|
|
const cids = channels.map(c => c._id)
|
|
|
|
return await db.programs.find({ _cid: { $in: cids } }).sort({ channel: 1, start: 1 })
|
|
}
|
|
|
|
async function setUp() {
|
|
logger.info(`Creating '${GUIDES_PATH}'...`)
|
|
await file.create(GUIDES_PATH)
|
|
await file.create(ERRORS_PATH)
|
|
}
|
|
|
|
async function logGuide(data) {
|
|
await file.append(GUIDES_PATH, JSON.stringify(data) + '\r\n')
|
|
}
|
|
|
|
async function logError(data) {
|
|
await file.append(ERRORS_PATH, JSON.stringify(data) + '\r\n')
|
|
}
|