diff --git a/scripts/commands/update-api.js b/scripts/commands/update-api.js index 04183d8f..347c2500 100644 --- a/scripts/commands/update-api.js +++ b/scripts/commands/update-api.js @@ -5,31 +5,45 @@ const DB_DIR = process.env.DB_DIR || 'scripts/database' const API_DIR = process.env.API_DIR || '.gh-pages/api' async function main() { - await generateChannelsJson() - await generateProgramsJson() - logger.info(`Done`) + await saveToChannelsJson(await loadChannels()) + await saveToProgramsJson(await loadPrograms()) } main() -async function generateChannelsJson() { - logger.info('Generating channels.json...') +async function loadChannels() { + logger.info('Loading channels from database...') - const channels = await loadChannels() + await db.channels.load() - const channelsPath = `${API_DIR}/channels.json` - logger.info(`Saving to "${channelsPath}"...`) - await file.create(channelsPath, JSON.stringify(channels)) -} + const channels = await db.channels.find({}).sort({ xmltv_id: 1 }) -async function generateProgramsJson() { - logger.info('Generating programs.json...') + const output = {} + for (const channel of channels) { + if (!output[channel.xmltv_id]) { + output[channel.xmltv_id] = { + id: channel.xmltv_id, + name: [], + logo: channel.logo || null, + country: channel.country, + guides: [] + } + } else { + output[channel.xmltv_id].logo = output[channel.xmltv_id].logo || channel.logo + } - const programs = await loadPrograms() + output[channel.xmltv_id].name.push(channel.name) + output[channel.xmltv_id].name = _.uniq(output[channel.xmltv_id].name) + channel.groups.forEach(group => { + if (channel.programCount) { + output[channel.xmltv_id].guides.push( + `https://iptv-org.github.io/epg/guides/${group}.epg.xml` + ) + } + }) + } - const programsPath = `${API_DIR}/programs.json` - logger.info(`Saving to "${programsPath}"...`) - await file.create(programsPath, JSON.stringify(programs)) + return Object.values(output) } async function loadPrograms() { @@ -37,9 +51,9 @@ async function loadPrograms() { await db.programs.load() - let items = await db.programs.find({}) + let programs = await db.programs.find({}) - items = items.map(item => { + programs = programs.map(item => { const categories = Array.isArray(item.category) ? item.category : [item.category] return { @@ -58,36 +72,19 @@ async function loadPrograms() { }) logger.info('Sort programs...') - return _.sortBy(items, ['channel', 'site', 'start']) + programs = _.sortBy(programs, ['channel', 'site', 'start']) + + return programs } -async function loadChannels() { - logger.info('Loading channels from database...') - - await db.channels.load() - - const items = await db.channels.find({}).sort({ xmltv_id: 1 }) - - const output = {} - for (const item of items) { - if (!output[item.xmltv_id]) { - output[item.xmltv_id] = { - id: item.xmltv_id, - name: [], - logo: item.logo || null, - country: item.country, - guides: [] - } - } else { - output[item.xmltv_id].logo = output[item.xmltv_id].logo || item.logo - } - - output[item.xmltv_id].name.push(item.name) - output[item.xmltv_id].name = _.uniq(output[item.xmltv_id].name) - output[item.xmltv_id].guides.push( - `https://iptv-org.github.io/epg/guides/${item.gid}/${item.site}.epg.xml` - ) - } - - return Object.values(output) +async function saveToChannelsJson(channels = []) { + const channelsPath = `${API_DIR}/channels.json` + logger.info(`Saving to "${channelsPath}"...`) + await file.create(channelsPath, JSON.stringify(channels)) +} + +async function saveToProgramsJson(programs = []) { + const programsPath = `${API_DIR}/programs.json` + logger.info(`Saving to "${programsPath}"...`) + await file.create(programsPath, JSON.stringify(programs)) } diff --git a/tests/__data__/input/database/channels.db b/tests/__data__/input/database/channels.db index 37493b35..dd53cc9e 100644 --- a/tests/__data__/input/database/channels.db +++ b/tests/__data__/input/database/channels.db @@ -1,4 +1,4 @@ -{"lang":"en","xmltv_id":"BravoEast.us","site_id":"237","logo":"https://www.directv.com/images/logos/channels/dark/large/579.png","name":"Bravo East","site":"directv.com","channelsPath":"sites/directv.com/directv.com_us.channels.xml","configPath":"sites/directv.com/directv.com.config.js","groups":["us"],"cluster_id":84,"country":"US","_id":"00AluKCrCnfgrl8W"} -{"lang":"fr","country":"US","xmltv_id":"CNNInternationalEurope.us","site_id":"53","logo":null,"name":"CNN International Europe","site":"chaines-tv.orange.fr","channelsPath":"sites/chaines-tv.orange.fr/chaines-tv.orange.fr_fr.channels.xml","configPath":"tests/__data__/input/sites/example.com.config.js","groups":["fr"],"cluster_id":1,"_id":"0Wefq0oMR3feCcuY"} -{"lang":"ru","country":"US","xmltv_id":"CNNInternationalEurope.us","site_id":"140","logo":"https://www.magticom.ge/images/channels/MjAxOC8wOS8xMC9lZmJhNWU5Yy0yMmNiLTRkMTAtOWY5Ny01ODM0MzY0ZTg0MmEuanBn.jpg","name":"CNN Int","site":"magticom.ge","channelsPath":"sites/magticom.ge/magticom.ge_ge.channels.xml","configPath":"tests/__data__/input/sites/example.com.config.js","groups":["ge"],"cluster_id":1,"_id":"1XzrxNkSF2AQNBrT"} -{"lang":"en","country":"ZA","xmltv_id":"MNetMovies2.za","site_id":"404a052b-3dea-4cac-a19c-de9a7d6f191d#MAP","logo":"https://rndcdn.dstv.com/dstvcms/2020/08/31/M-Net_Movies_2_Logo_4-3_lightbackground_xlrg.png","name":"M-Net Movies 2","site":"dstv.com","channelsPath":"sites/dstv.com/dstv.com_zw.channels.xml","configPath":"sites/dstv.com/dstv.com.config.js","groups":["zw"],"cluster_id":120,"_id":"1lnhXpN7g0ER5XwN"} +{"lang":"en","xmltv_id":"BravoEast.us","site_id":"237","logo":"https://www.directv.com/images/logos/channels/dark/large/579.png","name":"Bravo East","site":"directv.com","channelsPath":"sites/directv.com/directv.com_us.channels.xml","configPath":"sites/directv.com/directv.com.config.js","groups":["us/directv.com"],"cluster_id":84,"country":"US","programCount":1,"_id":"00AluKCrCnfgrl8W"} +{"lang":"fr","country":"US","xmltv_id":"CNNInternationalEurope.us","site_id":"53","logo":null,"name":"CNN International Europe","site":"chaines-tv.orange.fr","channelsPath":"sites/chaines-tv.orange.fr/chaines-tv.orange.fr_fr.channels.xml","configPath":"tests/__data__/input/sites/example.com.config.js","groups":["fr/chaines-tv.orange.fr"],"cluster_id":1,"programCount":1,"_id":"0Wefq0oMR3feCcuY"} +{"lang":"ru","country":"US","xmltv_id":"CNNInternationalEurope.us","site_id":"140","logo":"https://www.magticom.ge/images/channels/MjAxOC8wOS8xMC9lZmJhNWU5Yy0yMmNiLTRkMTAtOWY5Ny01ODM0MzY0ZTg0MmEuanBn.jpg","name":"CNN Int","site":"magticom.ge","channelsPath":"sites/magticom.ge/magticom.ge_ge.channels.xml","configPath":"tests/__data__/input/sites/example.com.config.js","groups":["ge/magticom.ge"],"cluster_id":1,"programCount":1,"_id":"1XzrxNkSF2AQNBrT"} +{"lang":"en","country":"ZA","xmltv_id":"MNetMovies2.za","site_id":"404a052b-3dea-4cac-a19c-de9a7d6f191d#MAP","logo":"https://rndcdn.dstv.com/dstvcms/2020/08/31/M-Net_Movies_2_Logo_4-3_lightbackground_xlrg.png","name":"M-Net Movies 2","site":"dstv.com","channelsPath":"sites/dstv.com/dstv.com_zw.channels.xml","configPath":"sites/dstv.com/dstv.com.config.js","groups":["zw/dstv.com"],"cluster_id":120,"programCount":1,"_id":"1lnhXpN7g0ER5XwN"}