Skip to content

Instantly share code, notes, and snippets.

@arubdesu
Last active August 29, 2015 14:04
Show Gist options
  • Save arubdesu/ccf32206ec01e50fa658 to your computer and use it in GitHub Desktop.
Save arubdesu/ccf32206ec01e50fa658 to your computer and use it in GitHub Desktop.
Utility to 1. find your soundcloud UID 2. create a folder for downloads(if not there, ~/Music/SndCldDwnlds) 3. look through the past 50 likes for your user to find download url's and 4. download 'em. Requires soundcloud module, installable via pip
#!/usr/bin/env python
# This tool checks your last 50 likes for downloadable files,
# then puts them in your Music folder, which it will check
# against on subsequent fetches.
import soundcloud, os, sys, urllib2, re
# Create this by making an app on the Soundcloud dev site
my_api_id = "xxxxxxxxxxxxxxxxxxxxxxx"
# you should know this...
username = "arub"
# We'll use the api to get your userid through which we'll find your likes
client = soundcloud.Client(client_id=my_api_id)
my_user = client.get('/resolve', url=('http://soundcloud.com/' + username))
my_user_id = str(my_user.id)
# This does the heavy lifting later on
def dwnldit(url, track_id, track_mp3):
print "First downloading url %r to %s" % (url, track_id)
mp3file = urllib2.urlopen(url)
output = open(track_id,'wb')
output.write(mp3file.read())
output.close()
# We deal with oddball characters like forward slashes in dates earlier,
# but this is so if there's still a name it barfs on you can fix it manually
print "Now renaming %s to %s" % ((track_id), (track_mp3))
try:
os.rename((track_id), (track_mp3))
except:
print "Sorry, could you find that one and fix it please?"
pass
# Setup our download destination folder
dest_dir = os.path.expanduser("~/Music/SndCldDwnlds/")
if not os.path.isdir(dest_dir):
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
print "Created directory %s" % dest_dir
os.chdir(dest_dir)
else:
print "File already exists at path %s, please rename or remove so we can continue" % dest_dir
sys.exit(1)
else:
print "Directory found at path %s" % dest_dir
os.chdir(dest_dir)
os.listdir(".")
# Use client ID to grab 'favorites' from your user_id and build a dictionary
client = soundcloud.Client(client_id=my_api_id)
tracks = client.get('/users/' + my_user_id + '/favorites')
tracklist = {}
for track in tracks:
try:
this_url = track.download_url
print "Found download link for track id %d, %s" % (track.id, track.title)
tracklist[track.id] = [track.download_url, track.title]
except:
pass
for k, v in tracklist.items():
string_compare_track_name = v[1] + ".mp3"
# Names with colons get mangled, so we call those out
track_raw = re.sub(':', '\:', str(v[1]))
# You'd get a 'file not found' error if you leave forward slashes
track_raw = re.sub('/', ':', track_raw) + ".mp3"
start_as_id = str(k)
if os.path.exists(track_raw):
print "Skipping %s" % track_raw
else:
dwnldit((str(v[0]) + "?client_id=" + my_api_id), start_as_id, track_raw)
print "Put down that cocktail! Your downloads are ready"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment