【问题标题】:Get all YouTube Videos in a Playlist using Angular JS使用 Angular JS 获取播放列表中的所有 YouTube 视频
【发布时间】:2016-10-25 06:36:47
【问题描述】:

我想获取指定播放列表中的所有 YouTube 视频。 API 目前允许您一次提取 50 条记录,但您可以通过传递下一页令牌来提取下一组记录。

我还是 Angular 的新手,我无法完成这项任务。

这是我目前拥有的:

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

我需要对 getPlaylistVideos 函数做些什么才能让它返回播放列表中的所有视频?而不仅仅是 50 个。

这是我之前只返回首页的代码:

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

一个示例播放列表 ID 为 PLFPg_IUxqnZNTAbUMEZ76_snWd-ED5en7。

非常感谢任何帮助!

修改后的答案

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

【问题讨论】:

  • @SandeepSukhija 我看到了这个问题,但我正在寻求有关它的角度部分的帮助。我已经知道分页,我的代码正在尝试使用它。我只是不确定如何修改我的函数,以便我可以使用 Angular 使其工作
  • @Aaron 我知道这是一个很老的问题,但是您有上述更好的工作示例吗?还是它的实际示例?

标签: javascript angularjs youtube


【解决方案1】:

您需要使用pageToken 属性来获取下一页。

从文档中, 使用 api 响应中的 nextPageToken 属性。调用 API 一次或多次以检索 列表。只要 API 响应返回一个 nextPageToken, 还有更多项目要检索。

在您的控制器中,定义一个局部变量 nextPageToken 以检索下一页并递归调用带有 nextPageToken 属性的 getPlaylistVideos 直到检索到所有结果。

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

在您的服务中,将 pageToken 属性传递给 api 以获取下一页。

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 })
...
}

【讨论】:

  • 我已经尝试添加您的代码,但它似乎仍然不起作用。在 getPlaylistVideos 中它不应该返回一些东西吗?此外,递归调用是否应该在参数中包含下一页标记。这对你有用吗?
  • 已更新答案。函数getPlaylistVideos 不返回任何内容,它使用获取的播放列表项更新$scope.playlistVideos 变量。此外,函数getPlaylistVideos 使用范围变量nextPageToken 来获取下一页。
  • 因此必须将返回添加到 getPlaylistVideos 以及将项目推送到数组中,而不是将实际数组推送到播放列表视频。我已将我的更改添加到我的问题中。非常感谢您的帮助。
猜你喜欢
  • 2016-11-19
  • 1970-01-01
  • 1970-01-01
  • 2015-08-20
  • 2012-11-01
  • 2016-12-25
  • 2017-09-08
  • 1970-01-01
  • 2015-11-24
相关资源
最近更新 更多