serkanyersen
4/25/2014 - 12:42 AM

reads all the annotations from google developers youtube channel and extracts goo.gl links

reads all the annotations from google developers youtube channel and extracts goo.gl links

// 1. load up youtube.com
// 2. open console and run this code

var jq = document.createElement('script');
jq.src = "https://code.jquery.com/jquery-latest.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);

// After that code has run, copy paste the following code and check results in console.
// Don't forget to add your own API key there

$(function() {

  var url = 'https://gdata.youtube.com/feeds/users/googledevelopers/uploads';
  $.ajax({
    url: url,
    dataType: "jsonp",
    crossDomain: true,
    data: {
      'alt': 'json',
      'start-index': 1, // increase this number after each request, 50, 100, 150, 200, 250 and so on.
      'max-results': 50 //  can only get max of 50, don't change this.
    },
    success: function(res) {
      $.each(res.feed.entry, function() {
        var a = 'http://gdata.youtube.com/feeds/videos/';
        var b = 'https://www.youtube.com/annotations_invideo?features=1&legacy=1&video_id=';
        var annotations = this.id.$t.replace(a, b);
        $.ajax({
          url: annotations,
          success: function(res, status, xhr) {
            var m = xhr.responseText.match(/(goo\.gl\/[\w\W]{6})[\"\\<\s]/gim);
            $.each(m || [], function() {
              var link = this.toString().replace('"', '').replace('<', '').replace(/\s+/g, '');
              var u = 'https://www.googleapis.com/urlshortener/v1/url/';
              $.ajax({
                url: u,
                dataType: "jsonp",
                crossDomain: true,
                data: {
                  'projection': 'FULL',
                  'key': 'API_KEY_HERE', // put your own API key here, othervise this won't work
                  'shortUrl': 'http://' + link
                },
                success: function(res) {
                  if (res.analytics) {
                    console.log(link, ' -- ', res.analytics.allTime.shortUrlClicks);
                  } else {
                    console.log(link, res);
                  }
                }
              });
            });
          }
        });

      });
    }
  });

});