Skip to content

Instantly share code, notes, and snippets.

@TakamiChie
Created January 15, 2022 06:42
Show Gist options
  • Save TakamiChie/d6625d075c4164016b8df7d8b4758f13 to your computer and use it in GitHub Desktop.
Save TakamiChie/d6625d075c4164016b8df7d8b4758f13 to your computer and use it in GitHub Desktop.
YouTube playlist auto-configuration tool
/**
* # Usage
* 1. start a new Google Apps Script project and paste this code into the script area.
* 2. After saving, set the `main()` function to be executed automatically.
* # Operation
* 1. Create a playlist.
* 2. Set the title of the video you want to add to the playlist as "Playlist Name:Video Title"(Colons can be full-size or half-size).
*/
function main() {
// Get the necessary items (own channel object, playlist list, video list).
const channel = YouTube.Channels.list(["snippet"], {
"mine": true
})["items"][0];
const playlists = YouTube.Playlists.list(["snippet"], {
"mine": true
});
const videos = YouTube.Search.list(["snippet"], {
"channelId": channel.id,
"order": "date",
"maxResults": 50,
});
// video foreach
Array.from(videos.items).forEach((v, i) => {
Logger.log(`>> ${v.snippet.title}`);
// Category Extraction
const cn = v.snippet.title.split(/[::]/);
if (cn.length == 2 && cn[0] && cn[1]) {
// Does the category name exist as a playlist?
const pl = playlists.items.find(item => item.snippet.title == cn[0]);
if (pl) {
Logger.log(`Category:${cn[0]}`);
// Is the video already on the relevant playlist?
if(YouTube.PlaylistItems.list(["id"], {
"playlistId": pl.id,
"videoId": v.id.videoId
}).items.length == 0){
// Subscribe to the playlist!
Logger.log(`Since the video is not in the list, add it.`);
const res = {
snippet: {
playlistId: pl.id,
resourceId: {
videoId: v.id.videoId,
kind: 'youtube#video'
}
}
}
YouTube.PlaylistItems.insert(res, ["snippet"]);
Logger.log(`Success`);
}else{
Logger.log(`The video is already registered.`);
}
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment