soundcloud.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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"name": u"SoundcloudSet",
  66. u"url":"https://soundcloud.com/the-concept-band/sets/the-royal-concept-ep",
  67. u"playlist": [
  68. {
  69. u"file":"30510138.mp3",
  70. u"md5":"f9136bf103901728f29e419d2c70f55d",
  71. u"info_dict": {
  72. u"upload_date": u"20111213",
  73. u"description": u"The Royal Concept from Stockholm\r\nFilip / Povel / David / Magnus\r\nwww.royalconceptband.com",
  74. u"uploader": u"The Royal Concept",
  75. u"title": u"D-D-Dance"
  76. }
  77. },
  78. {
  79. u"file":"47127625.mp3",
  80. u"md5":"09b6758a018470570f8fd423c9453dd8",
  81. u"info_dict": {
  82. u"upload_date": u"20120521",
  83. u"description": u"The Royal Concept from Stockholm\r\nFilip / Povel / David / Magnus\r\nwww.royalconceptband.com",
  84. u"uploader": u"The Royal Concept",
  85. u"title": u"The Royal Concept - Gimme Twice"
  86. }
  87. },
  88. {
  89. u"file":"47127627.mp3",
  90. u"md5":"154abd4e418cea19c3b901f1e1306d9c",
  91. u"info_dict": {
  92. u"upload_date": u"20120521",
  93. u"uploader": u"The Royal Concept",
  94. u"title": u"Goldrushed"
  95. }
  96. },
  97. {
  98. u"file":"47127629.mp3",
  99. u"md5":"2f5471edc79ad3f33a683153e96a79c1",
  100. u"info_dict": {
  101. u"upload_date": u"20120521",
  102. u"description": u"The Royal Concept from Stockholm\r\nFilip / Povel / David / Magnus\r\nwww.royalconceptband.com",
  103. u"uploader": u"The Royal Concept",
  104. u"title": u"In the End"
  105. }
  106. },
  107. {
  108. u"file":"47127631.mp3",
  109. u"md5":"f9ba87aa940af7213f98949254f1c6e2",
  110. u"info_dict": {
  111. u"upload_date": u"20120521",
  112. u"description": u"The Royal Concept from Stockholm\r\nFilip / David / Povel / Magnus\r\nwww.theroyalconceptband.com",
  113. u"uploader": u"The Royal Concept",
  114. u"title": u"Knocked Up"
  115. }
  116. },
  117. {
  118. u"file":"75206121.mp3",
  119. u"md5":"f9d1fe9406717e302980c30de4af9353",
  120. u"info_dict": {
  121. u"upload_date": u"20130116",
  122. 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! ",
  123. u"uploader": u"The Royal Concept",
  124. u"title": u"World On Fire"
  125. }
  126. }
  127. ]
  128. }
  129. def report_resolve(self, video_id):
  130. """Report information extraction."""
  131. self.to_screen(u'%s: Resolving id' % video_id)
  132. def _real_extract(self, url):
  133. mobj = re.match(self._VALID_URL, url)
  134. if mobj is None:
  135. raise ExtractorError(u'Invalid URL: %s' % url)
  136. # extract uploader (which is in the url)
  137. uploader = mobj.group(1)
  138. # extract simple title (uploader + slug of song title)
  139. slug_title = mobj.group(2)
  140. full_title = '%s/sets/%s' % (uploader, slug_title)
  141. self.report_resolve(full_title)
  142. url = 'http://soundcloud.com/%s/sets/%s' % (uploader, slug_title)
  143. resolv_url = 'http://api.soundcloud.com/resolve.json?url=' + url + '&client_id=b45b1aa10f1ac2941910a7f0d10f8e28'
  144. info_json = self._download_webpage(resolv_url, full_title)
  145. videos = []
  146. info = json.loads(info_json)
  147. if 'errors' in info:
  148. for err in info['errors']:
  149. self._downloader.report_error(u'unable to download video webpage: %s' % compat_str(err['error_message']))
  150. return
  151. self.report_extraction(full_title)
  152. for track in info['tracks']:
  153. video_id = track['id']
  154. streams_url = 'https://api.sndcdn.com/i1/tracks/' + str(video_id) + '/streams?client_id=b45b1aa10f1ac2941910a7f0d10f8e28'
  155. stream_json = self._download_webpage(streams_url, video_id, u'Downloading track info JSON')
  156. self.report_extraction(video_id)
  157. streams = json.loads(stream_json)
  158. mediaURL = streams['http_mp3_128_url']
  159. videos.append({
  160. 'id': video_id,
  161. 'url': mediaURL,
  162. 'uploader': track['user']['username'],
  163. 'upload_date': unified_strdate(track['created_at']),
  164. 'title': track['title'],
  165. 'ext': u'mp3',
  166. 'description': track['description'],
  167. })
  168. return videos