javascript - Getting Youtube view counts for several videos using APIv3 (jquery) -
i'm trying create youtube social feed includes video, title, description, date, , view count. i've got work except last one, view count. i've been able view counts show out of order , i'm lost on how make them match respective videos. used video initial tutorial: [https://www.youtube.com/watch?v=jdqsifw74jk][1] got me far, i'm lost again. here code via code pen: [http://codepen.io/thatsmyjams/pen/ejzwex][2]
here html:
<div id="container"> <h2>results:</h2> <ul id="results"></ul> </div>
and here javascript:
var yourapikey = 'aizasydpy9fhgp7evndr5mgrxwwkgubtfm8l5pe'; var channelname = 'googledevelopers'; var vidcount = 5; var vidheight = 275; var vidwidth = 400; $(document).ready(function(){ $.get( "https://www.googleapis.com/youtube/v3/channels", { part: 'contentdetails', forusername: channelname, key: yourapikey}, function(data){ $.each(data.items, function(i, item){ console.log(item); playerid = item.contentdetails.relatedplaylists.uploads; getvids(playerid); }) } ); function getvids() { $.get( "https://www.googleapis.com/youtube/v3/playlistitems", { part: 'snippet', maxresults: vidcount, playlistid: playerid, key: 'aizasydpy9fhgp7evndr5mgrxwwkgubtfm8l5pe'}, function(data){ var output; $.each(data.items, function(i, item){ console.log(item); vidtitle = item.snippet.title; videoid = item.snippet.resourceid.videoid; viddate = item.snippet.publishedat; viddesc = item.snippet.description; output = '<li>'+vidtitle+'<span class="date">'+viddate+'</span><p class="description">'+viddesc+'</p><iframe height="'+vidheight+'" width ="'+vidwidth+'" src=\"//www.youtube.com/embed/'+videoid+'\"></iframe></li>'; //append results list $('#results').append(output); getviews(videoid[i]); }) } ); } function getviews() { $.get( "https://www.googleapis.com/youtube/v3/videos", { part: 'statistics', id: videoid, key: 'aizasydpy9fhgp7evndr5mgrxwwkgubtfm8l5pe'}, function(data){ var output2; $.each(data.items, function(i, item){ vidviews = item.statistics.viewcount; output2 = '<span class="views">'+vidviews+'</span>' //append results list $('#results li').each(function() { $(this).append(output2); }); }) } ); } });
i want viewcount each video , insert html did title, date, etc. appreciated :)
here's modified code. display views @ end of each video:
var yourapikey = 'aizasydpy9fhgp7evndr5mgrxwwkgubtfm8l5pe'; var channelname = 'googledevelopers'; var vidcount = 5; var vidheight = 275; var vidwidth = 400; $(document).ready(function () { $.get( "https://www.googleapis.com/youtube/v3/channels", { part: 'contentdetails', forusername: channelname, key: yourapikey }, function (data) { $.each(data.items, function (i, item) { console.log(item); playerid = item.contentdetails.relatedplaylists.uploads; getvids(playerid); }) } ); function getvids() { $.get( "https://www.googleapis.com/youtube/v3/playlistitems", { part: 'snippet', maxresults: vidcount, playlistid: playerid, key: 'aizasydpy9fhgp7evndr5mgrxwwkgubtfm8l5pe' }, function (data) { var output; $.each(data.items, function (i, item) { console.log(item); vidtitle = item.snippet.title; videoid = item.snippet.resourceid.videoid; viddate = item.snippet.publishedat; viddesc = item.snippet.description; var viewcountid = "viewcount" + i; output = '<li>' + vidtitle + '<span class="date">' + viddate + '</span><p class="description">' + viddesc + '</p><iframe height="' + vidheight + '" width ="' + vidwidth + '" src=\"//www.youtube.com/embed/' + videoid + '\"></iframe>"<span id="' + viewcountid + '"></span></li>'; //append results list $('#results').append(output); getviews(viewcountid); }) } ); } function getviews(viewcountid) { $.get( "https://www.googleapis.com/youtube/v3/videos", { part: 'statistics', id: videoid, key: 'aizasydpy9fhgp7evndr5mgrxwwkgubtfm8l5pe' }, function (data) { var output2; $.each(data.items, function (i, item) { $('#'+viewcountid).text("views=" + item.statistics.viewcount); }) } ); } });
Comments
Post a Comment