Permalink
Browse files

Getting song title too

  • Loading branch information...
1 parent ad60ed1 commit d7fb644d3a75222f47cb09ede0f05de6652c12ac David committed Nov 6, 2011
Showing with 28 additions and 21 deletions.
  1. +1 −0 .gitignore
  2. +27 −21 soundcloud.py
View
@@ -0,0 +1 @@
+*.pyc
View
@@ -2,36 +2,42 @@
import re
from browser_clone import url_open
-def get_stream_token_uid(page):
+def get_stream_url(page):
""" returns stream token and uid as tuple """
- match = re.search('"uid":"([\w\d]+?)".*?stream_token=([\w\d]+)', page)
+ match = re.search('http://media.soundcloud.com/stream/.*?(?=\")', page)
if match:
- uid = match.group(1)
- stream_token = match.group(2)
- return (uid, stream_token)
+ return match.group(0)
-def download(uid, token):
- """ given url with token and uid, download file to mp3 """
+def get_song_title(page):
+ match = re.search('(?<=\"title\":\").*?(?=\")', page)
+ if match:
+ return match.group(0)
- url = "http://media.soundcloud.com/stream/%s?stream_token=%s" % (uid, token)
- response = url_open(url)
+def download(stream_url, song_title):
+ """ given url with token and uid, download file to mp3 """
+ response = url_open(stream_url)
+ song_file = song_title + '.mp3'
- f = open('test.mp3', 'w')
+ f = open(song_file, 'w')
f.write(response.read())
if __name__ == '__main__':
parser = OptionParser()
parser.add_option("-u", "--url")
(options, args) = parser.parse_args()
-
- # open up initial page (argument) to get stream token and uid
- response = url_open(options.url)
- html = response.read()
- (uid, token) = get_stream_token_uid(html)
-
- # the browser does this...so we will too
- response = url_open('http://media.soundcloud.com/crossdomain.xml')
-
- # compose a url with uid and token and request the mpeg
- download(uid, token)
+ if not options.url:
+ print "Need to give a URL silly"
+ else:
+ # open up initial page (argument) to get stream token and uid
+ response = url_open(options.url)
+ html = response.read()
+ stream_url = get_stream_url(html)
+ song_title = get_song_title(html)
+ print song_title
+
+ # the browser does this...so we will too
+ #response = url_open('http://media.soundcloud.com/crossdomain.xml')
+
+ # compose a url with uid and token and request the mpeg
+ #download(stream_url)

0 comments on commit d7fb644

Please sign in to comment.