Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

finish. now successfully downloads .mp3 files and saves it to file. n…

…eed to regex out title of song. (with much help from thedjpetersen and uberj
  • Loading branch information...
commit 89f85a0af43f3b1f7aa0300b5e0efe4ec04e58ed 1 parent 4a4b13d
Kevin Ngo authored November 04, 2011

Showing 1 changed file with 27 additions and 15 deletions. Show diff stats Hide diff stats

  1. 42  soundcloud.py
42  soundcloud.py
... ...
@@ -1,10 +1,8 @@
1 1
 from optparse import OptionParser
2  
-import mechanize
  2
+import cookielib
  3
+import urllib2
3 4
 import re
4 5
 
5  
-def open_page(url):
6  
-    return mechanize.urlopen(url)
7  
-
8 6
 def get_stream_token_uid(page):
9 7
     """ returns stream token and uid as tuple """
10 8
     match = re.search('"uid":"([\w\d]+?)".*?stream_token=([\w\d]+)', page)
@@ -13,26 +11,40 @@ def get_stream_token_uid(page):
13 11
         stream_token = match.group(2)
14 12
         return (uid, stream_token)
15 13
 
  14
+def download(uid, token)
  15
+    """ given url with token and uid, download file to mp3 """
  16
+
  17
+    url = "http://media.soundcloud.com/stream/%s?stream_token=%s" % (uid, token)
  18
+    request = urllib2.Request(url, headers=header_values)
  19
+    response = opener.open(request)
  20
+
  21
+    f = open('test.mp3', 'w')
  22
+    f.write(response.read())
  23
+
16 24
 if __name__ == '__main__':
17 25
 
18 26
     parser = OptionParser()
19 27
     parser.add_option("-u", "--url")
20 28
     (options, args) = parser.parse_args()
21 29
 
22  
-    cj = mechanize.LWPCookieJar()
23  
-    opener = mechanize.build_opener(mechanize.HTTPCookieProcessor(cookies))
24  
-    mechanize.install_opener(opener)
25  
-
26  
-    browser = mechanize.Browser(factory=mechanize.RobustFactory())
27  
-    browser.open(options.url)
28  
-    html = browser.response().get_data()
  30
+    # set up header values and openers
  31
+    header_values =  {'User-Agent' : 'Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16', 'Accept' : 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5', 'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', 'Accept-Encoding' : 'gzip,deflate,sdch', 'Accept-Language' : 'en-US,en;q=0.8', 'Cache-Control' : 'max-age=0', 'Connection' : 'keep-alive'}
  32
+    cj = cookielib.MozillaCookieJar()
  33
+    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj), urllib2.HTTPHandler())
  34
+    urllib2.install_opener(opener)
29 35
 
30  
-    # HOW GET COOKIE?
  36
+    # open up initial page (argument) to get stream token and uid
  37
+    request = urllib2.Request(options.url, headers=header_values)
  38
+    response = opener.open(request)
  39
+    html = response.read()
  40
+    (uid, token) = get_stream_token_uid(html)
31 41
 
32  
-    #page = open_page(options.url).read()
33  
-    #(uid, stream_token) = get_stream_token_uid(page)
  42
+    # the browser does this...so we will too
  43
+    request = urllib2.Request('http://media.soundcloud.com/crossdomain.xml', headers=header_values)
  44
+    response = opener.open(request)
34 45
 
35  
-    #url = "http://media.soundcloud.com/stream/%s?stream_token=%s" % (uid, stream_token)
  46
+    # compose a url with uid and token and request the mpeg 
  47
+    download(uid, token)
36 48
 
37 49
 
38 50
 

0 notes on commit 89f85a0

Please sign in to comment.
Something went wrong with that request. Please try again.