Search code examples
javascriptapisoundcloudprofile

SoundCloud API - create user profiles based on ID in system


I have this system that takes in an array of SoundCloud user IDs, iterates them through a series of SC.get functions to obtain information of each user (user ID, username, followings and genre preference).

<script src="//connect.soundcloud.com/sdk.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<body>

<script>
    //Initialize soundcloud API with client ID
SC.initialize({
    client_id: "54cb0ff94ff9313ef6ca4674e9fe3026"
});


var userIds = [9110252, 55829847, 145189041, 4184647, 225458844, 22557004, 109447512];

var userids = [];
var usernames = [];
var userFollowingsE = [];
var profile =[];
var ready = 0

for(u = 0; u < userIds.length; u++){

  var id = userIds[u];
  getUserData(id);

  function getUserData(userid){ 

  //**************************** USER ***************************//  
  SC.get("/users/"+id, function(user){

    userids.push(user.id);
    usernames.push(user.username);
  });

  //********************** USER/FOLLOWINGS **********************//
  SC.get('/users/'+id+'/followings',function(followings) {

    var userFollowings = [];

      for(i = 0; i < followings.collection.length; i++) { 
        userFollowings.push(followings.collection[i].username);                                         
    } 

    userFollowingsE.push(userFollowings);

      ready++
      if(ready >= userIds.length) onComplete();
    });
  }  

}

function onComplete(){

  console.log(usernames);
  console.log(userIds);
  console.log(userFollowingsE); 
}

var users = [ 
  { userid: this.userids,
    username: this.usernames,
    genre: this.genres,
    followings: this.followings,
  }
]

</script>
</body>

What I want the system to do is associate these bits of information with their corresponding user in an object e.g.

var users = {
  user9110252: {
    userid = userid, 
    username = username,
    genrePref = genre
    followings = followings
  }
}//etc...

However the arrays outputted from the system change order each time and I don't think are associated with one another.

I am unsure of how to do this and welcome any suggestions.


Solution

  • If you want the resulting array to look like you described, you should build the user Object as you go, instead of creating multiple arrays and then merging them.

    Here is what I've tried, run it and see if it gives you the expected result. Also note that I did not include the genres, because I could not find them in the data provided.

    //Initialize soundcloud API with client ID
    SC.initialize({ client_id: "54cb0ff94ff9313ef6ca4674e9fe3026" });
    
    var userIds = [9110252, 55829847, 145189041, 4184647, 225458844, 22557004, 109447512],
        users   = [];
    
    for(var i=0; i<userIds.length; i++){ getUserData(userIds[i]); }
    
    function getUserData(userid){
        // The user Object you'll be building
        var myUser = {};
        // Grab the info for this user
        SC.get("/users/"+userid, function(user){
            myUser.userid = user.id;
            myUser.username = user.username;
            // Then get its followings
            SC.get('/users/'+userid+'/followings',function(followings) {
                myUser.followings = followings.collection.map(function(user){
                    return user.username;
                });
                // Push that user to the user list
                users.push(myUser);
                // Execute the callback if all users have been fetched
                if(users.length == userIds.length){ onComplete(); }
            });
        });
    }
    
    function onComplete(){
        console.log(users);
        // Just for the demo:
        document.body.innerHTML = '<pre>' + JSON.stringify(users,0,2) + '</pre>';
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://connect.soundcloud.com/sdk.js"></script>