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?
The SDKs will make it easier to access the SoundCloud API on your framework of choice. We officially provide and support only JavaScript. All other SDKs (Ruby, Python) are supported by third-party developers.
Your data usage on SoundCloud is a product of the quality of the audio files that you listen to (higher quality means more data used) and how much time you spend listening to songs. Most songs on SoundCloud are decent-quality MP3s that are around 3.5 minutes long, so they take about 5 megabytes to stream.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With