|
| 1 | +// Define some variables used to remember state. |
| 2 | +var playlistId, nextPageToken, prevPageToken; |
| 3 | + |
| 4 | +// After the API loads, call a function to get the uploads playlist ID. |
| 5 | +function handleAPILoaded() { |
| 6 | + requestUserUploadsPlaylistId(); |
| 7 | +} |
| 8 | + |
| 9 | +// Call the Data API to retrieve the playlist ID that uniquely identifies the |
| 10 | +// list of videos uploaded to the currently authenticated user's channel. |
| 11 | +function requestUserUploadsPlaylistId() { |
| 12 | + // See https://developers.google.com/youtube/v3/docs/channels/list |
| 13 | + var request = gapi.client.youtube.channels.list({ |
| 14 | + mine: true, |
| 15 | + part: 'contentDetails' |
| 16 | + }); |
| 17 | + request.execute(function(response) { |
| 18 | + playlistId = response.result.items[0].contentDetails.relatedPlaylists.uploads; |
| 19 | + requestVideoPlaylist(playlistId); |
| 20 | + }); |
| 21 | +} |
| 22 | + |
| 23 | +// Retrieve the list of videos in the specified playlist. |
| 24 | +function requestVideoPlaylist(playlistId, pageToken) { |
| 25 | + $('#video-container').html(''); |
| 26 | + var requestOptions = { |
| 27 | + playlistId: playlistId, |
| 28 | + part: 'snippet', |
| 29 | + maxResults: 10 |
| 30 | + }; |
| 31 | + if (pageToken) { |
| 32 | + requestOptions.pageToken = pageToken; |
| 33 | + } |
| 34 | + var request = gapi.client.youtube.playlistItems.list(requestOptions); |
| 35 | + request.execute(function(response) { |
| 36 | + // Only show pagination buttons if there is a pagination token for the |
| 37 | + // next or previous page of results. |
| 38 | + nextPageToken = response.result.nextPageToken; |
| 39 | + var nextVis = nextPageToken ? 'visible' : 'hidden'; |
| 40 | + $('#next-button').css('visibility', nextVis); |
| 41 | + prevPageToken = response.result.prevPageToken |
| 42 | + var prevVis = prevPageToken ? 'visible' : 'hidden'; |
| 43 | + $('#prev-button').css('visibility', prevVis); |
| 44 | + |
| 45 | + var playlistItems = response.result.items; |
| 46 | + if (playlistItems) { |
| 47 | + $.each(playlistItems, function(index, item) { |
| 48 | + displayResult(item.snippet); |
| 49 | + }); |
| 50 | + } else { |
| 51 | + $('#video-container').html('Sorry you have no uploaded videos'); |
| 52 | + } |
| 53 | + }); |
| 54 | +} |
| 55 | + |
| 56 | +// Create a listing for a video. |
| 57 | +function displayResult(videoSnippet) { |
| 58 | + var title = videoSnippet.title; |
| 59 | + var videoId = videoSnippet.resourceId.videoId; |
| 60 | + $('#video-container').append('<p>' + title + ' - ' + videoId + '</p>'); |
| 61 | +} |
| 62 | + |
| 63 | +// Retrieve the next page of videos in the playlist. |
| 64 | +function nextPage() { |
| 65 | + requestVideoPlaylist(playlistId, nextPageToken); |
| 66 | +} |
| 67 | + |
| 68 | +// Retrieve the previous page of videos in the playlist. |
| 69 | +function previousPage() { |
| 70 | + requestVideoPlaylist(playlistId, prevPageToken); |
| 71 | +} |
0 commit comments