diff --git a/src/api.js b/src/api.js index 64599bc..4adea09 100644 --- a/src/api.js +++ b/src/api.js @@ -1053,6 +1053,19 @@ function api(db, channelDB, fillerDB, customShowDB, xmltvInterval, guideService delete program.streams; delete program.durationStr; delete program.commercials; + if ( + (typeof(program.duration) === 'undefined') + || + (program.duration <= 0) + ) { + console.error(`Input contained a program with invalid duration: ${program.duration}. This program has been deleted`); + return []; + } + if (! Number.isInteger(program.duration) ) { + console.error(`Input contained a program with invalid duration: ${program.duration}. Duration got fixed to be integer.`); + program.duration = Math.ceil(program.duration); + } + return [ program ]; } function cleanUpChannel(channel) { @@ -1063,10 +1076,15 @@ function api(db, channelDB, fillerDB, customShowDB, xmltvInterval, guideService ) { channel.groupTitle = "dizqueTV"; } - channel.programs.forEach( cleanUpProgram ); + channel.programs = channel.programs.flatMap( cleanUpProgram ); delete channel.fillerContent; delete channel.filler; - channel.fallback.forEach( cleanUpProgram ); + channel.fallback = channel.fallback.flatMap( cleanUpProgram ); + channel.duration = 0; + for (let i = 0; i < channel.programs.length; i++) { + channel.duration += channel.programs[i].duration; + } + } async function streamToolResult(toolRes, res) { diff --git a/src/database-migration.js b/src/database-migration.js index 4cf6321..9ce4a9d 100644 --- a/src/database-migration.js +++ b/src/database-migration.js @@ -20,7 +20,7 @@ const path = require('path'); var fs = require('fs'); -const TARGET_VERSION = 802; +const TARGET_VERSION = 803; const STEPS = [ // [v, v2, x] : if the current version is v, call x(db), and version becomes v2 @@ -42,6 +42,7 @@ const STEPS = [ [ 702, 800, (db,channels,dir) => reAddIcon(dir) ], [ 800, 801, (db) => addImageCache(db) ], [ 801, 802, () => addGroupTitle() ], + [ 802, 803, () => fixNonIntegerDurations() ], ] const { v4: uuidv4 } = require('uuid'); @@ -834,6 +835,48 @@ function addGroupTitle() { console.log("Done migrating group titles in channels."); } +function fixNonIntegerDurations() { + + function migrateChannel(channel) { + let programs = channel.programs; + let fixedCount = 0; + channel.duration = 0; + for (let i = 0; i < programs.length; i++) { + let program = programs[i]; + if ( ! Number.isInteger(program.duration) ) { + fixedCount++; + program.duration = Math.ceil(program.duration); + programs[i] = program; + } + channel.duration += program.duration; + } + if (fixedCount != 0) { + console.log(`Found ${fixedCount} non-integer durations in channel ${channel.number}, they were fixed but you should consider running random slots again so that the milliseconds are accurate.`); + } + + return { + fixed: (fixedCount != 0), + newChannel: channel, + }; + } + + console.log("Checking channels to make sure they weren't corrupted by random slots bug #350..."); + let channels = path.join(process.env.DATABASE, 'channels'); + let channelFiles = fs.readdirSync(channels); + for (let i = 0; i < channelFiles.length; i++) { + if (path.extname( channelFiles[i] ) === '.json') { + console.log("Checking durations in channel : " + channelFiles[i] +"..." ); + let channelPath = path.join(channels, channelFiles[i]); + let channel = JSON.parse(fs.readFileSync(channelPath, 'utf-8')); + let { fixed, newChannel } = migrateChannel(channel); + + if (fixed) { + fs.writeFileSync( channelPath, JSON.stringify(newChannel), 'utf-8'); + } + } + } + console.log("Done checking channels."); +} diff --git a/src/services/random-slots-service.js b/src/services/random-slots-service.js index 5cac49c..d7c149b 100644 --- a/src/services/random-slots-service.js +++ b/src/services/random-slots-service.js @@ -405,10 +405,14 @@ module.exports = async( programs, schedule ) => { } } else if (flexBetween) { //just distribute it equitatively - let div = rem / pads.length; + let div = Math.floor( rem / pads.length ); + let totalAdded = 0; for (let i = 0; i < pads.length; i++) { pads[i].pad += div; + totalAdded += div; } + pads[0].pad += rem - totalAdded; + } else { //also add div to the latest item pads[ pads.length - 1].pad += rem;