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