Jason
Jason

Reputation: 4957

How to display playback_count for a user using the SoundCloud API

I am trying to display the total track views ("playback_count") for a specific SoundCloud user using the SoundCloud API.

According to the API documentation I get the info using the below function call:

http://api.soundcloud.com/tracks/13158665.json?client_id=YOUR_CLIENT_ID

This is fine because it displays the number "13158665".

What is this number? Is it the trackid?

I need to get the "playback_count" for a user using the users username.

I tried getting the UserId from the Username using this:

$soundcloud_playsAPI = "MY_SOUNDCLOUD_API_KEY";

/* Get the SoundCloud UserId from the username */
$json = wp_remote_get("http://api.soundcloud.com/users/jwagener.json?client_id=".$soundcloud_playsAPI);

$soundcloudData = json_decode($json['body'], true);
$soundcloud_userid = $soundcloudData['id'];

This returns the UserId: 3207181

Now I tried to substitute that response into the previous URL to get the "playback_count" but it failed.

$json = wp_remote_get("http://api.soundcloud.com/tracks/3207181.json?client_id=".$soundcloud_playsAPI);
$soundcloudPlaysData = json_decode($json['body'], true);
echo $soundcloudPlaysData['playback_count'];

Any guidance would be greatly appreciated.

Thanks.

Upvotes: 0

Views: 1497

Answers (2)

HeatherSpence
HeatherSpence

Reputation: 170

Here is the full solution:

function listPlays() 
    {SC.initialize({  client_id: 'YOUR ID HERE'}); 
    // Get the SoundCloud UserId from the username 
    var userName="jwagener";
    SC.get("/users/"+userName,  function (users)
            {console.log(users.id);
            var myId=users.id;
            getTracks(myId);
            });

    var getTracks=function (myId)
        {var totalPlays=0;
        SC.get("/users/"+myId+"/tracks", function(getTracks)
            {for (var key in getTracks)    //get each track and look at it's playback_count
                {console.log(getTracks[key].title+"     "+getTracks[key].playback_count);
                totalPlays+=getTracks[key].playback_count;   //add the playback count for this track to the total
                }
            console.log("Total Plays for all tracks: "+totalPlays);

            });
        };

};

Upvotes: 0

HeatherSpence
HeatherSpence

Reputation: 170

The first number is the id of a track, the second number is the id of a user.

Now that you have the user id, you will need to fetch each of their tracks and tally how many times they have been played

First, get the id numbers for all tracks made by the user

GET: /users/{id}/tracks: list of tracks of the user

$json = wp_remote_get("http://api.soundcloud.com/users/3207181/tracks.json?client_id=".$soundcloud_playsAPI);

Now you have a list of track IDs so you will need to get each of those tracks and save the playback_count of each

$json = wp_remote_get("http://api.soundcloud.com/tracks/track-id-here.json?client_id=".$soundcloud_playsAPI);
$soundcloudPlaysData = json_decode($json['body'], true);
echo $soundcloudPlaysData['playback_count'];

Upvotes: 1

Related Questions