Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactoring for reability

  • Loading branch information...
commit 6bb2fad03da63b9718c57e18a05f52597bb1244f 1 parent d7fb644
David authored
Showing with 35 additions and 32 deletions.
  1. +2 −32 soundcloud.py
  2. +33 −0 utilities.py
View
34 soundcloud.py
@@ -1,25 +1,5 @@
from optparse import OptionParser
-import re
-from browser_clone import url_open
-
-def get_stream_url(page):
- """ returns stream token and uid as tuple """
- match = re.search('http://media.soundcloud.com/stream/.*?(?=\")', page)
- if match:
- return match.group(0)
-
-def get_song_title(page):
- match = re.search('(?<=\"title\":\").*?(?=\")', page)
- if match:
- return match.group(0)
-
-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(song_file, 'w')
- f.write(response.read())
+from utilities import download
if __name__ == '__main__':
@@ -29,15 +9,5 @@ def download(stream_url, song_title):
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)
+ download(options.url)
View
33 utilities.py
@@ -0,0 +1,33 @@
+#!/usr/bin/env python
+
+from browser_clone import url_open
+import re
+
+def get_stream_url(page):
+ """ returns stream token and uid as tuple """
+ match = re.search('http://media.soundcloud.com/stream/.*?(?=\")', page)
+ if match:
+ return match.group(0)
+
+def get_song_title(page):
+ match = re.search('(?<=\"title\":\").*?(?=\")', page)
+ if match:
+ return match.group(0)
+
+def download(song_url):
+ """ given url with token and uid, download file to mp3 """
+
+ # open up initial page (argument) to get stream token and uid
+ response = url_open(song_url)
+ html = response.read()
+ stream_url = get_stream_url(html)
+ song_title = get_song_title(html)
+
+ # the browser does this...so we will too
+ response = url_open('http://media.soundcloud.com/crossdomain.xml')
+
+ response = url_open(stream_url)
+ song_file = song_title + '.mp3'
+
+ f = open(song_file, 'w')
+ f.write(response.read())
Please sign in to comment.
Something went wrong with that request. Please try again.