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.

200 lines
7.9 KiB

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