soundcloud.py 7.7 KB

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