From 311f5d1509f48ebfa4d881af39d74be5ba45ea58 Mon Sep 17 00:00:00 2001
From: Mort Yao <mort.yao@gmail.com>
Date: Mon, 10 Dec 2012 00:09:13 +0100
Subject: [PATCH] add support for SoundCloud, fix #33

---
 .gitignore                       |  2 ++
 CHANGELOG.txt                    |  8 +++++---
 README.md                        |  2 ++
 README.txt                       |  1 +
 you_get/common.py                | 14 ++++++++++++--
 you_get/downloader/__init__.py   |  1 +
 you_get/downloader/soundcloud.py | 31 +++++++++++++++++++++++++++++++
 you_get/main.py                  |  1 +
 you_get/version.py               |  4 ++--
 9 files changed, 57 insertions(+), 7 deletions(-)
 create mode 100644 you_get/downloader/soundcloud.py

diff --git a/.gitignore b/.gitignore
index f0cef17f..5cf38bca 100644
--- a/.gitignore
+++ b/.gitignore
@@ -8,8 +8,10 @@ _*/
 *.download
 *.cmt.*
 *.3gp
+*.asf
 *.flv
 *.mkv
+*.mp3
 *.mp4
 *.mpg
 *.ts
diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index 0171f246..e5e152f4 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -1,12 +1,14 @@
 Changelog
 =========
 
-0.3dev-20121209
+0.3dev-20121210
 ---------------
 
-*Date: 2012-12-09*
+*Date: 2012-12-10*
 
-* YouTube: downloading the highest available quality now
+* YouTube: downloading the highest available quality now.
+* Add support for:
+    - SoundCloud
 
 0.2.16
 ------
diff --git a/README.md b/README.md
index f4866c7a..d957760d 100644
--- a/README.md
+++ b/README.md
@@ -14,6 +14,7 @@ Fork me on GitHub: <https://github.com/soimort/you-get>
 * Vimeo <http://vimeo.com>
 * Dailymotion <http://dailymotion.com>
 * Google+ <http://plus.google.com>
+* SoundCloud <http://soundcloud.com>
 * Youku (优酷) <http://www.youku.com>
 * Tudou (土豆) <http://www.tudou.com>
 * YinYueTai (音悦台) <http://www.yinyuetai.com>
@@ -202,6 +203,7 @@ You-Get基于优酷下载脚本[iambus/youku-lixian](https://github.com/iambus/y
 * Vimeo <http://vimeo.com>
 * Dailymotion <http://dailymotion.com>
 * Google+ <http://plus.google.com>
+* SoundCloud <http://soundcloud.com>
 * 优酷 <http://www.youku.com>
 * 土豆 <http://www.tudou.com>
 * 音悦台 <http://www.yinyuetai.com>
diff --git a/README.txt b/README.txt
index d50cdc1b..e3fd637f 100644
--- a/README.txt
+++ b/README.txt
@@ -17,6 +17,7 @@ Supported Sites (As of Now)
 * Vimeo http://vimeo.com
 * Dailymotion http://dailymotion.com
 * Google+ http://plus.google.com
+* SoundCloud http://soundcloud.com
 * Youku (优酷) http://www.youku.com
 * Tudou (土豆) http://www.tudou.com
 * YinYueTai (音悦台) http://www.yinyuetai.com
diff --git a/you_get/common.py b/you_get/common.py
index 34fe125a..d19d6a77 100644
--- a/you_get/common.py
+++ b/you_get/common.py
@@ -125,7 +125,9 @@ def url_info(url, faker = False):
         'video/mp4': 'mp4',
         'video/MP2T': 'ts',
         'video/webm': 'webm',
-        'video/x-flv': 'flv'
+        'video/x-flv': 'flv',
+        'video/x-ms-asf': 'asf',
+        'audio/mpeg': 'mp3'
     }
     if type in mapping:
         ext = mapping[type]
