soundcloud.py 7.1 KB

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