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.

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