Holen Sie sich alle YouTube-Videos in einer Playlist mit Angular JS

Ich möchte alle YouTube-Videos in einer angegebenen Wiedergabeliste erhalten. Mit der API können Sie derzeit 50 Datensätze gleichzeitig abrufen. Sie können jedoch die nächsten Datensätze abrufen, indem Sie ein Token für die nächste Seite übergeben.

Ich bin noch neu in Angular und habe Probleme, diese Aufgabe zu erfüllen.

Hier ist was ich aktuell habe:

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;
                    });
                });
            }
        }]);

Was muss ich mit der Funktion getPlaylistVideos tun, um alle Videos in einer Wiedergabeliste zurückzugeben? Anstatt nur 50.

Dies ist der Code, den ich vorher hatte, als ich gerade die erste Seite zurückgegeben habe:

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

Eine Beispiel-Playlist-ID ist PLFPg_IUxqnZNTAbUMEZ76_snWd-ED5en7.

Jede Hilfe wird sehr geschätzt!

Geänderte Antwort
$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);
            }
        });
    });
}

Antworten auf die Frage(2)

Ihre Antwort auf die Frage