Skip to content

Commit

Permalink
Prevent fixing database missing videos costing daily API quota
Browse files Browse the repository at this point in the history
  • Loading branch information
NikkelM committed May 25, 2024
1 parent b7e7811 commit 02615c1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

<!--Releasenotes start-->
- Cleaned up the popup and moved some settings to a separate menu.
- Fixed a bug where a correction of database corruption would reduce the user's daily API quota.
<!--Releasenotes end-->

## v3.1.2
Expand Down
21 changes: 11 additions & 10 deletions src/shuffleVideo.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,14 @@ async function uploadPlaylistToDatabase(playlistInfo, videosToDatabase, uploadsP
}

// ---------- YouTube API ----------
async function getPlaylistFromAPI(playlistId, useAPIKeyAtIndex, userQuotaRemainingToday, progressTextElement) {
async function getPlaylistFromAPI(playlistId, useAPIKeyAtIndex, userQuotaRemainingToday, progressTextElement, disregardUserQuota = false) {
// Get an API key
let { APIKey, isCustomKey, keyIndex } = await getAPIKey(useAPIKeyAtIndex);
// We need to keep track of the original key's index, so we know when we have tried all keys
const originalKeyIndex = keyIndex;

// If the user does not use a custom API key and has no quota remaining, we cannot continue
if (!isCustomKey && userQuotaRemainingToday <= 0) {
if (!isCustomKey && userQuotaRemainingToday <= 0 && !disregardUserQuota) {
throw new RandomYoutubeVideoError(
{
code: "RYV-4A",
Expand All @@ -314,11 +314,12 @@ async function getPlaylistFromAPI(playlistId, useAPIKeyAtIndex, userQuotaRemaini
let pageToken = "";
let apiResponse;

({ apiResponse, APIKey, isCustomKey, keyIndex, userQuotaRemainingToday } = await getPlaylistSnippetFromAPI(playlistId, pageToken, APIKey, isCustomKey, keyIndex, originalKeyIndex, userQuotaRemainingToday));
({ apiResponse, APIKey, isCustomKey, keyIndex, userQuotaRemainingToday } = await getPlaylistSnippetFromAPI(playlistId, pageToken, APIKey, isCustomKey, keyIndex, originalKeyIndex, userQuotaRemainingToday, disregardUserQuota));

// If there are more results we need to fetch than the user has quota remaining (+leeway) and the user is not using a custom API key, we need to throw an error
// If we would normally disregard the user quota (e.g. if we need to refetch all videos due to the database missing videos), but there are too many uploads, we need to protect the userbase and restrict the operation as well
const totalResults = apiResponse["pageInfo"]["totalResults"];
if (totalResults / 50 >= userQuotaRemainingToday + 50 && !isCustomKey) {
if (totalResults / 50 >= userQuotaRemainingToday + 50 && !isCustomKey && (!disregardUserQuota || totalResults >= 5000)) {
throw new RandomYoutubeVideoError(
{
code: "RYV-4B",
Expand Down Expand Up @@ -353,7 +354,7 @@ async function getPlaylistFromAPI(playlistId, useAPIKeyAtIndex, userQuotaRemaini
pageToken = apiResponse["nextPageToken"] ? apiResponse["nextPageToken"] : null;

while (pageToken !== null) {
({ apiResponse, APIKey, isCustomKey, keyIndex, userQuotaRemainingToday } = await getPlaylistSnippetFromAPI(playlistId, pageToken, APIKey, isCustomKey, keyIndex, originalKeyIndex, userQuotaRemainingToday));
({ apiResponse, APIKey, isCustomKey, keyIndex, userQuotaRemainingToday } = await getPlaylistSnippetFromAPI(playlistId, pageToken, APIKey, isCustomKey, keyIndex, originalKeyIndex, userQuotaRemainingToday, disregardUserQuota));

// Set the current progress as text for the shuffle button/info text
// We never get to this code part if there are less than or exactly 50 videos in the playlist, so we don't need to check for that
Expand Down Expand Up @@ -437,7 +438,7 @@ async function updatePlaylistFromAPI(playlistInfo, playlistId, useAPIKeyAtIndex,
// Make sure that we are not missing any videos in the database
if (totalNumVideosOnChannel > numLocallyKnownVideos) {
console.log(`There are less videos saved in the database than are uploaded on the channel (${numLocallyKnownVideos}/${totalNumVideosOnChannel}), so some videos are missing. Refetching all videos...`);
return await getPlaylistFromAPI(playlistId, keyIndex, userQuotaRemainingToday, progressTextElement);
return await getPlaylistFromAPI(playlistId, keyIndex, userQuotaRemainingToday, progressTextElement, true);
}

return { playlistInfo, userQuotaRemainingToday };
Expand Down Expand Up @@ -484,14 +485,14 @@ async function updatePlaylistFromAPI(playlistInfo, playlistId, useAPIKeyAtIndex,
const numVideosInDatabase = numLocallyKnownVideos + getLength(playlistInfo["newVideos"]);
if (totalNumVideosOnChannel > numVideosInDatabase) {
console.log(`There are less videos saved in the database than are uploaded on the channel (${numVideosInDatabase}/${totalNumVideosOnChannel}), so some videos are missing. Refetching all videos...`);
return await getPlaylistFromAPI(playlistId, keyIndex, userQuotaRemainingToday, progressTextElement);
return await getPlaylistFromAPI(playlistId, keyIndex, userQuotaRemainingToday, progressTextElement, true);
}

return { playlistInfo, userQuotaRemainingToday };
}

// Send a request to the Youtube API to get a snippet of a playlist
async function getPlaylistSnippetFromAPI(playlistId, pageToken, APIKey, isCustomKey, keyIndex, originalKeyIndex, userQuotaRemainingToday) {
async function getPlaylistSnippetFromAPI(playlistId, pageToken, APIKey, isCustomKey, keyIndex, originalKeyIndex, userQuotaRemainingToday, disregardUserQuota = false) {
const originalUserQuotaRemainingToday = userQuotaRemainingToday;
let apiResponse;

Expand Down Expand Up @@ -519,7 +520,7 @@ async function getPlaylistSnippetFromAPI(playlistId, pageToken, APIKey, isCustom
// We allow users to go beyond the daily limit in case there are only a few more videos to be fetched.
// But if it goes too far, we need to cancel the operation.
userQuotaRemainingToday--;
if (userQuotaRemainingToday <= -50) {
if (userQuotaRemainingToday <= -50 && !isCustomKey && !disregardUserQuota) {
throw new RandomYoutubeVideoError(
{
code: "RYV-4B",
Expand Down Expand Up @@ -586,7 +587,7 @@ async function getPlaylistSnippetFromAPI(playlistId, pageToken, APIKey, isCustom
}

// If the user is using a custom key, we do not want to update the quota
userQuotaRemainingToday = isCustomKey ? originalUserQuotaRemainingToday : userQuotaRemainingToday;
userQuotaRemainingToday = (isCustomKey || disregardUserQuota) ? originalUserQuotaRemainingToday : userQuotaRemainingToday;

return { apiResponse, APIKey, isCustomKey, keyIndex, userQuotaRemainingToday };
}
Expand Down

0 comments on commit 02615c1

Please sign in to comment.