Reading the Youtube API from PhantomJS

The following code will retrieve the duration of a video from the Youtube API. The output must be in JSON, because PhantomJS currently doesn’t handle the XML correctly (from email threads it appears this will be fixed in a future release)

address = 'http://gdata.youtube.com/feeds/api/videos/' + id + '?v=2&alt=json';
page.open(address, function (status) {
   var duration = page.evaluate(function(){
       var data = JSON.parse(document.querySelector("pre").innerText);
       return data.entry.media$group.media$content[0].duration;
   });
     
   console.log("video duration: " + duration);
   phantom.exit();
});

Leave a Reply

Your email address will not be published. Required fields are marked *