diff --git a/README.md b/README.md index 6f1e0bf..bbf6c65 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,8 @@ This takes ESPN/ESPN+, FOX Sports, and MLB.tv programming and transforms it into * This was not made for pirating streams. This is made for using your own credentials and have a different presentation than the ESPN, FOX Sports, and MLB.tv apps currently provide. * The Mouse might not like it and it could be taken down at any minute. Enjoy it while it lasts. ¯\\_(ツ)_/¯ +If you're using the `USE_LINEAR` option, have your client pull the XMLTV file every 4 hours so it can stay current if channels change. + # Using The server exposes 2 main endpoints: diff --git a/services/build-schedule.ts b/services/build-schedule.ts index 665032b..a9bfc5a 100644 --- a/services/build-schedule.ts +++ b/services/build-schedule.ts @@ -10,7 +10,11 @@ const isDigitalNetwork = (network: string): boolean => const isEventOnLinear = (event: IEntry): boolean => event.from !== 'mlbtv' && !isDigitalNetwork(event.network); const scheduleEntry = async (entry: IEntry & IDocument, startChannel: number): Promise => { - const availableChannels = await db.schedule.find({endsAt: {$lt: entry.start}}).sort({channel: 1}); + let channelNum: number; + + const availableChannels = await db.schedule + .find({channel: {$gte: startChannel}, endsAt: {$lt: entry.start}}) + .sort({channel: 1}); if (!availableChannels || !availableChannels.length) { const channelNums = await db.schedule.count({}); @@ -19,18 +23,19 @@ const scheduleEntry = async (entry: IEntry & IDocument, startChannel: number): P return; } - const newChannelNum = channelNums + startChannel; + channelNum = channelNums + startChannel; await db.schedule.insert({ - channel: newChannelNum, + channel: channelNum, endsAt: entry.end, }); - - await db.entries.update({_id: entry._id}, {$set: {channel: newChannelNum}}); } else { + channelNum = +availableChannels[0].channel; + await db.schedule.update({_id: availableChannels[0]._id}, {$set: {endsAt: entry.end}}); - await db.entries.update({_id: entry._id}, {$set: {channel: availableChannels[0].channel}}); } + + await db.entries.update({_id: entry._id}, {$set: {channel: channelNum}}); }; const scheduleLinearEntries = async (): Promise => { @@ -44,12 +49,18 @@ const scheduleLinearEntries = async (): Promise => { } const channelNum = exisingLinearChannel.channel + START_CHANNEL; + const scheduleExists = await db.schedule.findOne({channel: channelNum}); + + if (scheduleExists) { + await db.schedule.update({_id: scheduleExists._id}, {$set: {endsAt: entry.end}}); + } else { + await db.schedule.insert({ + channel: channelNum, + endsAt: entry.end, + }); + } await db.entries.update({_id: entry._id}, {$set: {channel: channelNum}}); - await db.schedule.insert({ - channel: channelNum, - endsAt: entry.end, - }); } }; @@ -68,8 +79,8 @@ export const scheduleEntries = async (firstRun = true): Promise => { {multi: true}, ); - // Remove linear channels that got added accidentally if (firstRun) { + // Remove linear channels that got added accidentally // eslint-disable-next-line object-shorthand const numRemoved = await db.linear.remove( { @@ -84,6 +95,34 @@ export const scheduleEntries = async (firstRun = true): Promise => { needReschedule = true; await db.linear.remove({}, {multi: true}); } + + // Check to see if some digital events creeped into the linear channels + const linearChannelNums = await db.linear.count({}); + const entriesInLinear = await db.entries.count({channel: {$lt: linearChannelNums + START_CHANNEL}}); + + if (entriesInLinear > 0) { + needReschedule = true; + } + + // Check to see if linear channel schedules got added a million times + if (linearChannelNums > 0) { + for (let a = 0; a < linearChannelNums; a++) { + const scheduleCount = await db.schedule.count({channel: a + START_CHANNEL}); + + if (scheduleCount > 1) { + needReschedule = true; + } + } + } + } + } else { + if (firstRun) { + // Check to see if we're not using linear channels anymore + const linearChannelNums = await db.linear.remove({}, {multi: true}); + + if (linearChannelNums > 0) { + needReschedule = true; + } } } @@ -130,27 +169,23 @@ export const scheduleEntries = async (firstRun = true): Promise => { } } - if (useLinear) { - if (firstRun) { - if (needReschedule) { - console.log('***************************************************************************'); - console.log('** **'); - console.log('** Need to rebuild the schedule because the USE_LINEAR variable was used **'); - console.log('** or networks have been changed **'); - console.log('** **'); - console.log('***************************************************************************'); - console.log('** THIS WILL BREAK SCHEDULED RECORDINGS IN YOUR DVR SOFTWARE **'); - console.log('***************************************************************************'); - - await db.entries.update({}, {$unset: {channel: true}}, {multi: true}); - await db.schedule.remove({}, {multi: true}); - - return await scheduleEntries(); - } + if (needReschedule) { + console.log('******************************************************************************'); + console.log('** **'); + console.log('** Need to rebuild the schedule because the USE_LINEAR variable has changed **'); + console.log('** or networks have changed **'); + console.log('** **'); + console.log('******************************************************************************'); + console.log('** THIS WILL BREAK SCHEDULED RECORDINGS IN YOUR DVR SOFTWARE **'); + console.log('******************************************************************************'); - return await scheduleLinearEntries(); - } else { - return await scheduleLinearEntries(); - } + await db.entries.update({}, {$unset: {channel: true}}, {multi: true}); + await db.schedule.remove({}, {multi: true}); + + return await scheduleEntries(); + } + + if (useLinear) { + return await scheduleLinearEntries(); } };