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();
});

Interested in JavaScript? I send out weekly, personalized emails with articles and conference talks. Click here to see an example and subscribe.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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