Permalink
Browse files

Refactoring some of the fetching logic into a new module

  • Loading branch information...
1 parent 8458fff commit ad60ed18d4dfcc36b60e3f22ca9ff6f97f8d000d David committed Nov 6, 2011
Showing with 20 additions and 20 deletions.
  1. +14 −0 browser_clone.py
  2. +6 −20 soundcloud.py
View
@@ -0,0 +1,14 @@
+#!/usr/bin/env python
+
+import cookielib
+import urllib2
+
+# set up header values and openers
+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'}
+cj = cookielib.MozillaCookieJar()
+opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj), urllib2.HTTPHandler())
+urllib2.install_opener(opener)
+
+def url_open(url):
+ request = urllib2.Request(url, headers=header_values)
+ return opener.open(request)
View
@@ -1,12 +1,11 @@
from optparse import OptionParser
-import cookielib
-import urllib2
import re
+from browser_clone import url_open
def get_stream_token_uid(page):
""" returns stream token and uid as tuple """
match = re.search('"uid":"([\w\d]+?)".*?stream_token=([\w\d]+)', page)
- if match:
+ if match:
uid = match.group(1)
stream_token = match.group(2)
return (uid, stream_token)
@@ -15,8 +14,7 @@ def download(uid, token):
""" given url with token and uid, download file to mp3 """
url = "http://media.soundcloud.com/stream/%s?stream_token=%s" % (uid, token)
- request = urllib2.Request(url, headers=header_values)
- response = opener.open(request)
+ response = url_open(url)
f = open('test.mp3', 'w')
f.write(response.read())
@@ -27,25 +25,13 @@ def download(uid, token):
parser.add_option("-u", "--url")
(options, args) = parser.parse_args()
- # set up header values and openers
- 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'}
- cj = cookielib.MozillaCookieJar()
- opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj), urllib2.HTTPHandler())
- urllib2.install_opener(opener)
-
# open up initial page (argument) to get stream token and uid
- request = urllib2.Request(options.url, headers=header_values)
- response = opener.open(request)
+ response = url_open(options.url)
html = response.read()
(uid, token) = get_stream_token_uid(html)
# the browser does this...so we will too
- request = urllib2.Request('http://media.soundcloud.com/crossdomain.xml', headers=header_values)
- response = opener.open(request)
+ response = url_open('http://media.soundcloud.com/crossdomain.xml')
- # compose a url with uid and token and request the mpeg
+ # compose a url with uid and token and request the mpeg
download(uid, token)
-
-
-
-

0 comments on commit ad60ed1

Please sign in to comment.