Get all YouTube Videos in a Playlist using Angular JS

Aaron

I want to get all of the YouTube videos in a specified playlist. The API currently allows you to pull 50 records at a time but you can pull the next set of records by passing a next page token.

I am still new to Angular and I am having trouble accomplishing this task.

Here is what I currently have:

var app = angular.module('ph', []);
        var key = '';

        app.factory('youtubeService', ['$http', function ($http) {

            function getPlaylists(channelId) {
                return $.get("https://www.googleapis.com/youtube/v3/playlists", { part: 'snippet', channelId: channelId, key: key, maxResults: 50 });
            }

            function getPlaylistVideos(playlistId) {

                var items = [];
                var nextPageToken = "";

                $.get('https://www.googleapis.com/youtube/v3/playlistItems', { part: 'snippet', maxResults: 50, playlistId: playlistId, key: key }).then(function (firstResponse) {

                    items = firstResponse.items;
                    var numberOfPages = Math.ceil(firstResponse.pageInfo.totalResults / 50);

                    for (var page = 1; page <= numberOfPages; page++) {
                        $.get('https://www.googleapis.com/youtube/v3/playlistItems', { part: 'snippet', maxResults: 50, pageToken: nextPageToken, playlistId: playlistId, key: key }).then(function(response) {
                            items = items.concat(response.items);
                            nextPageToken = response.nextPageToken;
                        });
                    }
                });

                return items;
            }

            function getVideos(keyword) {
                return $.get('https://www.googleapis.com/youtube/v3/search', { part: 'snippet', q: keyword, key: key, type: 'video' });
            }

            return { getPlaylists: getPlaylists, getPlaylistVideos: getPlaylistVideos, getVideos: getVideos }

        }]);


        app.controller('ctrl', ['$http', '$scope', 'youtubeService', function ($http, $scope, youtubeService) {

            youtubeService.getPlaylists('UC-9-kyTW8ZkZNDHQJ6FgpwQ').then(function (response) {
                $scope.$apply(function () {
                    $scope.playlists = response.items;
                });
            });

            $scope.getPlaylistVideos = function (selection) {
                youtubeService.getPlaylistVideos(selection.id).then(function (response) {
                    $scope.$apply(function () {
                        $scope.playlistvideos = response.items;
                    });
                });
            }

            $scope.getVideos = function (selection) {
                youtubeService.getVideos(selection).then(function (response) {
                    $scope.$apply(function () {
                        $scope.videos = response.items;
                    });
                });
            }
        }]);

What do I need to do to the getPlaylistVideos function to get it to return all of the videos in a playlist? Rather than just 50.

This is the code that I had before for just returning first page:

function getPlaylistVideos(playlistId) {
    return $.get('https://www.googleapis.com/youtube/v3/playlistItems', { part: 'snippet', maxResults: 50, playlistId: playlistId, key: key});
}

An example playlist id is PLFPg_IUxqnZNTAbUMEZ76_snWd-ED5en7.

Any help is much appreciated!

Modified Answer

$scope.getPlaylistVideos = function (selection) {
    // pass the nextPageToken attribute to the getPlaylistVideos method.
    youtubeService.getPlaylistVideos("PLFPg_IUxqnZNTAbUMEZ76_snWd-ED5en7", $scope.nextPageToken).then(function (response) {
        $scope.$apply(function () {
            angular.forEach(response.items, function (item) {
                $scope.playlistvideos.push(item);
            });

            if (typeof response.nextPageToken != 'undefined') {
                $scope.nextPageToken = response.nextPageToken;
                // call the get playlist function again
                $scope.getPlaylistVideos(selection);
            }
        });
    });
}
Sandeep Sukhija

You need to use the pageToken attribute to fetch the next page.

From the documentation, Use the nextPageToken attribute from the api response.Call the API one or more times to retrieve all items in the list. As long as the API response returns a nextPageToken, there are still more items to retrieve.

In your controller, define a local variable nextPageToken to retrieve the next page and call the getPlaylistVideos with the nextPageToken attribute recursively until all the results are retrieved.

app.controller('ctrl', ['$http', '$scope', 'youtubeService', function ($http, $scope, youtubeService) {

        // define a local variable pageToken, it is gonna be undefined for the first time and will be initialized with next page token after subsequent responses
        $scope.nextPageToken;
        $scope.playlistVideos = [];

        youtubeService.getPlaylists('UC-9-kyTW8ZkZNDHQJ6FgpwQ').then(function (response) {
            $scope.$apply(function () {
                $scope.playlists = response.items;
            });
        });

        $scope.getPlaylistVideos = function (selection) {
            // pass the nextPageToken attribute to the getPlaylistVideos method.
            youtubeService.getPlaylistVideos(selection.id, $scope.nextPageToken).then(function (response) {
                $scope.$apply(function () {
                    $scope.playlistVideos.push(response.items);

                    if(typeof response.nextPageToken != 'undefined'){
                          $scope.nextPageToken = response.nextPageToken;
                          // call the get playlist function again
                          $scope.getPlaylistVideos(selection);
                    } 
                });
            });
        }

        $scope.getVideos = function (selection) {
            youtubeService.getVideos(selection).then(function (response) {
                $scope.$apply(function () {
                    $scope.videos = response.items;
                });
            });
        }
}]);

