Created
December 15, 2014 22:47
-
-
Save mhafalir/274be867484d80580c9f to your computer and use it in GitHub Desktop.
YoutubePlaylistsBackup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function backupYoutubeListsV2(){ | |
| Logger.log('Processing your request: backupYoutubeListsV2'); | |
| var driveFile, docFile, lastBackupJson; | |
| var driveFiles = Drive.Files.list().items; | |
| for(var i = 0; i < driveFiles.length; i++){ | |
| var title = driveFiles[i].title; | |
| if(title == "YoutubeListsBackup"){ | |
| driveFile = driveFiles[i]; | |
| break; | |
| } | |
| } | |
| if(driveFile){ | |
| docFile = DocumentApp.openById(driveFile.id); | |
| lastBackupJson = JSON.parse(docFile.getBody().getText()); | |
| } | |
| else{ | |
| docFile = DocumentApp.create("YoutubeListsBackup"); | |
| } | |
| var currentBackupJson = { | |
| lastUpdateDate: new Date(), | |
| playlists: [] | |
| }; | |
| var results = YouTube.Channels.list('contentDetails', { mine: true }); | |
| var channel = results.items[0].contentDetails; | |
| currentBackupJson.playlists.push(FetchPlaylistItems(channel.relatedPlaylists.favorites, "Favorites")); | |
| currentBackupJson.playlists.push(FetchPlaylistItems(channel.relatedPlaylists.likes, "Likes")); | |
| currentBackupJson.playlists.push(FetchPlaylistItems(channel.relatedPlaylists.uploads, "Uploads")); | |
| currentBackupJson.playlists.push(FetchPlaylistItems(channel.relatedPlaylists.watchHistory, "WatchHistory")); | |
| currentBackupJson.playlists.push(FetchPlaylistItems(channel.relatedPlaylists.watchLater, "WatchLater")); | |
| var playlists = YouTube.Playlists.list("snippet", {mine: true}); | |
| for(var i = 0; i < playlists.items.length; i++){ | |
| if(playlists.items[i].id != channel.relatedPlaylists.favorites){ | |
| currentBackupJson.playlists.push(FetchPlaylistItems(playlists.items[i].id, playlists.items[i].snippet.title)); | |
| } | |
| } | |
| if(lastBackupJson){ | |
| currentBackupJson = MergeBackups(lastBackupJson, currentBackupJson); | |
| } | |
| docFile.getBody().setText(JSON.stringify(currentBackupJson)); | |
| docFile.saveAndClose(); | |
| Logger.log('Request processed: backupYoutubeListsV2'); | |
| } | |
| function FetchPlaylistItems(playlistId, playlistName){ | |
| var items = []; | |
| var nextPageToken; | |
| do { | |
| var playlistResponse = YouTube.PlaylistItems.list('snippet', { | |
| playlistId: playlistId, | |
| maxResults: 50, | |
| pageToken: nextPageToken | |
| }); | |
| for (var j = 0; j < playlistResponse.items.length; j++) { | |
| var playlistItem = playlistResponse.items[j]; | |
| items.push({id: playlistItem.snippet.resourceId.videoId.toString(), title: playlistItem.snippet.title}); | |
| } | |
| nextPageToken = playlistResponse.nextPageToken; | |
| } while (nextPageToken != null); | |
| return {id: playlistId, name: playlistName, items:items}; | |
| } | |
| function MergeBackups(lastBackupJson, currentBackupJson){ | |
| var deletedVideos = []; | |
| var privateVideos = []; | |
| var notificationText = ""; | |
| for(var i = 0; i < currentBackupJson.playlists.length; i++){ | |
| var currentBackupPlaylist = currentBackupJson.playlists[i]; | |
| var lastBackupPlaylist; | |
| for(var j = 0; j < lastBackupJson.playlists.length; j++){ | |
| if(lastBackupJson.playlists[j].id == currentBackupPlaylist.id){ | |
| lastBackupPlaylist = lastBackupJson.playlists[j]; | |
| break; | |
| } | |
| } | |
| if(lastBackupPlaylist){ | |
| for(var k = 0; k < currentBackupPlaylist.items.length; k++){ | |
| var currentBackupItem = currentBackupPlaylist.items[k]; | |
| if(currentBackupItem.title == "Deleted video"){ | |
| deletedVideos.push(currentBackupItem); | |
| } | |
| if(currentBackupItem.title == "Private video"){ | |
| privateVideos.push(currentBackupItem); | |
| } | |
| var lastBackupItem; | |
| for(var l = 0; l < lastBackupPlaylist.items.length; l++){ | |
| if(lastBackupPlaylist.items[l].id == currentBackupItem.id){ | |
| lastBackupItem = lastBackupPlaylist.items[l]; | |
| if(currentBackupItem.title != lastBackupItem.title){ | |
| notificationText += "Playlist: " + currentBackupPlaylist.name + ". Name: " + lastBackupItem.title + " > " + currentBackupItem.title + ". [http://www.youtube.com/watch?v=" + currentBackupItem.id + "]<br />"; | |
| } | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| if(notificationText.length == 0){ | |
| notificationText = "Nothing Has Changed"; | |
| } | |
| if(deletedVideos.length > 0){ | |
| notificationText += "<br /><br />These videos are seems to be deleted:"; | |
| for(var i = 0; i < deletedVideos.length; i++){ | |
| notificationText += "<br />Name: " + deletedVideos[i].title + ". [http://www.youtube.com/watch?v=" + deletedVideos[i].id + "]"; | |
| } | |
| } | |
| if(privateVideos.length > 0){ | |
| notificationText += "<br /><br />These videos are seems to be private:"; | |
| for(var i = 0; i < privateVideos.length; i++){ | |
| notificationText += "<br />Name: " + privateVideos[i].title + ". [http://www.youtube.com/watch?v=" + privateVideos[i].id + "]"; | |
| } | |
| } | |
| GmailApp.sendEmail(Session.getActiveUser().getEmail(), "Youtube Playlist Changes", notificationText, {htmlBody: notificationText}); | |
| return currentBackupJson; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment