diff --git a/package.json b/package.json index d6f96d5..5990b96 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "author": "Dan Ferguson", "license": "ISC", "dependencies": { + "JSONStream": "1.0.5", "angular": "^1.7.9", "angular-router-browserify": "0.0.2", "angular-vs-repeat": "2.0.13", diff --git a/src/api.js b/src/api.js index ef2dd40..a00501a 100644 --- a/src/api.js +++ b/src/api.js @@ -5,6 +5,7 @@ const fs = require('fs') const databaseMigration = require('./database-migration'); const channelCache = require('./channel-cache') const constants = require('./constants'); +const JSONStream = require('JSONStream'); const FFMPEGInfo = require('./ffmpeg-info'); const PlexServerDB = require('./dao/plex-server-db'); const Plex = require("./plex.js"); @@ -232,9 +233,10 @@ function api(db, channelDB, fillerDB, customShowDB, xmltvInterval, guideService try { let number = parseInt(req.params.number, 10); let channel = await channelCache.getChannelConfig(channelDB, number); + if (channel.length == 1) { - channel = channel[0]; - res.send( channel ); + channel = channel[0]; + res.send(channel); } else { return res.status(404).send("Channel not found"); } @@ -243,6 +245,61 @@ function api(db, channelDB, fillerDB, customShowDB, xmltvInterval, guideService res.status(500).send("error"); } }) + router.get('/api/channel/programless/:number', async (req, res) => { + try { + let number = parseInt(req.params.number, 10); + let channel = await channelCache.getChannelConfig(channelDB, number); + + if (channel.length == 1) { + channel = channel[0]; + let copy = {}; + Object.keys(channel).forEach( (key) => { + if (key != 'programs') { + copy[key] = channel[key]; + } + } ); + res.send(copy); + } else { + return res.status(404).send("Channel not found"); + } + } catch(err) { + console.error(err); + res.status(500).send("error"); + } + }) + + router.get('/api/channel/programs/:number', async (req, res) => { + try { + let number = parseInt(req.params.number, 10); + let channel = await channelCache.getChannelConfig(channelDB, number); + + if (channel.length == 1) { + channel = channel[0]; + let programs = channel.programs; + if (typeof(programs) === 'undefined') { + return res.status(404).send("Channel doesn't have programs?"); + } + res.writeHead(200, { + 'Content-Type': 'application.json' + }); + + let transformStream = JSONStream.stringify(); //false makes it not add 'separators' + transformStream.pipe(res); + + for (let i = 0; i < programs.length; i++) { + transformStream.write( programs[i] ); + await throttle(); + } + transformStream.end(); + + } else { + return res.status(404).send("Channel not found"); + } + } catch(err) { + console.error(err); + res.status(500).send("error"); + } + }) router.get('/api/channel/description/:number', async (req, res) => { try { let number = parseInt(req.params.number, 10); @@ -1014,3 +1071,10 @@ function api(db, channelDB, fillerDB, customShowDB, xmltvInterval, guideService return router } + + +async function throttle() { + return new Promise((resolve) => { + setImmediate(() => resolve()); + }); +} diff --git a/web/controllers/channels.js b/web/controllers/channels.js index c9fa27f..1cc861d 100644 --- a/web/controllers/channels.js +++ b/web/controllers/channels.js @@ -80,7 +80,12 @@ module.exports = function ($scope, dizquetv) { $scope.showChannelConfig = true } else { $scope.channels[index].pending = true; - let ch = await dizquetv.getChannel($scope.channels[index].number); + let p = await Promise.all([ + dizquetv.getChannelProgramless($scope.channels[index].number), + dizquetv.getChannelPrograms($scope.channels[index].number), + ]); + let ch = p[0]; + ch.programs = p[1]; let newObj = ch; newObj.startTime = new Date(newObj.startTime) $scope.originalChannelNumber = newObj.number; diff --git a/web/services/dizquetv.js b/web/services/dizquetv.js index 2ad247e..4e8b8f6 100644 --- a/web/services/dizquetv.js +++ b/web/services/dizquetv.js @@ -137,6 +137,13 @@ module.exports = function ($http, $q) { return $http.get(`/api/channel/description/${number}`).then( (d) => { return d.data } ) }, + getChannelProgramless: (number) => { + return $http.get(`/api/channel/programless/${number}`).then( (d) => { return d.data }) + }, + getChannelPrograms: (number) => { + return $http.get(`/api/channel/programs/${number}`).then( (d) => { return d.data } ) + }, + getChannelNumbers: () => { return $http.get('/api/channelNumbers').then( (d) => { return d.data } )