In your service, pass the pageToken attribute to the api to fetch the next page.

function getPlaylistVideos(playlistId, pageToken) {
...
     // pass the page token as a parameter to the API
     $.get('https://www.googleapis.com/youtube/v3/playlistItems', { part: 'snippet', maxResults: 50, playlistId: playlistId, key: key, pageToken: pageToken })
...
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get all videos of a YouTube Playlist in Java

From Dev

Get YouTube playlist videos anonymously

From Dev

YouTube API - get tags for all videos with playlist query

From Dev

YouTube API - get tags for all videos with playlist query

From Dev

Youtube playlist all videos duration show in php

From Dev

Get Youtube publish channel playlist videos in android

From Dev

Retrieve all videos from youtube playlist using youtube v3 API

From Dev

Delete videos from playlist using YouTube API

From Dev

Youtube Data API - how to get all videos in a playlist rather than just 50?

From Dev

Background Playlist Youtube Videos

From Dev

Extract youtube playlist videos

From Dev

How Can I Get my playlist videos from Youtube with angular 2

From Dev

youtube api v3 - get videos from a public playlist. with out using api key

From Dev

YouTube API Playlists list does not give all videos in the playlist

From Dev

YouTube API Playlists list does not give all videos in the playlist

From Dev

How to get correct JSON format of my playlist videos in YouTube in Android?

From Dev

How can I return the number of videos in a YouTube playlist using javascript?

From Dev

Is it possible to get all videos of YouTube using its API?

From Dev

Retrieve videos from youtube playlist

From Dev

get newest additions to a youtube playlist using youtube-dl

From Dev

Getting all videos of a channel using youtube API

From Dev

Get reference to youtube playlist

From Dev

Get Youtube playlist by Title

From Dev

Get all youtube videos from a channel (some videos are missing)

From Dev

Youtube api not displaying playlist thumbnail for few videos

From Dev

how to build video playlist having youtube videos?

From Dev

Batch Delete Videos From YouTube Favorites Playlist

From Dev

Youtube api not displaying playlist thumbnail for few videos

From Dev

Get all video ID's from a YouTube playlist into a PHP array

Related Related

  1. 1

    Get all videos of a YouTube Playlist in Java

  2. 2

    Get YouTube playlist videos anonymously

  3. 3

    YouTube API - get tags for all videos with playlist query

  4. 4

    YouTube API - get tags for all videos with playlist query

  5. 5

    Youtube playlist all videos duration show in php

  6. 6

    Get Youtube publish channel playlist videos in android

  7. 7

    Retrieve all videos from youtube playlist using youtube v3 API

  8. 8

    Delete videos from playlist using YouTube API

  9. 9

    Youtube Data API - how to get all videos in a playlist rather than just 50?

  10. 10

    Background Playlist Youtube Videos

  11. 11

    Extract youtube playlist videos

  12. 12

    How Can I Get my playlist videos from Youtube with angular 2

  13. 13

    youtube api v3 - get videos from a public playlist. with out using api key

  14. 14

    YouTube API Playlists list does not give all videos in the playlist

  15. 15

    YouTube API Playlists list does not give all videos in the playlist

  16. 16

    How to get correct JSON format of my playlist videos in YouTube in Android?

  17. 17

    How can I return the number of videos in a YouTube playlist using javascript?

  18. 18

    Is it possible to get all videos of YouTube using its API?

  19. 19

    Retrieve videos from youtube playlist

  20. 20

    get newest additions to a youtube playlist using youtube-dl

  21. 21

    Getting all videos of a channel using youtube API

  22. 22

    Get reference to youtube playlist

  23. 23

    Get Youtube playlist by Title

  24. 24

    Get all youtube videos from a channel (some videos are missing)

  25. 25

    Youtube api not displaying playlist thumbnail for few videos

  26. 26

    how to build video playlist having youtube videos?

  27. 27

    Batch Delete Videos From YouTube Favorites Playlist

  28. 28

    Youtube api not displaying playlist thumbnail for few videos

  29. 29

    Get all video ID's from a YouTube playlist into a PHP array

HotTag

Archive