Created
April 9, 2014 15:32
-
-
Save aaronbieber/10283633 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import urllib, re, argparse, subprocess, sys, eyeD3 | |
parser = argparse.ArgumentParser(description='Get music from Soundcloud.') | |
parser.add_argument('music_page') | |
options = parser.parse_args() | |
tag = eyeD3.Tag() | |
path = re.match(".*com(/.*)$", options.music_page) | |
song_path = path.group(1) | |
print "Retrieving %s..." % options.music_page | |
page = urllib.urlopen(options.music_page) | |
doc = page.read() | |
page.close() | |
print "Locating URL and track title..." | |
filename = None | |
get_url = None | |
artist = None | |
title = None | |
lines = doc.split("\n") | |
for line in lines: | |
if re.match(".*window.SC.bufferTracks.push.*%s" % song_path, line): | |
matches = re.match('.*"username":"(.*?)".*?"title":"(.*?)".*?"streamUrl":"(.*?)".*', line) | |
try: | |
if matches.group(1) and matches.group(2) and matches.group(3): | |
artist = matches.group(1) | |
title = matches.group(2) | |
filename = "%s - %s.mp3" % (artist, title) | |
get_url = matches.group(3) | |
except IndexError: | |
print "Track data couldn't be found in the page!" | |
print "Getting \"%s\"..." % filename | |
p = subprocess.Popen(['wget', '-q', '-O', filename, get_url]) | |
p.wait() | |
tag.link(filename) | |
tag.header.setVersion(eyeD3.ID3_V2_3) | |
tag.setArtist(artist) | |
tag.setTitle(title) | |
tag.update() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment