diff --git a/src/utils/media/yanker.js b/src/utils/media/yanker.js index 76df22e..2dff869 100644 --- a/src/utils/media/yanker.js +++ b/src/utils/media/yanker.js @@ -67,28 +67,41 @@ module.exports.yankMedia = async function(url, title){ module.exports.refreshRawLink = async function(mediaObj){ switch(mediaObj.type){ case 'yt': - /* We're skipping this one for now... - //Scrape expiration from query strings - const expires = mediaObj.rawLink.match(/expire=([0-9]+)/); - //Went with regex for speed, but I figure I'd keep this around in case we want the accuracy of a battle-tested implementation - //const expires = new URL(mediaObj.rawLink).searchParams.get("expire"); + //Create boolean to hold expired state + let expired = false; + //Create boolean to hold whether or not rawLink object is empty + let empty = true; - //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; + //For each link map in the rawLink object + for(const key of Object.keys(mediaObj.rawLink)){ + //Ignore da wombo-combo since it's probably just the fuckin regular URL + if(key != "combo"){ + for(const link of mediaObj.rawLink[key]){ + //Let it be known, this bitch got links + empty = false; + //Get expiration parameter from the link + const expires = new URL(link[1]).searchParams.get("expire") * 1000; + + //If this shit's already expired + if(expires < Date.now()){ + //Set expired to true, don't directly set the bool because we don't ever want to unset this flag + expired = true; + } + } + } } - */ + //If the raw link object is empty or expired + if(empty || expired){ + //Re-fetch media metadata + metadata = await ytdlpUtil.fetchYoutubeMetadata(mediaObj.id); + + //Refresh media rawlink from metadata + mediaObj.rawLink = metadata[0].rawLink; - //Re-fetch media metadata - metadata = await ytdlpUtil.fetchYoutubeMetadata(mediaObj.id); - - //Refresh media rawlink from metadata - mediaObj.rawLink = metadata[0].rawLink; - - //return media object - return mediaObj; + //return media object + return mediaObj; + } } //Return null to tell the calling function there is no refresh required for this media type