require([
  '$api/models',
  '$api/search#Search'
], function (models, Search) {

  var base = 'http://api.tumblr.com/v2/';
  var apiKey = 'xzGO5Gq1iF5aYb0pMGi9GHoqzICmWBkFg65FCsTrKtsG8aSFX7';

  var loadFriends = function (blogNames, callback) {
    var datas = [];
    blogNames.forEach(function (blogName) {
      $.get(base + 'blog/' + blogName + '/info?api_key=' + apiKey, function (data) {
        datas.push(data.response.blog);
        if (datas.length === blogNames.length) {
          callback(datas);
        }
      });
    });
  };

  var trackListing = function (blogName, callback) {

    // Get the blog info
    $.get(base + 'blog/' + blogName + '.tumblr.com/info?api_key=' + apiKey, function (data) {

      var blog = data.response.blog;

      // Grab songs and find on spotify
      $.get(base + 'blog/' + blogName + '.tumblr.com/posts?type=audio&limit=50&api_key=' + apiKey, function (postData) {

          var uris = [];
          var pending = postData.response.posts.length;
          postData.response.posts.forEach(function (post) {

            // FAIL
            if (! post.track_name) {
              pending -= 1;
              return;
            }

            var searcher = Search.search(post.track_name + '  ' + post.artist);
            searcher.tracks.snapshot({ length: 1 }).done(function (t) {

              if (t.length > 0) { uris.push(t._uris[0]); } // add result

              pending -= 1;
              if (pending === 0) {
                callback(data.response.blog, uris);
              }

            });

          });

        });

      });

  };

  exports.loadFriends = loadFriends;
  exports.trackListing = trackListing;

});


@@ -1118,32 +1118,38 @@
 %7B%0A              
+if (!(
 pending -= 1;%0A  
@@ -1148,31 +1148,434 @@
 -= 1
-;%0A              return;
+)) %7B callback(data.response.blog, uris); %7D%0A              return;%0A            %7D%0A%0A            // do we already have the URI?%0A            if (post.source_title === 'Spotify') %7B%0A              var m = post.source_url.match(/track%5C/(%5Cw+)/);%0A              if (m) %7B%0A                uris.push('spotify:track:' + m%5B1%5D);%0A                if (!(pending -= 1)) %7B callback(data.response.blog, uris); %7D%0A                return;%0A              %7D
 %0A   
@@ -1734,25 +1734,24 @@
 ction (t) %7B%0A
-%0A
             
@@ -1814,37 +1814,8 @@
 sult
-%0A%0A              pending -= 1;
 %0A   
@@ -1828,16 +1828,18 @@
      if 
+(!
 (pending
@@ -1843,32 +1843,16 @@
 ing 
-=== 0) %7B%0A               
+-= 1)) %7B
  cal
@@ -1887,26 +1887,11 @@
 is);
-%0A             
  %7D%0A
-%0A
     

USe source urls if we have them