View difference between Paste ID: jVgekwce and K2FSBWqb
SHOW: | | - or go back to the newest paste.
1-
#!/usr/bin/env python
1+
#!/usr/bin/python
2
"""
3-
Soundcloud downloader 
3+
Soundcloud downloader
4
usage: python soundcloud.py [URL]
5
6
    URL     soundcloud.com URL of the track to download
7
            ie. http://soundcloud.com/dunkelbunt/picnic-with-dunkelbunt
8
9
  * http://developers.soundcloud.com/docs/widget
10
  * https://github.com/soundcloud/Widget-JS-API/wiki
11
12
"""
13
14
import sys
15
import time
16
import urllib
17
import json
18
19
class MozillaURLopener(urllib.FancyURLopener):
20
    version = "Mozilla/5.0 (X11; Linux i686; rv:7.0.1) Gecko/20100101 Firefox/7.0.1"
21
22
def reporthook(blocks_read, block_size, total_size):
23-
    sys.stdout.write("\r% 3.1f%% of %d bytes " 
23+
    sys.stdout.write("\r% 3.1f%% of %d bytes "
24
        % (float(blocks_read * block_size) / total_size * 100, total_size))
25
    sys.stdout.flush()
26
27
def main(argv):
28
    if len(argv) > 1 and argv[1].startswith("http://soundcloud.com/"):
29
        url = argv[1]
30
    elif len(argv) > 1 and argv[1].startswith("https://soundcloud.com/"):
31
        url = argv[1]
32
    else:
33
        print(__doc__)
34
        url = raw_input("// soundcloud.com track url: ")
35
    url = "http://soundcloud.com/widget.json?" + urllib.urlencode({'url':url})
36
    urllib._urlopener = MozillaURLopener() # override the urlopener
37
    widget = urllib.urlopen(url).read()
38-
    name = "soundcloud." + str(int(time.time() * 100)) + ".mp3"
38+
39
    url = data['stream_url'] # http://developers.soundcloud.com/docs/widget
40
#    name = "soundcloud." + str(int(time.time() * 100)) + ".mp3"
41
    name = data['title'] + "-soundcloud.mp3"
42
    print("// try to retrieve remote %s to local %s" % (url, name))
43
    (filename, headers) = urllib.urlretrieve(url, name, reporthook)
44
    print("Done. (filename: %s, headers: %s)" % (filename, headers))
45
    return 0
46
47
if __name__ == "__main__":
48
    sys.exit(main(sys.argv))