我正在使用node-youtube(data api)来获取youtube-saerch的结果。当我写作
res.render('index',{data:(JSON.stringify(result, null, 2))});
然后我得到两个结果。但是当我写res.render('index',{data:result});
时,我只得到结果。如何通过简单地写res.render('index',{data:result});
来获得更多结果
而不是写
res.render('index',{data:(JSON.stringify(result, null, 2))});
以下是getbyId()metod的代码。
var YouTube = require('youtube-node');
var youTube = new YouTube();
youTube.setKey('*************************');
youTube.getById('HcwTxRuq-uk', function(error, result) {
if (error) {
console.log(error);
}
else {
res.render('index',{data:(JSON.stringify(result, null, 2))});
}
});
我也尝试过JSON.parse()方法。 像这样
var str=(JSON.stringify(result, null, 3));
var data=JSON.parse(str);
在str中有3个结果,但在数据中只有1个结果。为什么它回归 一个结果。我也可以使用JSON.parse()获得3个结果。
答案 0 :(得分:0)
我知道这不是你的问题,但我认为它可以帮助你了解我在我的一个项目中的表现。
如果您的视频位于播放列表中,则可以同时获取最多50个视频,如果您想要播放列表中的更多视频,请使用pageToken
获取更多视频。
您可以像这样获取播放列表:
/**
* Return an array about the videos contained in the GEM-MECHANIC playlist (based on the selected language).
*
* @param $errorMessage String to return the error message.
* @param $language Int language code for which we must fetch the video (default 1 = english)
* @param $maxVideo Int maximum of video we must fetch with the request (<= 0 mean infinite, 5 if invalid)
* @param $playList Int Playlist which want to fetch (Use PlayList class constants).
*
* @return Bidimensionnal Array
*/
static public function getVideoList(&$errorMessage, $language = 1, $maxVideo = 0, $playList = PlayList::GEM_CAR){
$errorMessage = "";
$list = array();
$gemPlayList = self::getPlayList($playList);
if (array_key_exists($language, $gemPlayList)){
if (!is_numeric($maxVideo) or $maxVideo < 0){
$maxVideo = 5;
}
$list = self::fetchVideoList($errorMessage, $gemPlayList[$language], $maxVideo);
}
elseif(empty($gemPlayList)){
$errorMessage = GeneralDbManager::getInstance()->getErrorMessage("GEM_MECHANIC_INVALID_PLAYLIST_ERR", "The selected playlist doesn't exists.");
}
else{
$errorMessage = GeneralDbManager::getInstance()->getErrorMessage("GEM_MECHANIC_INVALID_LANGUAGE_ERR", 'The selected playlist do not contains videos for the language selected.');
}
return $list;
}
/**
* Return an array about the videos contained in the GEM-MECHANIC playlist (based on the selected language).
*
* @param $errorMessage String to return the error message.
* @param $playListId String id of the youtube playlist for which we want to fetch the video list.
* @param $maxVideo Int maximum of video we must fetch with the request
* @param $maxVideo Int number of videos with fetched so far.
* @param $nextToken String to use to fetch more videos.
*
* @return Bidimensionnal Array
*/
private static function fetchVideoList(&$errorMessage, $playListId, $maxVideo, $currentCount = 0, $nextToken = "", $currentList = array()){
if ($currentCount < $maxVideo or $maxVideo === 0) {
$result = abs($maxVideo - $currentCount);
$param = array('playlistId' => $playListId);
if ($result > 50 or $result === 0){
$param['maxResults'] = 50;
$result = 50;
}
else{
$param['maxResults'] = $result;
}
if (!empty($nextToken)){
$param['pageToken'] = $nextToken;
}
try{
$client = new Google_Client();
$client->setDeveloperKey(self::$apiKey);
$youtube = new Google_Service_YouTube($client);
$playList = $youtube->playlistItems->listPlaylistItems('contentDetails, snippet', $param);
unset($youtube);
unset($client);
foreach($playList as $video){
$currentList[] = array('id' => $video['contentDetails']['videoId'], "title" => $video['snippet']['title'], "check" => 0);
}
$currentCount += $result;
if (empty($errorMessage) and !is_null($playList['nextPageToken']) and $currentCount < $maxVideo){
self::fetchVideoList($errorMessage, $language, $maxVideo, $currentCount, $playList['nextPageToken'], $currentList);
}
unset($playList);
}
catch (Google_Exception $exception) {
ExceptionLogger::logException($exception);
$errorMessage = GeneralDbManager::getInstance()->getErrorMessage("GEM_MECHANIC_CANT_FETCH_VIDEO_ERR", 'We are currently not able to fetch the video list from Youtube.');
}
}
return $currentList;
}
答案 1 :(得分:0)
我希望这就是你想要的
另请注意根本不需要JSON.stringify
var YouTube = require('youtube-node');
var youTube = new YouTube();
youTube.setKey('************************************');
youTube.search('World War z Trailer', 2, function(error, result) {
if (error) {
console.log(error);
} else {
// result should contain 2 videos with some info
// to get more info use getById
var videos = [];
var video1 = result.items[0].id.videoId;
var video2 = result.items[1].id.videoId;
//get 1st video
youTube.getById(video1, function(error, result) {
if (!error) videos.push(result.items[0]);
// get 2nd video
youTube.getById(video2, function(error, result) {
if (!error) videos.push(result.items[0]);
//console.log(videos[0]);
//console.log(videos[1]);
res.render('index',{data:videos)});
});
});
}
});