Skip to content

Commit

Permalink
Fixed bug where no videos are pushed to database (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
NikkelM authored Aug 4, 2023
1 parent 345ae8f commit a125486
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
# Changelog

## v2.2.1
## v2.2.2

<!--Releasenotes start-->
- Fixed a bug where there would be no video IDs stored in the database, leading to an error.
- Fixed the 'Changelog' button on the changelog page not doing anything.
<!--Releasenotes end-->

## v2.2.1

- Fixed a bug where the extension would be unable to determine if a non-embeddable video is a short or not.
- The extension now handles the case where shorts are ignored, but a channel has only uploaded shorts.
- Fixed some minor bugs regarding version compatibility.
<!--Releasenotes end-->

## v2.2.0

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "random-youtube-video",
"version": "2.2.1",
"version": "2.2.2",
"description": "Play a random video uploaded on the current YouTube channel.",
"scripts": {
"dev": "concurrently \"npm run dev:chromium\" \"npm run dev:firefox\"",
Expand Down
20 changes: 16 additions & 4 deletions src/shuffleVideo.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ export async function chooseRandomVideo(channelId, firedFromPopup, progressTextE
} else {
// Otherwise, we want to only upload new videos. If there are no "newVideos", we upload all videos, as this is the first time we are uploading the playlist
console.log("Uploading new video IDs to the database...");
videosToDatabase = playlistInfo["newVideos"] ?? playlistInfo["videos"] ?? {};
if(getLength(playlistInfo["newVideos"] ?? {}) > 0) {
videosToDatabase = playlistInfo["newVideos"];
} else {
videosToDatabase = playlistInfo["videos"] ?? 0;
}
}

await uploadPlaylistToDatabase(playlistInfo, videosToDatabase, uploadsPlaylistId, encounteredDeletedVideos);
Expand Down Expand Up @@ -203,6 +207,14 @@ async function tryGetPlaylistFromDB(playlistId) {
return {};
}

if (!playlistInfo["videos"]) {
// Due to some mistake, there is no video data in the database
// Overwrite the playlist with an empty one
console.log("The playlist was found in the database, but it is empty. Removing...");
await uploadPlaylistToDatabase({}, {}, playlistId, true);
return {};
}

playlistInfo["lastFetchedFromDB"] = new Date().toISOString();

return playlistInfo;
Expand All @@ -212,7 +224,7 @@ async function tryGetPlaylistFromDB(playlistId) {
async function uploadPlaylistToDatabase(playlistInfo, videosToDatabase, uploadsPlaylistId, encounteredDeletedVideos) {
// Only upload the wanted keys
const playlistInfoForDatabase = {
"lastUpdatedDBAt": playlistInfo["lastUpdatedDBAt"],
"lastUpdatedDBAt": playlistInfo["lastUpdatedDBAt"] ?? new Date().toISOString(),
"lastVideoPublishedAt": playlistInfo["lastVideoPublishedAt"] ?? new Date(0).toISOString(),
"videos": videosToDatabase
};
Expand Down Expand Up @@ -408,7 +420,7 @@ async function updatePlaylistFromAPI(playlistInfo, playlistId, useAPIKeyAtIndex,
playlistInfo["newVideos"] = newVideos;

// Make sure that we are not missing any videos in the database
const numVideosInDatabase = numLocallyKnownVideos + (getLength(playlistInfo["newVideos"] ?? {}));
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);
Expand Down Expand Up @@ -636,7 +648,7 @@ async function chooseRandomVideosFromPlaylist(playlistInfo, channelId, shouldUpd
}

// Sort all videos by date
let allVideos = Object.assign({}, playlistInfo["videos"], playlistInfo["newVideos"]);
let allVideos = Object.assign({}, playlistInfo["videos"], playlistInfo["newVideos"] ?? {});

let videosByDate = Object.keys(allVideos).sort((a, b) => {
return new Date(allVideos[b]) - new Date(allVideos[a]);
Expand Down
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function isEmpty(obj) {

// Gets the length of an object
export function getLength(obj) {
return Object.keys(obj).length;
return Object.keys(obj ?? {}).length;
}

// Adds a number of hours to a date
Expand Down
2 changes: 1 addition & 1 deletion static/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Random YouTube Video",
"description": "Play a random video uploaded on the current YouTube channel.",
"version": "2.2.1",
"version": "2.2.2",
"manifest_version": 3,
"content_scripts": [
{
Expand Down
8 changes: 8 additions & 0 deletions test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ describe('utils.js', function () {
});

context('getLength()', function () {
it('should return 0 for undefined', function () {
expect(getLength(undefined)).to.be(0);
});

it('should return 0 for null', function () {
expect(getLength(null)).to.be(0);
});

it('should return 0 for empty objects', function () {
expect(getLength({})).to.be(0);
});
Expand Down

0 comments on commit a125486

Please sign in to comment.