Skip to content

Instantly share code, notes, and snippets.

@sherbrow
Created August 14, 2013 14:42
Show Gist options
  • Save sherbrow/6231722 to your computer and use it in GitHub Desktop.
Save sherbrow/6231722 to your computer and use it in GitHub Desktop.
Get youtube user stats : here, we get the total view count of some user Needs a lot of improvements
/**
* Requires jQuery (will fallback)
* Can take advantage of `numeral()` https://github.com/adamwdraper/Numeral-js
*/
// Dependencies
/*
if(typeof jQuery === 'undefined') {
var elt = document.createElement('script');
elt.src = 'http://code.jquery.com/jquery.min.js';
document.getElementsByTagName('head')[0].appendChild(elt);
// $.noConflict();
}
// */
var username = ''; // Youtube username here
if(!username.length) username = /user\/([^\/]+)/.exec(document.location)[1];
var url = 'https://gdata.youtube.com/feeds/api/users/'+username+'?v=2';
jQuery.get(url, {responseType: 'text/xml'})
.success(function(data){
// This _might_ need cleanup & pretty selectors
var views = parseInt(data.documentElement.getElementsByTagName('yt:statistics')[0].getAttribute('totalUploadViews'),10);
// Use `views`
if(window.numeral) views = numeral(views).format('0,0.');
alert(views+' views');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment