diff --git a/scripts/commands/generate-guides.js b/scripts/commands/generate-guides.js index b7310846..5d81a4e6 100644 --- a/scripts/commands/generate-guides.js +++ b/scripts/commands/generate-guides.js @@ -14,12 +14,14 @@ async function main() { await setUp() await generateChannelsJson() + await generateProgramsJson() } main() async function setUp() { channels = await db.channels.find({}) + programs = await db.programs.find({}) } async function generateChannelsJson() { @@ -55,53 +57,53 @@ async function generateChannelsJson() { await file.create(`${PUBLIC_DIR}/api/channels.json`, JSON.stringify(items)) } -// async function createProgramsJson() { -// logger.info('Creating programs.json...') +async function generateProgramsJson() { + logger.info('Generating programs.json...') -// let items = programs + let items = programs -// items = _.sortBy(items, ['channel', 'start']) -// items = _.groupBy(items, 'channel') + items = _.sortBy(items, ['channel', 'start']) + items = _.groupBy(items, 'channel') -// for (let channel in items) { -// let programs = items[channel] -// programs = Object.values(_.groupBy(programs, i => i.site))[0] -// let slots = _.groupBy(programs, i => `${i.start}_${i.stop}`) + for (let channel in items) { + let channelPrograms = items[channel] + channelPrograms = Object.values(_.groupBy(channelPrograms, i => i.site))[0] + let slots = _.groupBy(channelPrograms, i => `${i.start}_${i.stop}`) -// for (let slotId in slots) { -// let program = { -// channel, -// site: null, -// title: [], -// description: [], -// categories: [], -// icons: [], -// start: null, -// stop: null -// } + for (let slotId in slots) { + let program = { + channel, + site: null, + title: [], + description: [], + categories: [], + icons: [], + start: null, + stop: null + } -// slots[slotId].forEach(item => { -// program.site = item.site -// if (item.title) program.title.push({ lang: item.lang, value: item.title }) -// if (item.description) -// program.description.push({ -// lang: item.lang, -// value: item.description -// }) -// if (item.category) program.categories.push({ lang: item.lang, value: item.category }) -// if (item.icon) program.icons.push(item.icon) -// program.start = item.start -// program.stop = item.stop -// }) + slots[slotId].forEach(item => { + program.site = item.site + if (item.title) program.title.push({ lang: item.lang, value: item.title }) + if (item.description) + program.description.push({ + lang: item.lang, + value: item.description + }) + if (item.category) program.categories.push({ lang: item.lang, value: item.category }) + if (item.icon) program.icons.push(item.icon) + program.start = item.start + program.stop = item.stop + }) -// slots[slotId] = program -// } + slots[slotId] = program + } -// items[channel] = Object.values(slots) -// } + items[channel] = Object.values(slots) + } -// await file.create(`${PUBLIC_PATH}/api/programs.json`, JSON.stringify(items, null, 2)) -// } + await file.create(`${PUBLIC_DIR}/api/programs.json`, JSON.stringify(items, null, 2)) +} // async function generateGuideXML() { // logger.info(`Generating guide.xml...`) diff --git a/tests/commands/generate-guides.test.js b/tests/commands/generate-guides.test.js index 495c92c0..4442f5be 100644 --- a/tests/commands/generate-guides.test.js +++ b/tests/commands/generate-guides.test.js @@ -24,3 +24,25 @@ it('can generate channels.json', () => { country: 'AD' }) }) + +it('can generate programs.json', () => { + const result = execSync( + 'PUBLIC_DIR=tests/__data__/output DB_DIR=tests/__data__/input/database node scripts/commands/generate-guides.js', + { encoding: 'utf8' } + ) + const json = fs.readFileSync(path.resolve('tests/__data__/output/api/programs.json'), { + encoding: 'utf8' + }) + const parsed = JSON.parse(json) + const program = parsed['AndorraTV.ad'][0] + expect(Object.keys(program).sort()).toEqual([ + 'categories', + 'channel', + 'description', + 'icons', + 'site', + 'start', + 'stop', + 'title' + ]) +})