【问题标题】:Youtube API v3 console.logging way too muchYoutube API v3 console.logging 太多了
【发布时间】:2015-09-01 11:59:12
【问题描述】:

我有一些代码应该得到channelinfo by name,然后是playlistId by channelinfo,然后是videoes by playlistId,最后是videodetails by videos

在大约 200 个视频中,调用相同的 YouTube API 500 次。

我的代码如下。

服务:

appApi.factory('ServiceAPI', ['$http', function($http) {
  var factory = {};

  factory.channelDetails = function(channelname, success, error){
    var promise = $http.get('https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername='+channelname+'&key=AIzaSyDQv-WpATIWLinCB3H_sH4W1sKx7plyvRA')
    if(success){
      promise.success(success);
    }
    if(error){
      promise.error(error);
    };
  }
  return factory;
}]);

appApi.factory('ServiceCHLnames', ['$http', function($http) {
  var factory = {};

  factory.channelnames = function(success, error){
    var promise = $http.get('http://localhost:8080/api/resources/channelNames')
    if(success){
      promise.success(success);
    }
    if(error){
      promise.error(error);
    };
  }
  return factory;
}]);

appApi.factory('ServiceVideos', ['$http', function($http) {
  var factory = {};

  factory.videos = function(playlistId, success, error){
    var promise = $http.get('https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=' + playlistId + '&key=AIzaSyDQv-WpATIWLinCB3H_sH4W1sKx7plyvRA')
    if(success){
      promise.success(success);
    }
    if(error){
      promise.error(error);
    };
  }
  return factory;
}]);

appApi.factory('ServiceVideoDtls', ['$http', function($http) {
  var factory = {};

  factory.videodetails = function(videoid, success, error){
    var promise = $http.get('https://www.googleapis.com/youtube/v3/videos?part=statistics&id=' + videoid + '&key=AIzaSyDQv-WpATIWLinCB3H_sH4W1sKx7plyvRA')
    if(success){
      promise.success(success);
      console.log("GOT ONE VIDEO DETAIL")
    }
    if(error){
      promise.error(error);
    };
  }
  return factory;
}]);

控制器:

var appApi = angular.module('YoutubeAPI', ['ngRoute'])

appApi.controller('youtubeCTRL', ['$scope','$http','$q','ServiceAPI','ServiceCHLnames','ServiceVideos','ServiceVideoDtls', function ($scope, $http, $q, ServiceAPI,ServiceCHLnames,ServiceVideos,ServiceVideoDtls) {
    $scope.channel = [];
    $scope.video = [];
    var playlistId = [];


    var pagetokenarr = [];

    //GET Id on channelname
    $scope.saveNewchlName = function () {

        var channelname = $scope.newchlName;

            ServiceAPI.channelDetails(channelname, function(data){

                $scope.newchannelNames = {
                    channelName: $scope.newchlName,
                    channelId: data.items[0].id,
                    playlistId: data.items[0].contentDetails.relatedPlaylists.uploads
                };
                console.log($scope.newchannelNames)
                $http({
                    method: 'POST',
                    url: 'http://localhost:8080/api/resources/channelNames/',
                    data: $scope.newchannelNames,
                    dataType: 'json'
                }).success(function (data) {
                    $scope.channel.push(data);
                    console.log('SUCCESS!');
                    $scope.error = null;
                }).error(function (data, status) {
                    if (status == 401) {
                        $scope.error = "You are not authenticated to Post these data";
                        return;
                    }
                    $scope.error = data;
                });


    });
}
    //Henter Details på alle videoer på PlaylistID fra save NewchlName
    $scope.GetDetailsOnChl = function () {
        var playlistId;

            ServiceCHLnames.channelnames(function(data){

                angular.forEach(data._embedded.channelNames, function (chlName) { // FOR EACH LOOP, LOOPER IGENNEM ALLE CHL NAMES OG FINDER PLAYLIST ID
                    playlistId = chlName.playlistId;
                    console.log("i forEach loop") // CONSOLE.LOGGING
                    console.log(playlistId)// CONSOLE.LOGGING

//                    if (pagetokenarr.length == 0) {

                        ServiceVideos.videos(playlistId, function(data){
                                angular.forEach(data.items, function (item) {
                                    var video = {
                                        id: item.snippet.resourceId.videoId,
                                        title: item.snippet.title,
                                        dateofupload: item.snippet.publishedAt
                                    };
                                    $scope.video.push(video);
//                                    console.log(video); // CONSOLE.LOGGING
//
//                                    console.log($scope.video.length); // CONSOLE.LOGGING

                                    pagetokenarr = data.nextPageToken;
                                    });
//                                    console.log($scope.video); // CONSOLE.LOGGING
//                                console.log($scope.video); // CONSOLE.LOGGING

                                angular.forEach($scope.video, function (video) {
                                var videoid = video.id;
//                                console.log(videoid); // CONSOLE.LOGGING

                                ServiceVideoDtls.videodetails(videoid, function(data){
//                                console.log("Vi er inde i videodetails") // CONSOLE.LOGGING
                                            videometrics = {
                                                id: data.items[0].id,
                                                title: video.title,
                                                dateofupload: video.dateofupload,
                                                views: data.items[0].statistics.viewCount,
                                                likes: data.items[0].statistics.likeCount,
                                                dislikes: data.items[0].statistics.dislikeCount,
                                                favoritecount: data.items[0].statistics.favoriteCount,
                                                commentcount: data.items[0].statistics.commentCount
                                            };
                                            $http({
                                                method: 'POST',
                                                url: 'http://localhost:8080/api/resources/videos/',
                                                data: videometrics,
                                                dataType: 'json'
                                            }).success(function (data) {
                                                $scope.channel.push(data);
                                                console.log('SUCCESS!'); // CONSOLE.LOGGING
                                                $scope.error = null;
                                            }).error(function (data, status) {
                                                if (status == 401) {
                                                    $scope.error = "You are not authenticated to Post these data";
                                                    return;
                                                }
                                                $scope.error = data;
                                            });

                                        })
                                        });
//                                }
                                });

                    })

我不知道是什么导致了这个问题,或者是否正常。

当我使用 Postman 检查http://localhost:8080/api/resources/videos/ 时,有 200 个视频应该调用(并且确实调用了)。 但是它仍然会不断打印出太多的“SUCCESS”console.log。

【问题讨论】:

    标签: javascript angularjs youtube-api


    【解决方案1】:

    所以视频数组进行了一些异步调用,因此我的代码在视频被正确推送到数组之前发布了一些数据。

    我刚刚删除了数组,它起作用了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-05
      • 2014-05-11
      • 1970-01-01
      • 2019-11-17
      • 2014-03-12
      • 2021-09-23
      • 2014-10-24
      • 2013-05-14
      相关资源
      最近更新 更多