我有一些代码应该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;
});
})
});
// }
});
})
我不知道造成这个问题的原因是什么,或者是否正常。
当我用邮递员检查http://localhost:8080/api/resources/videos/
时,有200个视频应该调用(并且已经完成)。
但它仍然不断打印成功" SUCCESS" console.log太多了。
答案 0 :(得分:0)
所以视频数组正在进行一些异步调用,因此我的代码在视频被正确推送到数组之前发布了一些数据。
我刚刚删除了数组,但它确实有效。