Duncan Palmer
Duncan Palmer

Reputation: 2913

Parsing Youtube video feed XML

So I am trying to make a script that gets the most recent video uploaded to my youtube channel. here is i what I have so far...

<body onLoad="loadVids()">
    <script>
        function loadXMLDoc(channel)
        {
            if (window.XMLHttpRequest)
              {
              xhttp=new XMLHttpRequest();
              }
            else
              {
              xhttp=new ActiveXObject("Microsoft.XMLHTTP");
              }
            xhttp.open("GET","https://gdata.youtube.com/feeds/api/users/"+channel+"/uploads/",false);
            xhttp.send();
            return xhttp.responseXML;
        }

        function loadVids() {
            var xml = loadXMLDoc("CHANNELNAME");


        }


    </script>

Now that I have loaded the XML how do I get each video uploaded?

Thanks.

Upvotes: 0

Views: 1912

Answers (1)

Sandeep Manne
Sandeep Manne

Reputation: 6092

Normally for doing javascript parsing json data is much easier, youtube provides json data api. Here is the example of how to get video details using youtube json data api.

function loadVideosJson(channelName, callback) {
    $.getJSON("https://gdata.youtube.com/feeds/api/users/"+channelName+"/uploads?v=2&alt=json", callback)
}

function onVideoLoad(data) {
    var entries = data.feed.entry;
    for (i=0 ; i<entries.length; i++ ) {
        //Title
        console.log(data.feed.entry[i].title["$t"]);
        //Video thumbnail 
        console.log(data.feed.entry[i].media$group.media$thumbnail[0].url);
        //Like that you can parse through json to get different attributes of video.
    }

}
//Call above functions using
loadVideosJson("bbc", onVideoLoad);

Upvotes: 3

Related Questions