diff --git a/scripts/commands/create-queue.js b/scripts/commands/create-queue.js index 580fe2ed..464cd28e 100644 --- a/scripts/commands/create-queue.js +++ b/scripts/commands/create-queue.js @@ -56,7 +56,7 @@ async function createQueue() { site_id: item.site_id, lang: item.lang, date: undefined, - error: 'Wrong channel ID' + error: 'The channel has the wrong xmltv_id' }) continue } diff --git a/scripts/commands/update-guides.js b/scripts/commands/update-guides.js index fe80054a..865b2fca 100644 --- a/scripts/commands/update-guides.js +++ b/scripts/commands/update-guides.js @@ -16,19 +16,17 @@ 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() + const grouped = groupByGroup(await loadQueue()) for (const key in grouped) { - const [__, site] = key.split('/') const filepath = `${PUBLIC_DIR}/guides/${key}.epg.xml` - let items = grouped[key] - - const errors = [] - for (const item of items) { + const criticalErrors = [] + const channels = [] + let programs = [] + for (const item of grouped[key]) { if (item.error) { const error = { xmltv_id: item.channel.xmltv_id, @@ -38,34 +36,52 @@ async function generateGuides() { date: item.date, error: item.error } - errors.push(error) - } - } - await logErrors(key, errors) + criticalErrors.push(error) + await logError(key, error) + } else { + const itemPrograms = await loadProgramsForItem(item) + 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 programs = await loadProgramsForItems(items) - let channels = Object.keys(_.groupBy(programs, 'channel')) + 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 + } - logger.info(`Creating "${filepath}"...`) - channels = channels - .map(id => { - const channel = api.channels.find({ id }) - if (!channel) return null - - return { + channels.push({ xmltv_id: channel.id, name: channel.name, logo: channel.logo, - site - } - }) - .filter(i => i) + site: item.channel.site + }) + programs = programs.concat(itemPrograms) + } + } + + logger.info(`Creating "${filepath}"...`) const output = grabber.convertToXMLTV({ channels, programs }) await file.create(filepath, output) let status = 0 - if (errors.length > 0 || !channels.length) { + if (criticalErrors.length > 0 || !channels.length) { status = 1 } @@ -103,10 +119,8 @@ async function loadQueue() { return await db.queue.find({}).sort({ xmltv_id: 1 }) } -async function loadProgramsForItems(items = []) { - const qids = items.map(i => i._id) - - return await db.programs.find({ _qid: { $in: qids } }).sort({ channel: 1, start: 1 }) +async function loadProgramsForItem(item) { + return await db.programs.find({ _qid: item._id }).sort({ channel: 1, start: 1 }) } async function setUp() { @@ -119,9 +133,11 @@ async function logGuide(data) { await file.append(GUIDES_PATH, JSON.stringify(data) + '\r\n') } -async function logErrors(key, errors) { - if (!errors.length) return false - errors = errors.map(e => JSON.stringify(e)).join('\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.create(`${LOGS_DIR}/errors/${key}.log`, errors) + await file.append(filepath, JSON.stringify(data) + '\r\n') } diff --git a/tests/__data__/expected/logs/errors.log b/tests/__data__/expected/logs/errors.log deleted file mode 100644 index 42045432..00000000 --- a/tests/__data__/expected/logs/errors.log +++ /dev/null @@ -1,2 +0,0 @@ -{"xmltv_id":"BravoEast.us","site":"directv.com","site_id":"237","lang":"en","date":"2022-01-21T00:00:00Z","error":"Invalid header value char"} -{"xmltv_id":"Perviykanal.ru","site":"yandex.ru","site_id":"1","lang":"ru","date":"2022-01-21T00:00:00Z","error":"Some error"} diff --git a/tests/__data__/expected/logs/errors/ca-nl/example.com.log b/tests/__data__/expected/logs/errors/ca-nl/example.com.log index 26fae7bf..eb0f2aac 100644 --- a/tests/__data__/expected/logs/errors/ca-nl/example.com.log +++ b/tests/__data__/expected/logs/errors/ca-nl/example.com.log @@ -1 +1 @@ -{"xmltv_id":"CNNInternationalEurope2.us","site":"example.com","site_id":"141","lang":"en","error":"Wrong channel ID"} +{"xmltv_id":"CNNInternationalEurope2.us","site":"example.com","site_id":"141","lang":"en","error":"The channel has the wrong xmltv_id"} diff --git a/tests/__data__/expected/logs/errors/ge/magticom.ge.log b/tests/__data__/expected/logs/errors/ge/magticom.ge.log new file mode 100644 index 00000000..4d8a3977 --- /dev/null +++ b/tests/__data__/expected/logs/errors/ge/magticom.ge.log @@ -0,0 +1 @@ +{"xmltv_id":"CNNInternationalEurope.us","site":"magticom.ge","site_id":"140","lang":"ru","date":"2022-01-21T00:00:00Z","error":"Programs not found"} diff --git a/tests/__data__/expected/logs/errors/ru/yandex.ru.log b/tests/__data__/expected/logs/errors/ru/yandex.ru.log index cba9a795..4fd847ed 100644 --- a/tests/__data__/expected/logs/errors/ru/yandex.ru.log +++ b/tests/__data__/expected/logs/errors/ru/yandex.ru.log @@ -1 +1 @@ -{"xmltv_id":"Perviykanal.ru","site":"yandex.ru","site_id":"1","lang":"ru","date":"2022-01-21T00:00:00Z","error":"Some error"} \ No newline at end of file +{"xmltv_id":"Perviykanal.ru","site":"yandex.ru","site_id":"1","lang":"ru","date":"2022-01-21T00:00:00Z","error":"Some error"} diff --git a/tests/__data__/expected/logs/errors/us/directv.com.log b/tests/__data__/expected/logs/errors/us/directv.com.log index 9410d598..9fb73a9e 100644 --- a/tests/__data__/expected/logs/errors/us/directv.com.log +++ b/tests/__data__/expected/logs/errors/us/directv.com.log @@ -1 +1 @@ -{"xmltv_id":"BravoEast.us","site":"directv.com","site_id":"237","lang":"en","date":"2022-01-21T00:00:00Z","error":"Invalid header value char"} \ No newline at end of file +{"xmltv_id":"BravoEast.us","site":"directv.com","site_id":"237","lang":"en","date":"2022-01-21T00:00:00Z","error":"Invalid header value char"} diff --git a/tests/commands/create-queue.test.js b/tests/commands/create-queue.test.js index 82a911f9..1ea09c3f 100644 --- a/tests/commands/create-queue.test.js +++ b/tests/commands/create-queue.test.js @@ -35,7 +35,7 @@ it('can create queue', () => { ) }) -it('can create errors log', () => { +it('can log errors', () => { let output = content('tests/__data__/output/logs/errors/ca-nl/example.com.log') let expected = content('tests/__data__/expected/logs/errors/ca-nl/example.com.log') diff --git a/tests/commands/update-guides.test.js b/tests/commands/update-guides.test.js index 9756d836..6d65f631 100644 --- a/tests/commands/update-guides.test.js +++ b/tests/commands/update-guides.test.js @@ -51,6 +51,11 @@ it('can log errors', () => { const expected2 = content('tests/__data__/expected/logs/errors/us/directv.com.log') expect(output2).toBe(expected2) + + const output3 = content('tests/__data__/output/logs/errors/ge/magticom.ge.log') + const expected3 = content('tests/__data__/expected/logs/errors/ge/magticom.ge.log') + + expect(output3).toBe(expected3) }) function content(filepath) {