@@ -355,7 +357,7 @@ def download_urls(urls, title, ext, total_size, output_dir = '.', refer = None,
         print('Real URLs:\n', urls, '\n')
         return
     
-    assert ext in ('3gp', 'flv', 'mp4', 'webm')
+    #assert ext in ('3gp', 'flv', 'mp4', 'webm')
     if not total_size:
         try:
             total_size = urls_size(urls)
@@ -504,8 +506,12 @@ def playlist_not_supported(name):
 def print_info(site_info, title, type, size):
     if type in ['3gp']:
         type = 'video/3gpp'
+    elif type in ['asf']:
+        type = 'video/x-ms-asf'
     elif type in ['flv', 'f4v']:
         type = 'video/x-flv'
+    elif type in ['mp3']:
+        type = 'audio/mpeg'
     elif type in ['mp4']:
         type = 'video/mp4'
     elif type in ['ts']:
@@ -531,8 +537,12 @@ def print_info(site_info, title, type, size):
     #    type_info = "Matroska video (%s)" % type
     #elif type in ['video/x-ms-wmv']:
     #    type_info = "Windows Media video (%s)" % type
+    elif type in ['video/x-ms-asf']:
+        type_info = "Advanced Systems Format (%s)" % type
     #elif type in ['video/mpeg']:
     #    type_info = "MPEG video (%s)" % type
+    elif type in ['audio/mpeg']:
+        type_info = "MP3 (%s)" % type
     else:
         type_info = "Unknown type (%s)" % type
     
diff --git a/you_get/downloader/__init__.py b/you_get/downloader/__init__.py
index e656dc12..1adc856b 100644
--- a/you_get/downloader/__init__.py
+++ b/you_get/downloader/__init__.py
@@ -12,6 +12,7 @@ from .pptv import *
 from .qq import *
 from .sina import *
 from .sohu import *
+from .soundcloud import *
 from .tudou import *
 from .vimeo import *
 from .w56 import *
diff --git a/you_get/downloader/soundcloud.py b/you_get/downloader/soundcloud.py
new file mode 100644
index 00000000..2e1190a7
--- /dev/null
+++ b/you_get/downloader/soundcloud.py
@@ -0,0 +1,31 @@
+#!/usr/bin/env python
+
+__all__ = ['soundcloud_download', 'soundcloud_download_by_id']
+
+from ..common import *
+
+def soundcloud_download_by_id(id, title = None, output_dir = '.', merge = True, info_only = False):
+    assert title
+    
+    #if info["downloadable"]:
+    #   url = 'https://api.soundcloud.com/tracks/' + id + '/download?client_id=b45b1aa10f1ac2941910a7f0d10f8e28'
+    url = 'https://api.soundcloud.com/tracks/' + id + '/stream?client_id=b45b1aa10f1ac2941910a7f0d10f8e28'
+    assert url
+    type, ext, size = url_info(url)
+    
+    print_info(site_info, title, type, size)
+    if not info_only:
+        download_urls([url], title, ext, size, output_dir, merge = merge)
+
+def soundcloud_download(url, output_dir = '.', merge = True, info_only = False):
+    metadata = get_html('https://api.sndcdn.com/resolve.json?url=' + url + '&client_id=b45b1aa10f1ac2941910a7f0d10f8e28')
+    import json
+    info = json.loads(metadata)
+    title = info["title"]
+    id = str(info["id"])
+    
+    soundcloud_download_by_id(id, title, output_dir, merge = merge, info_only = info_only)
+
+site_info = "SoundCloud.com"
+download = soundcloud_download
+download_playlist = playlist_not_supported('soundcloud')
diff --git a/you_get/main.py b/you_get/main.py
index bf3de6f0..a987e0ab 100644
--- a/you_get/main.py
+++ b/you_get/main.py
@@ -34,6 +34,7 @@ def url_to_module(url):
         'sina': sina,
         'smgbb': bilibili,
         'sohu': sohu,
+        'soundcloud': soundcloud,
         'tudou': tudou,
         'vimeo': vimeo,
         'yinyuetai': yinyuetai,
diff --git a/you_get/version.py b/you_get/version.py
index c4d7be68..75577b60 100644
--- a/you_get/version.py
+++ b/you_get/version.py
@@ -1,4 +1,4 @@
 #!/usr/bin/env python
 
-__version__ = '0.3dev-20121209'
-__date__ = '2012-12-09'
+__version__ = '0.3dev-20121210'
+__date__ = '2012-12-10'