Click here to Skip to main content
15,890,609 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to get the views and description of the video from the YouTube through JavaScript and JSON


XML
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title></title>

  <script type='text/javascript' src='http://code.jquery.com/jquery-1.8.3.js'></script>



<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
function LoadVids(startindex){
if (typeof startindex === "undefined" || startindex===null) startindex = 1;
var maxresults = 10;

var playListURL = 'http://gdata.youtube.com/feeds/api/playlists/PL3B8939169E1256C0?orderby=published&v=2&alt=json&&start-index=' + startindex + '&max-results=' + maxresults;
var videoURL= 'videos.html?name=';
$.getJSON(playListURL, function(data) {
    var list_data="";
    $.each(data.feed.entry, function(i, item) {
        var feedTitle = item.title.$t;
        var feedURL = item.link[1].href;
        var fragments = feedURL.split("/");
        var videoID = fragments[fragments.length - 2];
        var vid = item.media$group.yt$videoid.$t;
        var url = videoURL + videoID;
        var vidtitle = item.title.$t;
        var vidviews = item.views.$t;
        var content = item.description.$t;
        var thumb = "http://img.youtube.com/vi/"+ videoID +"/default.jpg";
        list_data += '<div><a href="'+ url +'" title="'+ feedTitle +'"><img alt="'+ feedTitle+'" src="'+ thumb +'"</a>' + vidtitle +"<br/>"+ vidviews +"<br/>"+ content + '</div>';
    });
    $(list_data).appendTo(".cont");
});
}


//
$( document ).ready(function() {
 LoadVids(1); // call on load more click
});
});//


</script>
</head>
<body>
<ul  class="cont">
</ul>

</body>
</html>
Posted
Comments
Kornfeld Eliyahu Peter 25-Dec-14 4:44am    
You have two problems...
1. You are using old (v2)...
2. The data structure you are using does not fit the one of v2 either...
Krishna Chaitanya Kollu 25-Dec-14 4:56am    
Sir Will you Suggest some code to Resolve this Issue Please
Kornfeld Eliyahu Peter 25-Dec-14 5:05am    
You URL should look like this:
https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&maxResults=10&playlistId=PL3B8939169E1256C0&key={YOUR_API_KEY}
Read more here: https://developers.google.com/youtube/v3/docs/playlistItems/list
Krishna Chaitanya Kollu 25-Dec-14 5:32am    
Sir That Code is Not Working
$(window).load(function(){
function LoadVids(startindex){
if (typeof startindex === "undefined" || startindex===null) startindex = 1;
var maxresults = 10;

var playListURL = 'https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&maxResults=10&playlistId=PL3B8939169E1256C0&key={AIzaSyCnMsLoh3V5bzCJ5CYF3e_9X7-Ati9ecv0}' + startindex + '&max-results=' + maxresults;
var videoURL= 'videos.html?name=';
$.getJSON(playListURL, function(data) {
var list_data="";
$.each(data.feed.entry, function(i, item) {
var feedTitle = item.title.$t;
var feedURL = item.link[1].href;
var fragments = feedURL.split("/");
var videoID = fragments[fragments.length - 2];
var vid = item.media$group.yt$videoid.$t;
var url = videoURL + videoID;
var vidtitle = item.title.$t;
var vidviews = item.views.$t;
var content = item.description.$t;
var thumb = "http://img.youtube.com/vi/"+ videoID +"/default.jpg";
list_data += '<div><img alt="'+ feedTitle+'" src="'+ thumb +'"' + vidtitle +"<br/>"+ vidviews +"<br/>"+ content + '</div>';
});
$(list_data).appendTo(".cont");
});
}


//
$( document ).ready(function() {
LoadVids(1); // call on load more click
});
});//
Kornfeld Eliyahu Peter 25-Dec-14 5:49am    
https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&maxResults=10&playlistId=PL3B8939169E1256C0&key=AIzaSyCnMsLoh3V5bzCJ5CYF3e_9X7-Ati9ecv0
Key Should not been enclosed in {}, also that part of startindex is wrong, and max-results should be maxResults and already exists!!!

JavaScript
var rootURI = 'https://www.googleapis.com/youtube/v3/playlistItems?playlistId=PL3B8939169E1256C0&key=AIzaSyCnMsLoh3V5bzCJ5CYF3e_9X7-Ati9ecv0&part=contentDetails&maxResults=10';
var pageToken = '&pageToken=';


$(document).ready(function () {
    LoadVids();
});

function LoadVids(pageTokenID) {
    var playListURL = rootURI + ((pageTokenID) ? pageToken + pageTokenID : '');

    $.getJSON(playListURL, function (data) {
        var list_data = "";

        $.each(data.items, function (i, item) {
            // do what you want to do
        });
    });
}

The actual data has a header that contains the page info for next page and the total number of items (for paging)...
JavaScript
{
  "kind": "youtube#playlistItemListResponse",
  "etag": etag,
  "nextPageToken": string,
  "prevPageToken": string,
  "pageInfo": {
    "totalResults": integer,
    "resultsPerPage": integer
  },
  "items": [
  ]
}

The actual item data depends on the value the part parameter...
Read about it on the help pages of Google: https://developers.google.com/youtube/v3/docs/playlistItems#resource[^]
 
Share this answer
 
Kornfeld Sir Thanks For Your Support I'll Never Forget Your Support
After Reading So Many Documents I Got My Solution in V2 Only
XML
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title></title>

  <script type='text/javascript' src='http://code.jquery.com/jquery-1.8.3.js'></script>



<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
function LoadVids(startindex){
if (typeof startindex === "undefined" || startindex===null) startindex = 1;
var maxresults = 10;

var playListURL = 'http://gdata.youtube.com/feeds/api/playlists/PL3B8939169E1256C0?orderby=published&v=2&alt=json&&start-index=' + startindex + '&max-results=' + maxresults;
var videoURL= 'videos.html?name=';
$.getJSON(playListURL, function(data) {
    var list_data="";
    $.each(data.feed.entry, function(i, item) {
        var feedTitle = item.title.$t;
        var feedURL = item.link[1].href;
        var fragments = feedURL.split("/");
        var videoID = fragments[fragments.length - 2];
        var vid = item.media$group.yt$videoid.$t;
        var url = videoURL + videoID;
        var vidtitle = item.title.$t;
        var vidviews = item.yt$statistics.viewCount;
        var content = item.media$group.media$description.$t;
        var thumb = "http://img.youtube.com/vi/"+ videoID +"/default.jpg";
        list_data += '<a href="'+ url +'" title="'+ feedTitle +'"><div><img alt="'+ feedTitle+'" src="'+ thumb +'" />' + vidtitle +"<br/>"+ vidviews +"<br/>"+ content +'</div>''</a>';
    });
    $(list_data).appendTo(".cont");
});
}


//
$( document ).ready(function() {
 LoadVids(1); // call on load more click
});
});//


</script>
</head>
<body>
<ul  class="cont">
</ul>

</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900