]> git.bitcoin.ninja Git - youtube-dl/blob - soundcloud.py
5f3a5540d2775ae1952d31ab86447ed5151e952f
[youtube-dl] / soundcloud.py
1 import json
2 import re
3
4 from .common import InfoExtractor
5 from ..utils import (
6     compat_str,
7     compat_urlparse,
8
9     ExtractorError,
10     unified_strdate,
11 )
12
13
14 class SoundcloudIE(InfoExtractor):
15     """Information extractor for soundcloud.com
16        To access the media, the uid of the song and a stream token
17        must be extracted from the page source and the script must make
18        a request to media.soundcloud.com/crossdomain.xml. Then
19        the media can be grabbed by requesting from an url composed
20        of the stream token and uid
21      """
22
23     _VALID_URL = r'''^(?:https?://)?
24                     (?:(?:(?:www\.)?soundcloud\.com/([\w\d-]+)/([\w\d-]+)/?(?:[?].*)?$)
25                        |(?:api\.soundcloud\.com/tracks/(?P<track_id>\d+))
26                        |(?P<widget>w.soundcloud.com/player/?.*?url=.*)
27                     )
28                     '''
29     IE_NAME = u'soundcloud'
30     _TEST = {
31         u'url': u'http://soundcloud.com/ethmusic/lostin-powers-she-so-heavy',
32         u'file': u'62986583.mp3',
33         u'md5': u'ebef0a451b909710ed1d7787dddbf0d7',
34         u'info_dict': {
35             u"upload_date": u"20121011", 
36             u"description": u"No Downloads untill we record the finished version this weekend, i was too pumped n i had to post it , earl is prolly gonna b hella p.o'd", 
37             u"uploader": u"E.T. ExTerrestrial Music", 
38             u"title": u"Lostin Powers - She so Heavy (SneakPreview) Adrian Ackers Blueprint 1"
39         }
40     }
41
42     _CLIENT_ID = 'b45b1aa10f1ac2941910a7f0d10f8e28'
43
44     @classmethod
45     def suitable(cls, url):
46         return re.match(cls._VALID_URL, url, flags=re.VERBOSE) is not None
47
48     def report_resolve(self, video_id):
49         """Report information extraction."""
50         self.to_screen(u'%s: Resolving id' % video_id)
51
52     @classmethod
53     def _resolv_url(cls, url):
54         return 'http://api.soundcloud.com/resolve.json?url=' + url + '&client_id=' + cls._CLIENT_ID
55
56     def _extract_info_dict(self, info, full_title=None):
57         video_id = info['id']
58         name = full_title or video_id
59         self.report_extraction(name)
60
61         thumbnail = info['artwork_url']
62         if thumbnail is not None:
63             thumbnail = thumbnail.replace('-large', '-t500x500')
64         return {
65             'id':       info['id'],
66             'url':      info['stream_url'] + '?client_id=' + self._CLIENT_ID,
67             'uploader': info['user']['username'],
68             'upload_date': unified_strdate(info['created_at']),
69             'title':    info['title'],
70             'ext':      u'mp3',
71             'description': info['description'],
72             'thumbnail': thumbnail,
73         }
74
75     def _real_extract(self, url):
76         mobj = re.match(self._VALID_URL, url, flags=re.VERBOSE)
77         if mobj is None:
78             raise ExtractorError(u'Invalid URL: %s' % url)
79
80         track_id = mobj.group('track_id')
81         if track_id is not None:
82             info_json_url = 'http://api.soundcloud.com/tracks/' + track_id + '.json?client_id=' + self._CLIENT_ID
83             full_title = track_id
84         elif mobj.group('widget'):
85             query = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query)
86             return self.url_result(query['url'][0], ie='Soundcloud')
87         else:
88             # extract uploader (which is in the url)
89             uploader = mobj.group(1)
90             # extract simple title (uploader + slug of song title)
91             slug_title =  mobj.group(2)
92             full_title = '%s/%s' % (uploader, slug_title)
93     
94             self.report_resolve(full_title)
95     
96             url = 'http://soundcloud.com/%s/%s' % (uploader, slug_title)
97             info_json_url = self._resolv_url(url)
98         info_json = self._download_webpage(info_json_url, full_title, u'Downloading info JSON')
99
100         info = json.loads(info_json)
101         return self._extract_info_dict(info, full_title)
102
103 class SoundcloudSetIE(SoundcloudIE):
104     _VALID_URL = r'^(?:https?://)?(?:www\.)?soundcloud\.com/([\w\d-]+)/sets/([\w\d-]+)(?:[?].*)?$'
105     IE_NAME = u'soundcloud:set'
106     _TEST = {
107         u"url":"https://soundcloud.com/the-concept-band/sets/the-royal-concept-ep",
108         u"playlist": [
109             {
110                 u"file":"30510138.mp3",
111                 u"md5":"f9136bf103901728f29e419d2c70f55d",
112                 u"info_dict": {
113                     u"upload_date": u"20111213",
114                     u"description": u"The Royal Concept from Stockholm\r\nFilip / Povel / David / Magnus\r\nwww.royalconceptband.com",
115                     u"uploader": u"The Royal Concept",
116                     u"title": u"D-D-Dance"
117                 }
118             },
119             {
120                 u"file":"47127625.mp3",
121                 u"md5":"09b6758a018470570f8fd423c9453dd8",
122                 u"info_dict": {
123                     u"upload_date": u"20120521",
124                     u"description": u"The Royal Concept from Stockholm\r\nFilip / Povel / David / Magnus\r\nwww.royalconceptband.com",
125                     u"uploader": u"The Royal Concept",
126                     u"title": u"The Royal Concept - Gimme Twice"
127                 }
128             },
129             {
130                 u"file":"47127627.mp3",
131                 u"md5":"154abd4e418cea19c3b901f1e1306d9c",
132                 u"info_dict": {
133                     u"upload_date": u"20120521",
134                     u"uploader": u"The Royal Concept",
135                     u"title": u"Goldrushed"
136                 }
137             },
138             {
139                 u"file":"47127629.mp3",
140                 u"md5":"2f5471edc79ad3f33a683153e96a79c1",
141                 u"info_dict": {
142                     u"upload_date": u"20120521",
143                     u"description": u"The Royal Concept from Stockholm\r\nFilip / Povel / David / Magnus\r\nwww.royalconceptband.com",
144                     u"uploader": u"The Royal Concept",
145                     u"title": u"In the End"
146                 }
147             },
148             {
149                 u"file":"47127631.mp3",
150                 u"md5":"f9ba87aa940af7213f98949254f1c6e2",
151                 u"info_dict": {
152                     u"upload_date": u"20120521",
153                     u"description": u"The Royal Concept from Stockholm\r\nFilip / David / Povel / Magnus\r\nwww.theroyalconceptband.com",
154                     u"uploader": u"The Royal Concept",
155                     u"title": u"Knocked Up"
156                 }
157             },
158             {
159                 u"file":"75206121.mp3",
160                 u"md5":"f9d1fe9406717e302980c30de4af9353",
161                 u"info_dict": {
162                     u"upload_date": u"20130116",
163                     u"description": u"The unreleased track World on Fire premiered on the CW's hit show Arrow (8pm/7pm central).  \r\nAs a gift to our fans we would like to offer you a free download of the track!  ",
164                     u"uploader": u"The Royal Concept",
165                     u"title": u"World On Fire"
166                 }
167             }
168         ]
169     }
170
171     def _real_extract(self, url):
172         mobj = re.match(self._VALID_URL, url)
173         if mobj is None:
174             raise ExtractorError(u'Invalid URL: %s' % url)
175
176         # extract uploader (which is in the url)
177         uploader = mobj.group(1)
178         # extract simple title (uploader + slug of song title)
179         slug_title =  mobj.group(2)
180         full_title = '%s/sets/%s' % (uploader, slug_title)
181
182         self.report_resolve(full_title)
183
184         url = 'http://soundcloud.com/%s/sets/%s' % (uploader, slug_title)
185         resolv_url = self._resolv_url(url)
186         info_json = self._download_webpage(resolv_url, full_title)
187
188         videos = []
189         info = json.loads(info_json)
190         if 'errors' in info:
191             for err in info['errors']:
192                 self._downloader.report_error(u'unable to download video webpage: %s' % compat_str(err['error_message']))
193             return
194
195         self.report_extraction(full_title)
196         return {'_type': 'playlist',
197                 'entries': [self._extract_info_dict(track) for track in info['tracks']],
198                 'id': info['id'],
199                 'title': info['title'],
200                 }