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.

129 lines
4.9 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. def report_resolve(self, video_id):
  20. """Report information extraction."""
  21. self.to_screen(u'%s: Resolving id' % video_id)
  22. def _real_extract(self, url):
  23. mobj = re.match(self._VALID_URL, url)
  24. if mobj is None:
  25. raise ExtractorError(u'Invalid URL: %s' % url)
  26. # extract uploader (which is in the url)
  27. uploader = mobj.group(1)
  28. # extract simple title (uploader + slug of song title)
  29. slug_title = mobj.group(2)
  30. full_title = '%s/%s' % (uploader, slug_title)
  31. self.report_resolve(full_title)
  32. url = 'http://soundcloud.com/%s/%s' % (uploader, slug_title)
  33. resolv_url = 'http://api.soundcloud.com/resolve.json?url=' + url + '&client_id=b45b1aa10f1ac2941910a7f0d10f8e28'
  34. info_json = self._download_webpage(resolv_url, full_title, u'Downloading info JSON')
  35. info = json.loads(info_json)
  36. video_id = info['id']
  37. self.report_extraction(full_title)
  38. streams_url = 'https://api.sndcdn.com/i1/tracks/' + str(video_id) + '/streams?client_id=b45b1aa10f1ac2941910a7f0d10f8e28'
  39. stream_json = self._download_webpage(streams_url, full_title,
  40. u'Downloading stream definitions',
  41. u'unable to download stream definitions')
  42. streams = json.loads(stream_json)
  43. mediaURL = streams['http_mp3_128_url']
  44. upload_date = unified_strdate(info['created_at'])
  45. return [{
  46. 'id': info['id'],
  47. 'url': mediaURL,
  48. 'uploader': info['user']['username'],
  49. 'upload_date': upload_date,
  50. 'title': info['title'],
  51. 'ext': u'mp3',
  52. 'description': info['description'],
  53. }]
  54. class SoundcloudSetIE(InfoExtractor):
  55. """Information extractor for soundcloud.com sets
  56. To access the media, the uid of the song and a stream token
  57. must be extracted from the page source and the script must make
  58. a request to media.soundcloud.com/crossdomain.xml. Then
  59. the media can be grabbed by requesting from an url composed
  60. of the stream token and uid
  61. """
  62. _VALID_URL = r'^(?:https?://)?(?:www\.)?soundcloud\.com/([\w\d-]+)/sets/([\w\d-]+)'
  63. IE_NAME = u'soundcloud:set'
  64. def report_resolve(self, video_id):
  65. """Report information extraction."""
  66. self.to_screen(u'%s: Resolving id' % video_id)
  67. def _real_extract(self, url):
  68. mobj = re.match(self._VALID_URL, url)
  69. if mobj is None:
  70. raise ExtractorError(u'Invalid URL: %s' % url)
  71. # extract uploader (which is in the url)
  72. uploader = mobj.group(1)
  73. # extract simple title (uploader + slug of song title)
  74. slug_title = mobj.group(2)
  75. full_title = '%s/sets/%s' % (uploader, slug_title)
  76. self.report_resolve(full_title)
  77. url = 'http://soundcloud.com/%s/sets/%s' % (uploader, slug_title)
  78. resolv_url = 'http://api.soundcloud.com/resolve.json?url=' + url + '&client_id=b45b1aa10f1ac2941910a7f0d10f8e28'
  79. info_json = self._download_webpage(resolv_url, full_title)
  80. videos = []
  81. info = json.loads(info_json)
  82. if 'errors' in info:
  83. for err in info['errors']:
  84. self._downloader.report_error(u'unable to download video webpage: %s' % compat_str(err['error_message']))
  85. return
  86. self.report_extraction(full_title)
  87. for track in info['tracks']:
  88. video_id = track['id']
  89. streams_url = 'https://api.sndcdn.com/i1/tracks/' + str(video_id) + '/streams?client_id=b45b1aa10f1ac2941910a7f0d10f8e28'
  90. stream_json = self._download_webpage(streams_url, video_id, u'Downloading track info JSON')
  91. self.report_extraction(video_id)
  92. streams = json.loads(stream_json)
  93. mediaURL = streams['http_mp3_128_url']
  94. videos.append({
  95. 'id': video_id,
  96. 'url': mediaURL,
  97. 'uploader': track['user']['username'],
  98. 'upload_date': unified_strdate(track['created_at']),
  99. 'title': track['title'],
  100. 'ext': u'mp3',
  101. 'description': track['description'],
  102. })
  103. return videos