6

I am building an application that would record what people say, generate an audio file and upload it to SoundCloud and get the URL of the uploaded track using Python.

I used PyAudio to record and generate an audio file - a wave file.

But I need to know how to upload the file to SoundCloud. By research I found there is a Python wrapper for SoundCloud API and with Python library Poster, one can easily upload files to SoundCloud.

How do I do it? I have not used this API thing before and I don't find a proper tutorial or a guide to how to make use of it. So if anybody can help me with this, please answer my question here.

How to use this SoundCloud Python API wrapper to upload files to SoundCloud using Python with the help of the Python library Poster?

vijay
  • 325
  • 1
  • 3
  • 12

1 Answers1

7

We just released a new Python API wrapper. You can get it on PyPi or from our Github account. To upload a track, you'll want to first get an access token using one of the supported OAuth2 auth flows. You can read about that in the README file. Let me know if you want me to elaborate on auth and I can edit my answer.

To get an access token, first register your application on soundcloud.com. You will need to provide a URI that users will be directed to after authorizing your application and you will be given a client id and client secret. Once you have those credentials, pass them to the Client constructor:

import soundcloud
client = soundcloud.Client(client_id=YOUR_CLIENT_ID,
                           client_secret=YOUR_CLIENT_SECRET,
                           redirect_uri="http://your/redirect/uri")

You'll then be able to redirect the user to the authorization URL in order to authorize your app. The user will be sent to soundcloud.com to log in (if they do not have an active session) and approve access for your app. Depending on the framework you're using (e.g. Django, Flask, etc) it could look something like this:

return redirect(client.authorize_url)

After approving access for your app, the user will be redirected to the redirect uri you specified when registering your app and in the constructor. The URL will have a query string that includes a 'code' parameter which you can then use to obtain an access token. Again, depending on the framework you're using, this could look like this:

code = request.params.get('code')
token = client.exchange_token(code)
print token.access_token  # don't actually print it, just showing how you would access it

You should probably store the access token (i.e. in some kind of data store like MySQL or Redis) so you can use it whenever that user wants to access SoundCloud in the future.

Once you've got an access token, uploading a track should be pretty simple. Once you've got your audio file, just send a POST request to the tracks resource. Here's an example:

import soundcloud

client = soundcloud.Client(access_token='YOUR_ACCESS_TOKEN')
track = client.post('/tracks', track={
    'title': 'The title you want to give your track',
    'sharing': 'private',  # make this 'public' if you want
    'asset_data': open('yourtrack.mp4', 'rb')
})

You'll get back a track resource, which you can then use to get the Soundcloud URL:

print track.permalink_url

Hope that helps! Let me know if you have any questions.

Paul Osman
  • 4,119
  • 2
  • 26
  • 20
  • Thanks a lot @Paul! It was really helpful! And yes, I would like to hear more on auth and about how to get the access token.. It would help me a lot.. – vijay Feb 14 '12 at 18:17
  • No problem vijay. I just edited my answer to include details about OAuth2. – Paul Osman Feb 15 '12 at 18:36
  • Paul, would it be possible to post this to your own account, i.e. not doing it on behalf of another user, but just for your own user. How would the authentication differ? – Andres Mar 04 '12 at 06:59
  • Hey Andres, you could use the User Credentials Flow in that case. http://developers.soundcloud.com/docs/api/authentication#user-credentials-flow – Paul Osman Mar 05 '12 at 16:50