开发者

Get the amount of likes/dislikes for a YouTube video via API?

开发者 https://www.devze.com 2023-01-14 22:52 出处:网络
How do I get the amount of likes/dislikes for开发者_如何学Python a YouTube video via YouTube API?You can query the YouTube API like this:

How do I get the amount of likes/dislikes for开发者_如何学Python a YouTube video via YouTube API?


You can query the YouTube API like this:

<?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);

}

?>


If you are using Java API, then you could get the likes dislikes as follows:

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

videoEntry, is the VideoEntry variable from (com.google.gdata.data.youtube.VideoEntry)


If you are wondering where the dislikes are, dislikeCount = ratingCount - likeCount


Likes count, Dislikes can be fetched by setting part as statistics

Below is the python code:

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

Response will be:

{
 "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"
   }
  }
 ]
}
0

精彩评论

暂无评论...
验证码 换一张
取 消