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.

241 lines
9.4 KiB

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