Stack Overflow Asked by thePOPstream on September 24, 2020
I have the following code that can add videos to a playlist from google sheets using video IDs.
function updateYTPlaylist() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet1');
const yt_video_ids = sh.getRange('A2:A'+sh.getLastRow()).getValues().flat([1]);
const playlistId = "PL7Ias5xZGXQ9o60ezsHZhl8NkoSm7Tdvj";
yt_video_ids.forEach( vid =>
YouTube.PlaylistItems.insert({
snippet: {
playlistId: playlistId,
resourceId: {
kind: "youtube#video",
videoId: vid
}
}
}, "snippet"));
Utilities.sleep(2000);
}
I would also like to remove the videos that were there the previous day before I add the new ones but am unsure how to go about this.
YouTube API does not provide information regarding when you added a particular video to the playlist. So we can't just delete the videos of the previous day. What I would suggest is to create a new playlist and everyday you will run a function (via a time basis trigger) to remove the current videos (that added the previous day) and then add new ones. Therefore, the first part of the script will clear your playlist and the second one will add new videos to it.
I added the following code snippet to your code that removes every single video from your playlist; or in other words it clears your playlist:
const playlistResponse = YouTube.PlaylistItems.list('snippet', {playlistId: playlistId});
const playlistItem = playlistResponse.items;
playlistItem.forEach(dvid =>
{YouTube.PlaylistItems.remove(dvid.id);
Utilities.sleep(2000);}
);
Therefore, the full code that clears a playlist and then adds youtube videos based on A column of Sheet1 is the following:
function updateYTPlaylist() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet1');
const yt_video_ids = sh.getRange('A2:A'+sh.getLastRow()).getValues().flat([1]);
const playlistId = "PL7Ias5xZGXQ9o60ezsHZhl8NkoSm7Tdvj";
const playlistResponse = YouTube.PlaylistItems.list('snippet', {playlistId: playlistId});
const playlistItem = playlistResponse.items;
playlistItem.forEach(dvid =>
{YouTube.PlaylistItems.remove(dvid.id);
Utilities.sleep(2000);}
);
yt_video_ids.forEach( avid =>
YouTube.PlaylistItems.insert({
snippet: {
playlistId: playlistId,
resourceId: {
kind: "youtube#video",
videoId: avid
}
}
}, "snippet"));
Utilities.sleep(2000);
}
From the google script editor you need to click on Resources => Advanced Google Services and then enable YouTube Data API v3 (as of now this is the newest version).
Correct answer by Marios on September 24, 2020
Here is the official documentation for the deletion of an item from a playlist. Unfortunately this http request only deletes one video from the playlist, so you will have to loop through all the id's of the videos the playlist has, deleting each one at a time.
Answered by yaexiste2 on September 24, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP