mirror of
https://github.com/iptv-org/epg.git
synced 2025-05-09 16:40:07 -04:00
Stop generate extra logs
This commit is contained in:
parent
c86d32c98d
commit
42bdeb5db8
2 changed files with 8 additions and 87 deletions
|
@ -2,18 +2,9 @@ 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/update.log`
|
||||
|
||||
async function main() {
|
||||
await setUp()
|
||||
await generateGuides()
|
||||
}
|
||||
|
||||
main()
|
||||
|
||||
async function generateGuides() {
|
||||
logger.info(`Generating guides/...`)
|
||||
|
||||
logger.info('Loading "database/programs.db"...')
|
||||
|
@ -22,53 +13,17 @@ async function generateGuides() {
|
|||
|
||||
const grouped = groupByGroup(await loadQueue())
|
||||
for (const key in grouped) {
|
||||
const filepath = `${PUBLIC_DIR}/guides/${key}.epg.xml`
|
||||
const criticalErrors = []
|
||||
let channels = {}
|
||||
let programs = []
|
||||
for (const item of grouped[key]) {
|
||||
if (item.error) continue
|
||||
|
||||
const itemPrograms = await loadProgramsForItem(item)
|
||||
programs = programs.concat(itemPrograms)
|
||||
|
||||
if (channels[item.channel.xmltv_id]) continue
|
||||
|
||||
if (item.error) {
|
||||
const error = {
|
||||
xmltv_id: item.channel.xmltv_id,
|
||||
site: item.channel.site,
|
||||
site_id: item.channel.site_id,
|
||||
lang: item.channel.lang,
|
||||
date: item.date,
|
||||
error: item.error
|
||||
}
|
||||
criticalErrors.push(error)
|
||||
await logError(key, error)
|
||||
} else {
|
||||
if (!itemPrograms.length) {
|
||||
await logError(key, {
|
||||
xmltv_id: item.channel.xmltv_id,
|
||||
site: item.channel.site,
|
||||
site_id: item.channel.site_id,
|
||||
lang: item.channel.lang,
|
||||
date: item.date,
|
||||
error: 'Programs not found'
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
const channel = api.channels.find({ id: item.channel.xmltv_id })
|
||||
if (!channel) {
|
||||
await logError(key, {
|
||||
xmltv_id: item.channel.xmltv_id,
|
||||
site: item.channel.site,
|
||||
site_id: item.channel.site_id,
|
||||
lang: item.channel.lang,
|
||||
date: item.date,
|
||||
error: 'The channel has the wrong xmltv_id'
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
const channel = api.channels.find({ id: item.channel.xmltv_id })
|
||||
if (channel) {
|
||||
channels[channel.id] = {
|
||||
xmltv_id: channel.id,
|
||||
name: item.channel.display_name,
|
||||
|
@ -77,30 +32,21 @@ async function generateGuides() {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
channels = Object.values(channels)
|
||||
channels = _.sortBy(channels, 'xmltv_id')
|
||||
programs = _.sortBy(programs, ['channel', 'start'])
|
||||
|
||||
const filepath = `${PUBLIC_DIR}/guides/${key}.epg.xml`
|
||||
logger.info(`Creating "${filepath}"...`)
|
||||
const output = grabber.convertToXMLTV({ channels, programs })
|
||||
await file.create(filepath, output)
|
||||
|
||||
let status = 0
|
||||
if (criticalErrors.length > 0 || !channels.length) {
|
||||
status = 1
|
||||
}
|
||||
|
||||
await logGuide({
|
||||
group: key,
|
||||
count: channels.length,
|
||||
status
|
||||
})
|
||||
}
|
||||
|
||||
logger.info(`Done`)
|
||||
}
|
||||
|
||||
main()
|
||||
|
||||
function groupByGroup(items = []) {
|
||||
const groups = {}
|
||||
|
||||
|
@ -128,22 +74,3 @@ async function loadQueue() {
|
|||
async function loadProgramsForItem(item) {
|
||||
return await db.programs.find({ _qid: item._id }).sort({ channel: 1, start: 1 })
|
||||
}
|
||||
|
||||
async function setUp() {
|
||||
logger.info(`Creating '${GUIDES_PATH}'...`)
|
||||
await file.create(GUIDES_PATH)
|
||||
await file.createDir(`${LOGS_DIR}/errors`)
|
||||
}
|
||||
|
||||
async function logGuide(data) {
|
||||
await file.append(GUIDES_PATH, JSON.stringify(data) + '\r\n')
|
||||
}
|
||||
|
||||
async function logError(key, data) {
|
||||
const filepath = `${LOGS_DIR}/errors/${key}.log`
|
||||
if (!(await file.exists(filepath))) {
|
||||
await file.create(filepath)
|
||||
}
|
||||
|
||||
await file.append(filepath, JSON.stringify(data) + '\r\n')
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ beforeEach(() => {
|
|||
fs.copyFileSync('tests/__data__/input/database/programs.db', 'tests/__data__/output/programs.db')
|
||||
|
||||
const stdout = execSync(
|
||||
'DB_DIR=tests/__data__/output DATA_DIR=tests/__data__/input/data PUBLIC_DIR=tests/__data__/output LOGS_DIR=tests/__data__/output/logs npm run guides:update',
|
||||
'DB_DIR=tests/__data__/output DATA_DIR=tests/__data__/input/data PUBLIC_DIR=tests/__data__/output npm run guides:update',
|
||||
{ encoding: 'utf8' }
|
||||
)
|
||||
})
|
||||
|
@ -23,12 +23,6 @@ it('can generate /guides', () => {
|
|||
)
|
||||
})
|
||||
|
||||
it('can create guides.log', () => {
|
||||
expect(content('tests/__data__/output/logs/guides/update.log')).toBe(
|
||||
content('tests/__data__/expected/logs/guides/update.log')
|
||||
)
|
||||
})
|
||||
|
||||
function content(filepath) {
|
||||
const data = fs.readFileSync(path.resolve(filepath), {
|
||||
encoding: 'utf8'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue