You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
8.3 KiB

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