Essentially, I am attempting to extract the video duration for each video that appears in the search results. Feel free to play around with the demo I have prepared!
Additionally, when I refer to duration, I am talking about the length of the video in M/S format (0:00)..
DEMO http://codepen.io/mistkaes/pen/6b6a347c7d24edee15b3491420db4ecd
jQuery:
var apikey = '<API KEY>';
$(function() {
var searchField = $('#search-input');
$('#search-form').submit(function(e) {
e.preventDefault();
});
});
function search() {
$('#results').html('');
q = $('#search-input').val();
$.get(
"https://www.googleapis.com/youtube/v3/search", {
part: 'snippet, id',
q: q,
maxResults: 50,
type: 'video',
key: apikey
},
function(data) {
$.each(data.items, function(i, item) {
var output = getResults(item);
$('#results').append(output);
});
});
}
function getResults(item) {
var videoID = item.id.videoId;
var title = item.snippet.title;
var thumb = item.snippet.thumbnails.high.url;
var channelTitle = item.snippet.channelTitle;
var output = '<li>' +
'<div class="list-left">' +
'<img src="' + thumb + '">' +
'</div>' +
'<div class="list-right">' +
'<h3><a href="http://youtube.com/embed/' + videoID + '?rel=0">' + title + '</a></h3>' +
'<p class="cTitle">' + channelTitle + '</p>' +
'</div>' +
'</li>' +
'<div class="clearfix"></div>' +
'';
return output;
}