Skip to content

Commit

Permalink
Hopefully fix issue with digital events creeping into linear channels
Browse files Browse the repository at this point in the history
  • Loading branch information
m0ngr31 committed Apr 27, 2023
1 parent daa6570 commit a5a3b2e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 32 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
99 changes: 67 additions & 32 deletions services/build-schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
const availableChannels = await db.schedule.find<IChannel>({endsAt: {$lt: entry.start}}).sort({channel: 1});
let channelNum: number;

const availableChannels = await db.schedule
.find<IChannel>({channel: {$gte: startChannel}, endsAt: {$lt: entry.start}})
.sort({channel: 1});

if (!availableChannels || !availableChannels.length) {
const channelNums = await db.schedule.count({});
Expand All @@ -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<IChannel>({
channel: newChannelNum,
channel: channelNum,
endsAt: entry.end,
});

await db.entries.update<IEntry>({_id: entry._id}, {$set: {channel: newChannelNum}});
} else {
channelNum = +availableChannels[0].channel;

await db.schedule.update<IChannel>({_id: availableChannels[0]._id}, {$set: {endsAt: entry.end}});
await db.entries.update<IEntry>({_id: entry._id}, {$set: {channel: availableChannels[0].channel}});
}

await db.entries.update<IEntry>({_id: entry._id}, {$set: {channel: channelNum}});
};

const scheduleLinearEntries = async (): Promise<void> => {
Expand All @@ -44,12 +49,18 @@ const scheduleLinearEntries = async (): Promise<void> => {
}

const channelNum = exisingLinearChannel.channel + START_CHANNEL;
const scheduleExists = await db.schedule.findOne<IChannel>({channel: channelNum});

if (scheduleExists) {
await db.schedule.update<IChannel>({_id: scheduleExists._id}, {$set: {endsAt: entry.end}});
} else {
await db.schedule.insert<IChannel>({
channel: channelNum,
endsAt: entry.end,
});
}

await db.entries.update<IEntry>({_id: entry._id}, {$set: {channel: channelNum}});
await db.schedule.insert<IChannel>({
channel: channelNum,
endsAt: entry.end,
});
}
};

Expand All @@ -68,8 +79,8 @@ export const scheduleEntries = async (firstRun = true): Promise<void> => {
{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(
{
Expand All @@ -84,6 +95,34 @@ export const scheduleEntries = async (firstRun = true): Promise<void> => {
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;
}
}
}

Expand Down Expand Up @@ -130,27 +169,23 @@ export const scheduleEntries = async (firstRun = true): Promise<void> => {
}
}

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<IEntry>({}, {$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<IEntry>({}, {$unset: {channel: true}}, {multi: true});
await db.schedule.remove({}, {multi: true});

return await scheduleEntries();
}

if (useLinear) {
return await scheduleLinearEntries();
}
};

0 comments on commit a5a3b2e

Please sign in to comment.