Last active
June 25, 2020 14:56
-
-
Save mandrasch/4449846e21ed657f05f45673fd4d0a29 to your computer and use it in GitHub Desktop.
Copy YouTube channel video to Drive Spreadsheet (Google Apps Script), spreadsheet template: https://docs.google.com/spreadsheets/d/1LvF0iqeQQyoUc_NjiIYjTvYGtIMWIXFlTHUGhhYXZ5g/edit#gid=0
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
/** | |
* Original Source: https://developers.google.com/youtube/v3/code_samples/apps-script | |
* Custom modifications by Matthias Andrasch, CC0 https://creativecommons.org/publicdomain/zero/1.0/deed.de | |
* Button solution via: https://www.benlcollins.com/apps-script/google-sheets-button/ | |
* | |
* Get URL, title, thumbnail from playlist or channel (via YouTube Data API) | |
* YouTube API data needs to be enabled in "resources" -> "Advanced google services" | |
*/ | |
function retrieveChannelOrPlaylistVideos() { | |
// https://stackoverflow.com/questions/45879224/yes-no-user-prompt-to-continue-or-stop-the-script | |
var ui = SpreadsheetApp.getUi(); | |
var response = ui.alert('Soll das Skript "YouTube2Spreadsheet" mit den angegebenen Daten (Zeile 1 und 2) ausgeführt werden?', ui.ButtonSet.YES_NO); | |
// Process the user's response. | |
if (response == ui.Button.NO) { | |
Logger.log('The user clicked "NO" or the close button in the dialog\'s bar'); | |
} else { | |
Logger.log('The user clicked "YES", start script'); | |
var sheet = SpreadsheetApp.getActiveSpreadsheet(); | |
//var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName); | |
var channelOrPlaylistFromSpreadsheet = sheet.getRange('B1').getValue(); | |
var idFromSpreadsheet = sheet.getRange('B2').getValue(); | |
if(channelOrPlaylistFromSpreadsheet == "channel"){ | |
// we get all uploaded videos for a channel with id: | |
var results = YouTube.Channels.list('contentDetails', {id: idFromSpreadsheet}); | |
// Get the playlist ID, which is nested in contentDetails, as described in the | |
// Channel resource: https://developers.google.com/youtube/v3/docs/channels | |
var playlistId = results.items[0].contentDetails.relatedPlaylists.uploads; | |
Logger.log("PlaylistId is, determined via channelId (API): "+playlistId); | |
} | |
else{ | |
var playlistId = idFromSpreadsheet; | |
Logger.log("PlaylistId set by spreadsheet: "+playlistId); | |
} | |
// get placeholder/default values from row 6 | |
var defaultCollection = sheet.getRange('C6').getValue(); | |
var defaultType = sheet.getRange('D6').getValue(); | |
var defaultLearningResourceType = sheet.getRange('H6').getValue(); | |
var defaultDiscipline = sheet.getRange('I6').getValue(); | |
var defaultEducationalContext = sheet.getRange('J6').getValue(); | |
var defaultLicense = sheet.getRange('K6').getValue(); | |
var defaultIntendedEndUserRole = sheet.getRange('L6').getValue(); | |
var defaultTypicalAgeRangeFrom = sheet.getRange('M6').getValue(); | |
var defaultTypicalAgeRangeTo = sheet.getRange('N6').getValue(); | |
var defaultLanguage = sheet.getRange('O6').getValue(); | |
var defaultKeyword = sheet.getRange('P6').getValue(); | |
var nextPageToken = ''; | |
// This loop retrieves a set of playlist items and checks the nextPageToken in the | |
// response to determine whether the list contains additional items. It repeats that process | |
// until it has retrieved all of the items in the list. | |
while (nextPageToken != null) { | |
var playlistResponse = YouTube.PlaylistItems.list('snippet', { | |
playlistId: playlistId, | |
maxResults: 25, | |
pageToken: nextPageToken | |
}); | |
for (var j = 0; j < playlistResponse.items.length; j++) { | |
var playlistItem = playlistResponse.items[j]; | |
//Logger.log(playlistItem); | |
Logger.log('[%s] Title: %s', | |
playlistItem.snippet.resourceId.videoId, | |
playlistItem.snippet.title); | |
// https://mashe.hawksey.info/2018/02/google-apps-script-patterns-writing-rows-of-data-to-google-sheets/ | |
//var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
// The code below logs the index of a sheet named "Expenses" | |
if (sheet == null) { | |
Logger.log("Error: sheet name not found"); | |
} | |
sheet.appendRow([ | |
// title | |
playlistItem.snippet.title, | |
// uuid | |
"", | |
// collection | |
defaultCollection, | |
// type | |
defaultType, | |
// description | |
playlistItem.snippet.description, | |
// url | |
"https://youtube.com/watch?v="+ playlistItem.snippet.resourceId.videoId, | |
// thumbnail | |
playlistItem.snippet.thumbnails.high.url, | |
// learningResourceType | |
defaultLearningResourceType, | |
// discipline | |
defaultDiscipline, | |
// educationalContext | |
defaultEducationalContext, | |
// license | |
defaultLicense, | |
// intendedEndUserRole | |
defaultIntendedEndUserRole, | |
// typicalAgeRangeFrom | |
defaultTypicalAgeRangeFrom, | |
// typicalAgeRangeTo | |
defaultTypicalAgeRangeTo, | |
// language | |
defaultLanguage, | |
// keyword | |
defaultKeyword | |
]); | |
} | |
// use this for dev to only test first items: | |
// nextPageToken = null; | |
nextPageToken = playlistResponse.nextPageToken; | |
} // eo while | |
} // eo else | |
} | |
function clearCrawledData(){ | |
// https://stackoverflow.com/questions/45879224/yes-no-user-prompt-to-continue-or-stop-the-script | |
var ui = SpreadsheetApp.getUi(); | |
var response = ui.alert('Sollen alle eingelesenen Zeilen gelöscht werden?', ui.ButtonSet.YES_NO); | |
// Process the user's response. | |
if (response == ui.Button.NO) { | |
Logger.log('The user clicked "NO" or the close button in the dialog\'s bar'); | |
} else { | |
Logger.log('The user clicked "YES", start script'); | |
var sheet = SpreadsheetApp.getActiveSpreadsheet(); | |
var range = sheet.getRange("A7:P1000"); | |
range.clearContent(); | |
} // eo else | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment