通过API获取YouTube视频的喜欢/不喜欢的数量?

时间:2010-09-01 14:48:35

标签: youtube youtube-api

如何通过 YouTube API 获取YouTube视频的喜欢/不喜欢的数量?

4 个答案:

答案 0 :(得分:10)

您可以像这样查询YouTube API:

<?php

$curlhandle = curl_init();
curl_setopt($curlhandle, CURLOPT_URL, "http://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&q=computers&max-results=10&orderby=viewCount");
curl_setopt($curlhandle, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($curlhandle);
curl_close($curlhandle);

$json = json_decode($response);


foreach ($json->data->items as $result)
{

        echo '<div class="video"><a href="'.$result->player->default.'" target="_blank">';
        echo '<img src="'.$result->thumbnail->hqDefault.'">';
        echo ' <div class="title"> '.$result->title.'</div><div class="rating">'.$result->likeCount.'</div></a></div>';
        //print_r($result);

}

&GT?;

答案 1 :(得分:3)

如果您使用的是Java API,那么您可以通过以下方式获得喜欢:

YtRaing ytRating = videoEntry.getYtRating();
int likes = ytRating.getNumLikes();
int dislikes = ytRating.getNumDislikes();

videoEntry,是来自(com.google.gdata.data.youtube.VideoEntry)的VideoEntry变量

答案 2 :(得分:3)

如果你想知道不喜欢的地方,dislikeCount = ratingCount - likeCount

答案 3 :(得分:0)

喜欢计数,可以通过将部分设置为统计信息

来获取不喜欢信息

下面是python代码:

payload = {'id': search_result["id"]["videoId"], 'part': 'statistics', 'key': DEVELOPER_KEY}
l = requests.Session().get('https://www.googleapis.com/youtube/v3/videos', params=payload)    
print l.text

回复将是:

{
 "kind": "youtube#videoListResponse",
 "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/0NR0uhQMzlaae_et8wHFZKsdFPA\"",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 1
 },
 "items": [
  {
   "kind": "youtube#video",
   "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/vBL_50n3XI1eQcsdivfxN_g9c2M\"",
   "id": "hMncTg0iBko",
   "statistics": {
    "viewCount": "10281",
    "likeCount": "61",
    "dislikeCount": "9",
    "favoriteCount": "0",
    "commentCount": "1"
   }
  }
 ]
}