mixcloud.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. from __future__ import unicode_literals
  2. import base64
  3. import functools
  4. import re
  5. from .common import InfoExtractor
  6. from ..compat import (
  7. compat_chr,
  8. compat_ord,
  9. compat_urllib_parse_unquote,
  10. compat_urlparse,
  11. )
  12. from ..utils import (
  13. clean_html,
  14. ExtractorError,
  15. OnDemandPagedList,
  16. parse_count,
  17. str_to_int,
  18. )
  19. class MixcloudIE(InfoExtractor):
  20. _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/([^/]+)/(?!stream|uploads|favorites|listens|playlists)([^/]+)'
  21. IE_NAME = 'mixcloud'
  22. _TESTS = [{
  23. 'url': 'http://www.mixcloud.com/dholbach/cryptkeeper/',
  24. 'info_dict': {
  25. 'id': 'dholbach-cryptkeeper',
  26. 'ext': 'm4a',
  27. 'title': 'Cryptkeeper',
  28. 'description': 'After quite a long silence from myself, finally another Drum\'n\'Bass mix with my favourite current dance floor bangers.',
  29. 'uploader': 'Daniel Holbach',
  30. 'uploader_id': 'dholbach',
  31. 'thumbnail': 're:https?://.*\.jpg',
  32. 'view_count': int,
  33. 'like_count': int,
  34. },
  35. }, {
  36. 'url': 'http://www.mixcloud.com/gillespeterson/caribou-7-inch-vinyl-mix-chat/',
  37. 'info_dict': {
  38. 'id': 'gillespeterson-caribou-7-inch-vinyl-mix-chat',
  39. 'ext': 'mp3',
  40. 'title': 'Caribou 7 inch Vinyl Mix & Chat',
  41. 'description': 'md5:2b8aec6adce69f9d41724647c65875e8',
  42. 'uploader': 'Gilles Peterson Worldwide',
  43. 'uploader_id': 'gillespeterson',
  44. 'thumbnail': 're:https?://.*',
  45. 'view_count': int,
  46. 'like_count': int,
  47. },
  48. }]
  49. # See https://www.mixcloud.com/media/js2/www_js_2.9e23256562c080482435196ca3975ab5.js
  50. @staticmethod
  51. def _decrypt_play_info(play_info):
  52. KEY = 'pleasedontdownloadourmusictheartistswontgetpaid'
  53. play_info = base64.b64decode(play_info.encode('ascii'))
  54. return ''.join([
  55. compat_chr(compat_ord(ch) ^ compat_ord(KEY[idx % len(KEY)]))
  56. for idx, ch in enumerate(play_info)])
  57. def _real_extract(self, url):
  58. mobj = re.match(self._VALID_URL, url)
  59. uploader = mobj.group(1)
  60. cloudcast_name = mobj.group(2)
  61. track_id = compat_urllib_parse_unquote('-'.join((uploader, cloudcast_name)))
  62. webpage = self._download_webpage(url, track_id)
  63. message = self._html_search_regex(
  64. r'(?s)<div[^>]+class="global-message cloudcast-disabled-notice-light"[^>]*>(.+?)<(?:a|/div)',
  65. webpage, 'error message', default=None)
  66. encrypted_play_info = self._search_regex(
  67. r'm-play-info="([^"]+)"', webpage, 'play info')
  68. play_info = self._parse_json(
  69. self._decrypt_play_info(encrypted_play_info), track_id)
  70. if message and 'stream_url' not in play_info:
  71. raise ExtractorError('%s said: %s' % (self.IE_NAME, message), expected=True)
  72. song_url = play_info['stream_url']
  73. PREFIX = (
  74. r'm-play-on-spacebar[^>]+'
  75. r'(?:\s+[a-zA-Z0-9-]+(?:="[^"]+")?)*?\s+')
  76. title = self._html_search_regex(
  77. PREFIX + r'm-title="([^"]+)"', webpage, 'title')
  78. thumbnail = self._proto_relative_url(self._html_search_regex(
  79. PREFIX + r'm-thumbnail-url="([^"]+)"', webpage, 'thumbnail',
  80. fatal=False))
  81. uploader = self._html_search_regex(
  82. PREFIX + r'm-owner-name="([^"]+)"',
  83. webpage, 'uploader', fatal=False)
  84. uploader_id = self._search_regex(
  85. r'\s+"profile": "([^"]+)",', webpage, 'uploader id', fatal=False)
  86. description = self._og_search_description(webpage)
  87. like_count = parse_count(self._search_regex(
  88. r'\bbutton-favorite[^>]+>.*?<span[^>]+class=["\']toggle-number[^>]+>\s*([^<]+)',
  89. webpage, 'like count', fatal=False))
  90. view_count = str_to_int(self._search_regex(
  91. [r'<meta itemprop="interactionCount" content="UserPlays:([0-9]+)"',
  92. r'/listeners/?">([0-9,.]+)</a>'],
  93. webpage, 'play count', fatal=False))
  94. return {
  95. 'id': track_id,
  96. 'title': title,
  97. 'url': song_url,
  98. 'description': description,
  99. 'thumbnail': thumbnail,
  100. 'uploader': uploader,
  101. 'uploader_id': uploader_id,
  102. 'view_count': view_count,
  103. 'like_count': like_count,
  104. }
  105. class MixcloudPlaylistBaseIE(InfoExtractor):
  106. _PAGE_SIZE = 24
  107. def _fetch_tracks_page(self, path, video_id, page_name, current_page):
  108. resp = self._download_webpage(
  109. 'https://www.mixcloud.com/%s/' % path, video_id,
  110. note='Download %s (page %d)' % (page_name, current_page + 1),
  111. errnote='Unable to download %s' % page_name,
  112. query={'page': (current_page + 1), 'list': 'main', '_ajax': '1'},
  113. headers={'X-Requested-With': 'XMLHttpRequest'})
  114. for url in re.findall(r'm-play-button m-url="(?P<url>[^"]+)"', resp):
  115. yield self.url_result(
  116. compat_urlparse.urljoin('https://www.mixcloud.com', clean_html(url)),
  117. MixcloudIE.ie_key())
  118. def _get_user_description(self, page_content):
  119. return self._html_search_regex(
  120. r'<div[^>]+class="description-text"[^>]*>(.+?)</div>',
  121. page_content, 'user description', fatal=False)
  122. class MixcloudUserIE(MixcloudPlaylistBaseIE):
  123. _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/(?P<user>[^/]+)/(?P<type>uploads|favorites|listens)?/?$'
  124. IE_NAME = 'mixcloud:user'
  125. _TESTS = [{
  126. 'url': 'http://www.mixcloud.com/dholbach/',
  127. 'info_dict': {
  128. 'id': 'dholbach_uploads',
  129. 'title': 'Daniel Holbach (uploads)',
  130. 'description': 'md5:327af72d1efeb404a8216c27240d1370',
  131. },
  132. 'playlist_mincount': 11,
  133. }, {
  134. 'url': 'http://www.mixcloud.com/dholbach/uploads/',
  135. 'info_dict': {
  136. 'id': 'dholbach_uploads',
  137. 'title': 'Daniel Holbach (uploads)',
  138. 'description': 'md5:327af72d1efeb404a8216c27240d1370',
  139. },
  140. 'playlist_mincount': 11,
  141. }, {
  142. 'url': 'http://www.mixcloud.com/dholbach/favorites/',
  143. 'info_dict': {
  144. 'id': 'dholbach_favorites',
  145. 'title': 'Daniel Holbach (favorites)',
  146. 'description': 'md5:327af72d1efeb404a8216c27240d1370',
  147. },
  148. 'params': {
  149. 'playlist_items': '1-100',
  150. },
  151. 'playlist_mincount': 100,
  152. }, {
  153. 'url': 'http://www.mixcloud.com/dholbach/listens/',
  154. 'info_dict': {
  155. 'id': 'dholbach_listens',
  156. 'title': 'Daniel Holbach (listens)',
  157. 'description': 'md5:327af72d1efeb404a8216c27240d1370',
  158. },
  159. 'params': {
  160. 'playlist_items': '1-100',
  161. },
  162. 'playlist_mincount': 100,
  163. }]
  164. def _real_extract(self, url):
  165. mobj = re.match(self._VALID_URL, url)
  166. user_id = mobj.group('user')
  167. list_type = mobj.group('type')
  168. # if only a profile URL was supplied, default to download all uploads
  169. if list_type is None:
  170. list_type = 'uploads'
  171. video_id = '%s_%s' % (user_id, list_type)
  172. profile = self._download_webpage(
  173. 'https://www.mixcloud.com/%s/' % user_id, video_id,
  174. note='Downloading user profile',
  175. errnote='Unable to download user profile')
  176. username = self._og_search_title(profile)
  177. description = self._get_user_description(profile)
  178. entries = OnDemandPagedList(
  179. functools.partial(
  180. self._fetch_tracks_page,
  181. '%s/%s' % (user_id, list_type), video_id, 'list of %s' % list_type),
  182. self._PAGE_SIZE, use_cache=True)
  183. return self.playlist_result(
  184. entries, video_id, '%s (%s)' % (username, list_type), description)
  185. class MixcloudPlaylistIE(MixcloudPlaylistBaseIE):
  186. _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/(?P<user>[^/]+)/playlists/(?P<playlist>[^/]+)/?$'
  187. IE_NAME = 'mixcloud:playlist'
  188. _TESTS = [{
  189. 'url': 'https://www.mixcloud.com/RedBullThre3style/playlists/tokyo-finalists-2015/',
  190. 'info_dict': {
  191. 'id': 'RedBullThre3style_tokyo-finalists-2015',
  192. 'title': 'National Champions 2015',
  193. 'description': 'md5:6ff5fb01ac76a31abc9b3939c16243a3',
  194. },
  195. 'playlist_mincount': 16,
  196. }, {
  197. 'url': 'https://www.mixcloud.com/maxvibes/playlists/jazzcat-on-ness-radio/',
  198. 'info_dict': {
  199. 'id': 'maxvibes_jazzcat-on-ness-radio',
  200. 'title': 'Jazzcat on Ness Radio',
  201. 'description': 'md5:7bbbf0d6359a0b8cda85224be0f8f263',
  202. },
  203. 'playlist_mincount': 23
  204. }]
  205. def _real_extract(self, url):
  206. mobj = re.match(self._VALID_URL, url)
  207. user_id = mobj.group('user')
  208. playlist_id = mobj.group('playlist')
  209. video_id = '%s_%s' % (user_id, playlist_id)
  210. profile = self._download_webpage(
  211. url, user_id,
  212. note='Downloading playlist page',
  213. errnote='Unable to download playlist page')
  214. description = self._get_user_description(profile)
  215. playlist_title = self._html_search_regex(
  216. r'<span[^>]+class="[^"]*list-playlist-title[^"]*"[^>]*>(.*?)</span>',
  217. profile, 'playlist title')
  218. entries = OnDemandPagedList(
  219. functools.partial(
  220. self._fetch_tracks_page,
  221. '%s/playlists/%s' % (user_id, playlist_id), video_id, 'tracklist'),
  222. self._PAGE_SIZE)
  223. return self.playlist_result(entries, video_id, playlist_title, description)