queue refreshes raw link for items starting in less than 10 seconds, rawRefresh only refreshes expired links.

This commit is contained in:
rainbow napkin 2025-05-06 22:45:26 -04:00
parent 60cd21d938
commit 2a3740dece
3 changed files with 36 additions and 12 deletions

View file

@ -37,6 +37,9 @@ module.exports = class{
this.syncDelta = 1000; this.syncDelta = 1000;
//Create variable to hold current timestamp within the video //Create variable to hold current timestamp within the video
this.timestamp = 0; this.timestamp = 0;
//Delay between pre-switch function call and start of media
//This should be enough time to do things like pre-fetch updated raw links from youtube
this.preSwitchDelta = 10 * 1000;
//Create variable to hold sync timer //Create variable to hold sync timer
this.syncTimer = null; this.syncTimer = null;
@ -300,16 +303,13 @@ module.exports = class{
const now = new Date().getTime(); const now = new Date().getTime();
//Calculate the amount of time in ms that the next item will start in //Calculate the amount of time in ms that the next item will start in
const startsIn = nextItem.startTime - now; const startsIn = nextItem.startTime - now;
//Delay between pre-switch function call and start of media
//This should be enough time to do things like pre-fetch updated raw links from youtube
const preSwitchDelta = 10 * 1000;
//Calculate when the pre-switch timer would be called //Calculate when the pre-switch timer would be called
const preSwitchTime = nextItem.startTime - preSwitchDelta; const preSwitchTime = nextItem.startTime - this.preSwitchDelta;
//Calculate how long the pre-switch timer will be called in //Calculate how long the pre-switch timer will be called in
const preSwitchIn = preSwitchTime - now; const preSwitchIn = preSwitchTime - now;
//If we have enough time to call the pre-switch timer //If we have enough time to call the pre-switch timer
if(preSwitchIn > preSwitchDelta){ if(preSwitchIn > this.preSwitchDelta){
//Set the pre-switch timer //Set the pre-switch timer
this.preSwitchTimer = setTimeout(()=>{this.preSwitch(nextItem)}, preSwitchIn); this.preSwitchTimer = setTimeout(()=>{this.preSwitch(nextItem)}, preSwitchIn);
} }
@ -551,8 +551,10 @@ module.exports = class{
*/ */
for(let mediaObj of media){ for(let mediaObj of media){
const now = new Date().getTime();
//If someone is trying to schedule something that starts and ends in the past //If someone is trying to schedule something that starts and ends in the past
if((mediaObj.getEndTime() < new Date().getTime()) && !force){ if((mediaObj.getEndTime() < now) && !force){
//If an originating socket was provided for this request //If an originating socket was provided for this request
if(socket != null){ if(socket != null){
//Yell at the user for being an asshole //Yell at the user for being an asshole
@ -562,9 +564,9 @@ module.exports = class{
} }
//If the item has already started //If the item has already started
if((mediaObj.startTime < new Date().getTime()) && !force){ if((mediaObj.startTime < now) && !force){
//Set time stamp to existing timestamp plus the difference between the orginal start-date and now //Set time stamp to existing timestamp plus the difference between the orginal start-date and now
const calculatedTimeStamp = mediaObj.startTimeStamp + ((new Date().getTime() - mediaObj.startTime) / 1000) const calculatedTimeStamp = mediaObj.startTimeStamp + ((now - mediaObj.startTime) / 1000)
//If the calculated time stamp is more than negligible, and therefore not simply caused by serverside processing time //If the calculated time stamp is more than negligible, and therefore not simply caused by serverside processing time
if(calculatedTimeStamp > 5){ if(calculatedTimeStamp > 5){
@ -572,7 +574,7 @@ module.exports = class{
mediaObj.startTimeStamp = calculatedTimeStamp; mediaObj.startTimeStamp = calculatedTimeStamp;
//Start the item now //Start the item now
mediaObj.startTime = new Date().getTime(); mediaObj.startTime = now;
} }
} }
@ -587,6 +589,12 @@ module.exports = class{
return false; return false;
} }
//If we start in less than 10 seconds
if((mediaObj.startTime - this.preSwitchDelta) < now){
//Asyncrhounosly Check if we need to refresh the raw link
this.handleRawRefresh(mediaObj);
}
//Create an empty temp array to sparsley populate with our schedule //Create an empty temp array to sparsley populate with our schedule
const tempSchedule = []; const tempSchedule = [];
//Create new map to replace our current schedule map //Create new map to replace our current schedule map
@ -674,6 +682,10 @@ module.exports = class{
} }
async preSwitch(mediaObj){ async preSwitch(mediaObj){
this.handleRawRefresh(mediaObj);
}
async handleRawRefresh(mediaObj){
//Check if media needs a new raw link and update if it does //Check if media needs a new raw link and update if it does
if(await yanker.refreshRawLink(mediaObj)){ if(await yanker.refreshRawLink(mediaObj)){
//If the fetch took so god damned long we've already started the video (isn't 10 seconds enough?) //If the fetch took so god damned long we've already started the video (isn't 10 seconds enough?)
@ -681,6 +693,9 @@ module.exports = class{
//Tell the clients to update the raw file for the current item fore.st-style, as it probably got sent out with a stale link //Tell the clients to update the raw file for the current item fore.st-style, as it probably got sent out with a stale link
this.server.io.in(this.channel.name).emit("updateCurrentRawFile", {file: mediaObj.rawLink}); this.server.io.in(this.channel.name).emit("updateCurrentRawFile", {file: mediaObj.rawLink});
} }
//Return media obj to tell of success
return mediaObj;
} }
} }
@ -989,10 +1004,9 @@ module.exports = class{
throw loggerUtils.exceptionSmith(`Unable to find channel document ${this.channel.name} while rehydrating queue!`, "queue"); throw loggerUtils.exceptionSmith(`Unable to find channel document ${this.channel.name} while rehydrating queue!`, "queue");
} }
//Get current time
const now = new Date().getTime(); const now = new Date().getTime();
//Next: Update this function to handle ended items
//If something was playing //If something was playing
if(chanDB.media.nowPlaying != null){ if(chanDB.media.nowPlaying != null){
//Rehydrate the currently playing item int oa queued media object //Rehydrate the currently playing item int oa queued media object

View file

@ -15,7 +15,6 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.*/ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
//Node Imports //Node Imports
const url = require("node:url");
const validator = require('validator'); const validator = require('validator');
//Local Imports //Local Imports

View file

@ -15,6 +15,7 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.*/ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
//NPM Imports //NPM Imports
const url = require("node:url");
const validator = require('validator');//No express here, so regular validator it is! const validator = require('validator');//No express here, so regular validator it is!
//local import //local import
@ -41,6 +42,16 @@ module.exports.yankMedia = async function(url, title){
module.exports.refreshRawLink = async function(mediaObj){ module.exports.refreshRawLink = async function(mediaObj){
switch(mediaObj.type){ switch(mediaObj.type){
case 'yt': case 'yt':
//Scrape expiration from query strings
//const expires = mediaObj.rawLink.match(/expire=([0-9]+)/); //Keeping this regex version I wrote at first in-case we need the speed
const expires = new URL(mediaObj.rawLink).searchParams.get("expire");
//If we have a valid raw file link that will be good by the end of the video
if(expires != null && (expires * 1000) > mediaObj.getEndTime()){
//Return null to tell the calling function there is no refresh required for this video at this time
return null;
}
//Re-fetch media metadata //Re-fetch media metadata
metadata = await ytdlpUtil.fetchYoutubeVideoMetadata(mediaObj.id); metadata = await ytdlpUtil.fetchYoutubeVideoMetadata(mediaObj.id);
//Refresh media rawlink from metadata //Refresh media rawlink from metadata