Skip to content

Instantly share code, notes, and snippets.

@mcprat
Created May 17, 2019 22:00
Show Gist options
  • Save mcprat/0ecc8cfda428cea4cca774c1fa18a04f to your computer and use it in GitHub Desktop.
Save mcprat/0ecc8cfda428cea4cca774c1fa18a04f to your computer and use it in GitHub Desktop.
Soundcloud API - Download all private tracks from an account with login info
# designed for Python 2
# in the future look into not having to authenticate for other peoples public tracks
# using the id from the waveform url to add to http://media.soundcloud.com/stream/
from time import sleep
try:
import requests
import soundcloud
except ImportError:
print('you are missing either the requests module or the soundcloud-python API module')
exit(1)
#instantiate session and get basic account status
SC = soundcloud.Client(
client_id='CLIENT_ID',
client_secret='CLIENT_SECRET',
username='[email protected]',
password='PASSWORD'
)
username = SC.get('/me').username
print 'Username: ',username
ID = SC.get('/me').id
print 'ID: ',ID
PubTrackCount = SC.get('/me').track_count
print 'Public Track Count: ',PubTrackCount
PrivTrackCount = SC.get('/me').private_tracks_count
print 'Private Track Count: ',PrivTrackCount
FavCount = SC.get('/me').public_favorites_count
print 'Liked Tracks: ',FavCount
PubPlaylistCount = SC.get('/me').playlist_count
print 'Public Playlists: ',PubPlaylistCount
PrivPlaylistCount = SC.get('/me').private_playlists_count
print 'Private Playlists: ',PrivPlaylistCount
#playlists = SC.get('/me/playlists')
#followings = SC.get('/me/followings')
#followers = SC.get('/me/followers')
#favorites = SC.get('/me/favorites')
# Get all tracks
TrackCount = PubTrackCount + PrivTrackCount
#print TrackCount
tracklist = []
# first call to resource
tracks = SC.get('/me/tracks', order='created_at', limit = 50, linked_partitioning = 1)
# loops to get all tracks, 50 at a time
# conditions that arent working:
#while tracks.collection != None:
#while tracks.next_href != None:
while len(tracklist) < TrackCount:
for track in tracks.collection:
tracklist.append(track)
#print tracklist
#print len(tracklist)
if len(tracklist) < TrackCount:
tracks = SC.get(tracks.next_href, order='created_at', limit = 50, linked_partitioning = 1)
#print tracklist
#print len(tracklist)
# Get ID list, downloadable list, download URI list
idlist = []
for track in tracklist:
idlist.append(track.id)
#print idlist
#print len(idlist)
ifdownloadable = []
for track in tracklist:
ifdownloadable.append(track.downloadable)
#print ifdownloadable
#print len(ifdownloadable)
downURI = []
for track in tracklist:
downURI.append(track.download_url)
#print downURI
#print len(downURI)
# Delete all tracks
# sleep in between to prevent HTTP 500
# and retry if HTTP 500 occurs
# printing on successful API PUT Request
#except requests.exceptions.RequestException as e:
#print 'retrying...'
#continue
#class HTTPError(Exception):
#pass
for track in tracklist:
while True:
try:
sleep(2)
print 'deleted track number:',tracklist.index(track)
#SC.delete(track.uri)
except requests.HTTPError:
break
break
print 'done!'
# designed for Python 2
# in the future look into not having to authenticate for other peoples public tracks
# using the id from the waveform url to add to http://media.soundcloud.com/stream/
from time import sleep
try:
import requests
import soundcloud
except ImportError:
print('you are missing either the requests module or the soundcloud-python API module')
exit(1)
#instantiate session and get basic account status
SC = soundcloud.Client(
client_id='CLIENT_ID',
client_secret='CLIENT_SECRET',
username='[email protected]',
password='PASSWORD'
)
username = SC.get('/me').username
print 'Username: ',username
ID = SC.get('/me').id
print 'ID: ',ID
PubTrackCount = SC.get('/me').track_count
print 'Public Track Count: ',PubTrackCount
PrivTrackCount = SC.get('/me').private_tracks_count
print 'Private Track Count: ',PrivTrackCount
FavCount = SC.get('/me').public_favorites_count
print 'Liked Tracks: ',FavCount
PubPlaylistCount = SC.get('/me').playlist_count
print 'Public Playlists: ',PubPlaylistCount
PrivPlaylistCount = SC.get('/me').private_playlists_count
print 'Private Playlists: ',PrivPlaylistCount
#playlists = SC.get('/me/playlists')
#followings = SC.get('/me/followings')
#followers = SC.get('/me/followers')
#favorites = SC.get('/me/favorites')
# Get all tracks
TrackCount = PubTrackCount + PrivTrackCount
#print TrackCount
tracklist = []
# first call to resource
tracks = SC.get('/me/tracks', order='created_at', limit = 50, linked_partitioning = 1)
# loops to get all tracks, 50 at a time
# conditions that arent working:
#while tracks.collection != None:
#while tracks.next_href != None:
while len(tracklist) < TrackCount:
for track in tracks.collection:
tracklist.append(track)
#print tracklist
#print len(tracklist)
if len(tracklist) < TrackCount:
tracks = SC.get(tracks.next_href, order='created_at', limit = 50, linked_partitioning = 1)
#print tracklist
#print len(tracklist)
# Get ID list, downloadable list, download URI list
idlist = []
for track in tracklist:
idlist.append(track.id)
#print idlist
#print len(idlist)
ifdownloadable = []
for track in tracklist:
ifdownloadable.append(track.downloadable)
#print ifdownloadable
#print len(ifdownloadable)
downURI = []
for track in tracklist:
downURI.append(track.download_url)
#print downURI
#print len(downURI)
# Make all tracks downloadable
# sleep in between to prevent HTTP 500
# and retry if HTTP 500 occurs
# printing on successful API PUT Request
#print tracklist[1].downloadable
for track in tracklist:
while True:
try:
if track.downloadable == False:
SC.put(track.uri, track={
'downloadable': 'true'
})
sleep(1)
print 'now downloadable for track number:',tracklist.index(track)
except requests.HTTPError:
print 'retrying...'
continue
break
print 'done!'
# Direct Download of original media typically requires:
# secret token, client ID, client secret or Oauth token
# in the format:
# https://api.soundcloud.com/tracks/{ID}/download?A=_&B=_&C=_...
# secret token and OAuth token come along with the resource objects
# view the full contents of objects with <>.__dict__
DirectDownloadList = []
for track in tracklist:
URIparts = [str(track.download_url),
'?secret_token=',
str(track.secret_token),
'&client_id=',
str(SC.client_id),
'&oauth_token=',
str(SC.access_token)]
URI = ''.join(URIparts)
DirectDownloadList.append(URI)
#print DirectDownloadList
#print len(DirectDownloadList)
# Save all lists in a file
lists = open('lists.txt', 'a')
lists.write('\n\n')
lists.write('\n\n')
lists.write(str(idlist))
lists.write('\n\n')
lists.write(str(ifdownloadable))
lists.write('\n\n')
lists.write(str(downURI))
lists.write('\n\n')
lists.write(str(DirectDownloadList))
lists.write('\n\n')
lists.close()
# Download all tracks
for track in tracklist:
# make filename by date and time to be OS sane and join with .ogg
filename = ''.join([str(track.created_at).replace('/', '-').replace(':', '.')[:19],'.ogg'])
# regenerate download URL for each track
URIparts = [str(track.download_url),
'?secret_token=',
str(track.secret_token),
'&client_id=',
str(SC.client_id),
'&oauth_token=',
str(SC.access_token)]
URI = ''.join(URIparts)
#download the original .ogg for each track
with open(filename, "w+b") as ogg:
response = requests.get(URI)
ogg.write(response.content)
ogg.close()
print 'downloaded track ',track
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment