My Sites


Saturday, August 1, 2015

YouTube Data API (v3) with javascript - Essentials



https://developers.google.com/youtube/v3/docs/playlists/list
https://developers.google.com/youtube/v3/docs/playlistItems/list
https://developers.google.com/youtube/v3/docs/videos/list


$.getJSON('https://www.googleapis.com/youtube/v3/channels', { part: 'contentDetails' , forUsername : 'vivahachanel', key :'AIzaSyAK-kqi3Ft5XQVmi6MX6Cf8ByuyAwwcG4Q' }, function(data) {
                    console.dir(data);

                    $.each(data.items, function(index, element) {
                        //console.log(element.id);
                        var pid = element.contentDetails.relatedPlaylists.uploads;
                        //console.log(pid);
                       getPlaylists(element.id);
                    });
 });

$.getJSON('https://www.googleapis.com/youtube/v3/playlists', { part: 'snippet,contentDetails' , maxResults : 50, channelId:channelID,key :'AIzaSyAK-kqi3Ft5XQVmi6MX6Cf8ByuyAwwcG4Q' }, function(data) {
                    console.dir(data);
                    $.each(data.items, function(index, element) {

                        var videoTitle = element.snippet.title;
                        var publishDate = element.snippet.publishedAt;
                        var thumbnail = element.snippet.thumbnails.high.url;
                        var author = element.snippet.channelTitle;
                        var playlistID = element.id;
                        var videoCount = element.contentDetails.itemCount;
                        //console.log(videoTitle  + publishDate + thumbnail + author);
                       var e =  '<div class="row"><hr><a href="videopage.html?key='+playlistID+'" style="color:black"><h4 class="list-group-item-heading col-xs-12">'+videoTitle+'</h4>    <img src="'+thumbnail+'" class="img-responsive img-rounded col-xs-5" ><p class="list-group-item-text col-xs-7" style="font-size: 14px;font-weight: 700;">No of videos : '+videoCount+'<br>Author : '+author+' <br> Created : '+getDateFromString(publishDate)+'</p>    </a></div>';

                       $('#videoContainer').append(e);                                        });
        });function getDateFromString(datestring) {
                var d = new Date(datestring);
                var curr_date = d.getDate();
                var curr_month = d.getMonth();
                curr_month++;
                var curr_year = d.getFullYear();
                return curr_year + "-" + curr_month + "-" +curr_date ;}

No comments:

Post a Comment