У меня есть код, который должен получить channelinfo by name
, а затем playlistId by channelinfo
, а затем и, наконец, videodetails by videos
.Youtube API v3 console.logging way too much
Примерно 200 видеороликов, которые вызывают один и тот же API YouTube 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;
});
})
});
// }
});
})
Я понятия не имею, что является причиной этой проблемы, или если нормально.
Когда я проверяю http://localhost:8080/api/resources/videos/
с почтальоном, есть 200 видео, которые он должен назвать (и сделал). однако он все еще слишком много распечатывает «УСПЕХ» console.log.