1 from __future__
import unicode_literals
7 from .common
import InfoExtractor
13 compat_urllib_parse_unquote
,
28 class MixcloudIE(InfoExtractor
):
29 _VALID_URL
= r
'https?://(?:(?:www|beta|m)\.)?mixcloud\.com/([^/]+)/(?!stream|uploads|favorites|listens|playlists)([^/]+)'
33 'url': 'http://www.mixcloud.com/dholbach/cryptkeeper/',
35 'id': 'dholbach-cryptkeeper',
37 'title': 'Cryptkeeper',
38 'description': 'After quite a long silence from myself, finally another Drum\'n\'Bass mix with my favourite current dance floor bangers.',
39 'uploader': 'Daniel Holbach',
40 'uploader_id': 'dholbach',
41 'thumbnail': r
're:https?://.*\.jpg',
45 'url': 'http://www.mixcloud.com/gillespeterson/caribou-7-inch-vinyl-mix-chat/',
47 'id': 'gillespeterson-caribou-7-inch-vinyl-mix-chat',
49 'title': 'Caribou 7 inch Vinyl Mix & Chat',
50 'description': 'md5:2b8aec6adce69f9d41724647c65875e8',
51 'uploader': 'Gilles Peterson Worldwide',
52 'uploader_id': 'gillespeterson',
53 'thumbnail': 're:https?://.*',
57 'url': 'https://beta.mixcloud.com/RedLightRadio/nosedrip-15-red-light-radio-01-18-2016/',
58 'only_matching': True,
62 def _decrypt_xor_cipher(key
, ciphertext
):
63 """Encrypt/Decrypt XOR cipher. Both ways are possible because it's XOR."""
65 compat_chr(compat_ord(ch
) ^
compat_ord(k
))
66 for ch
, k
in compat_zip(ciphertext
, itertools
.cycle(key
))])
68 def _real_extract(self
, url
):
69 mobj
= re
.match(self
._VALID
_URL
, url
)
70 uploader
= mobj
.group(1)
71 cloudcast_name
= mobj
.group(2)
72 track_id
= compat_urllib_parse_unquote('-'.join((uploader
, cloudcast_name
)))
74 webpage
= self
._download
_webpage
(url
, track_id
)
77 encrypted_play_info
= self
._search
_regex
(
78 r
'm-play-info="([^"]+)"', webpage
, 'play info', default
=None)
80 if encrypted_play_info
is not None:
82 encrypted_play_info
= compat_b64decode(encrypted_play_info
)
85 full_info_json
= self
._parse
_json
(self
._html
_search
_regex
(
86 r
'<script id="relay-data" type="text/x-mixcloud">([^<]+)</script>',
87 webpage
, 'play info'), 'play info')
88 for item
in full_info_json
:
90 item
, lambda x
: x
['cloudcast']['data']['cloudcastLookup'],
92 if try_get(item_data
, lambda x
: x
['streamInfo']['url']):
96 raise ExtractorError('Failed to extract matching stream info')
98 message
= self
._html
_search
_regex
(
99 r
'(?s)<div[^>]+class="global-message cloudcast-disabled-notice-light"[^>]*>(.+?)<(?:a|/div)',
100 webpage
, 'error message', default
=None)
102 js_url
= self
._search
_regex
(
103 r
'<script[^>]+\bsrc=["\"](https://(?:www\.)?mixcloud\.com/media/(?:js2/www_js_4|js/www)\.[^>]+\.js)',
105 js
= self
._download
_webpage
(js_url
, track_id
, 'Downloading JS')
106 # Known plaintext attack
107 if encrypted_play_info
:
108 kps
= ['{"stream_url":']
109 kpa_target
= encrypted_play_info
111 kps
= ['https://', 'http://']
112 kpa_target
= compat_b64decode(info_json
['streamInfo']['url'])
114 partial_key
= self
._decrypt
_xor
_cipher
(kpa_target
, kp
)
115 for quote
in ["'", '"']:
116 key
= self
._search
_regex
(
117 r
'{0}({1}[^{0}]*){0}'.format(quote
, re
.escape(partial_key
)),
118 js
, 'encryption key', default
=None)
125 raise ExtractorError('Failed to extract encryption key')
127 if encrypted_play_info
is not None:
128 play_info
= self
._parse
_json
(self
._decrypt
_xor
_cipher
(key
, encrypted_play_info
), 'play info')
129 if message
and 'stream_url' not in play_info
:
130 raise ExtractorError('%s said: %s' % (self
.IE_NAME
, message
), expected
=True)
131 song_url
= play_info
['stream_url']
133 'format_id': 'normal',
137 title
= self
._html
_search
_regex
(r
'm-title="([^"]+)"', webpage
, 'title')
138 thumbnail
= self
._proto
_relative
_url
(self
._html
_search
_regex
(
139 r
'm-thumbnail-url="([^"]+)"', webpage
, 'thumbnail', fatal
=False))
140 uploader
= self
._html
_search
_regex
(
141 r
'm-owner-name="([^"]+)"', webpage
, 'uploader', fatal
=False)
142 uploader_id
= self
._search
_regex
(
143 r
'\s+"profile": "([^"]+)",', webpage
, 'uploader id', fatal
=False)
144 description
= self
._og
_search
_description
(webpage
)
145 view_count
= str_to_int(self
._search
_regex
(
146 [r
'<meta itemprop="interactionCount" content="UserPlays:([0-9]+)"',
147 r
'/listeners/?">([0-9,.]+)</a>',
148 r
'(?:m|data)-tooltip=["\']([\d
,.]+) plays
'],
149 webpage, 'play count
', default=None))
152 title = info_json['name
']
154 'https
://thumbnailer
.mixcloud
.com
/unsafe
/600x600
/',
155 try_get(info_json, lambda x: x['picture
']['urlRoot
'], compat_str))
156 uploader = try_get(info_json, lambda x: x['owner
']['displayName
'])
157 uploader_id = try_get(info_json, lambda x: x['owner
']['username
'])
158 description = try_get(info_json, lambda x: x['description
'])
159 view_count = int_or_none(try_get(info_json, lambda x: x['plays
']))
161 stream_info = info_json['streamInfo
']
164 def decrypt_url(f_url):
165 for k in (key, 'IFYOUWANTTHEARTISTSTOGETPAIDDONOTDOWNLOADFROMMIXCLOUD
'):
166 decrypted_url = self._decrypt_xor_cipher(k, f_url)
167 if re.search(r'^https?
://[0-9A
-Za
-z
.]+/[0-9A
-Za
-z
/.?
=&_
-]+$
', decrypted_url):
170 for url_key in ('url
', 'hlsUrl
', 'dashUrl
'):
171 format_url = stream_info.get(url_key)
174 decrypted = decrypt_url(compat_b64decode(format_url))
177 if url_key == 'hlsUrl
':
178 formats.extend(self._extract_m3u8_formats(
179 decrypted, track_id, 'mp4
', entry_protocol='m3u8_native
',
180 m3u8_id='hls
', fatal=False))
181 elif url_key == 'dashUrl
':
182 formats.extend(self._extract_mpd_formats(
183 decrypted, track_id, mpd_id='dash
', fatal=False))
188 'downloader_options
': {
189 # Mixcloud starts throttling at >~5M
190 'http_chunk_size
': 5242880,
193 self._sort_formats(formats)
199 'description
': description,
200 'thumbnail
': thumbnail,
201 'uploader
': uploader,
202 'uploader_id
': uploader_id,
203 'view_count
': view_count,
207 class MixcloudPlaylistBaseIE(InfoExtractor):
210 def _find_urls_in_page(self, page):
211 for url in re.findall(r'm
-play
-button m
-url
="(?P<url>[^"]+)"', page):
212 yield self.url_result(
213 compat_urlparse.urljoin('https://www.mixcloud.com', clean_html(url)),
216 def _fetch_tracks_page(self, path, video_id, page_name, current_page, real_page_number=None):
217 real_page_number = real_page_number or current_page + 1
218 return self._download_webpage(
219 'https://www.mixcloud.com/%s/' % path, video_id,
220 note='Download %s (page %d)' % (page_name, current_page + 1),
221 errnote='Unable to download %s' % page_name,
222 query={'page': real_page_number, 'list': 'main', '_ajax': '1'},
223 headers={'X-Requested-With': 'XMLHttpRequest'})
225 def _tracks_page_func(self, page, video_id, page_name, current_page):
226 resp = self._fetch_tracks_page(page, video_id, page_name, current_page)
228 for item in self._find_urls_in_page(resp):
231 def _get_user_description(self, page_content):
232 return self._html_search_regex(
233 r'<div[^>]+class="profile
-bio
"[^>]*>(.+?)</div>',
234 page_content, 'user description', fatal=False)
237 class MixcloudUserIE(MixcloudPlaylistBaseIE):
238 _VALID_URL = r'https?://(?:www\.)?mixcloud\.com/(?P<user>[^/]+)/(?P<type>uploads|favorites|listens)?/?$'
239 IE_NAME = 'mixcloud:user'
242 'url': 'http://www.mixcloud.com/dholbach/',
244 'id': 'dholbach_uploads',
245 'title': 'Daniel Holbach (uploads)',
246 'description': 'md5:def36060ac8747b3aabca54924897e47',
248 'playlist_mincount': 11,
250 'url': 'http://www.mixcloud.com/dholbach/uploads/',
252 'id': 'dholbach_uploads',
253 'title': 'Daniel Holbach (uploads)',
254 'description': 'md5:def36060ac8747b3aabca54924897e47',
256 'playlist_mincount': 11,
258 'url': 'http://www.mixcloud.com/dholbach/favorites/',
260 'id': 'dholbach_favorites',
261 'title': 'Daniel Holbach (favorites)',
262 'description': 'md5:def36060ac8747b3aabca54924897e47',
265 'playlist_items': '1-100',
267 'playlist_mincount': 100,
269 'url': 'http://www.mixcloud.com/dholbach/listens/',
271 'id': 'dholbach_listens',
272 'title': 'Daniel Holbach (listens)',
273 'description': 'md5:def36060ac8747b3aabca54924897e47',
276 'playlist_items': '1-100',
278 'playlist_mincount': 100,
281 def _real_extract(self, url):
282 mobj = re.match(self._VALID_URL, url)
283 user_id = mobj.group('user')
284 list_type = mobj.group('type')
286 # if only a profile URL was supplied, default to download all uploads
287 if list_type is None:
288 list_type = 'uploads'
290 video_id = '%s_%s' % (user_id, list_type)
292 profile = self._download_webpage(
293 'https://www.mixcloud.com/%s/' % user_id, video_id,
294 note='Downloading user profile',
295 errnote='Unable to download user profile')
297 username = self._og_search_title(profile)
298 description = self._get_user_description(profile)
300 entries = OnDemandPagedList(
302 self._tracks_page_func,
303 '%s/%s' % (user_id, list_type), video_id, 'list of %s' % list_type),
306 return self.playlist_result(
307 entries, video_id, '%s (%s)' % (username, list_type), description)
310 class MixcloudPlaylistIE(MixcloudPlaylistBaseIE):
311 _VALID_URL = r'https?://(?:www\.)?mixcloud\.com/(?P<user>[^/]+)/playlists/(?P<playlist>[^/]+)/?$'
312 IE_NAME = 'mixcloud:playlist'
315 'url': 'https://www.mixcloud.com/RedBullThre3style/playlists/tokyo-finalists-2015/',
317 'id': 'RedBullThre3style_tokyo-finalists-2015',
318 'title': 'National Champions 2015',
319 'description': 'md5:6ff5fb01ac76a31abc9b3939c16243a3',
321 'playlist_mincount': 16,
323 'url': 'https://www.mixcloud.com/maxvibes/playlists/jazzcat-on-ness-radio/',
324 'only_matching': True,
327 def _real_extract(self, url):
328 mobj = re.match(self._VALID_URL, url)
329 user_id = mobj.group('user')
330 playlist_id = mobj.group('playlist')
331 video_id = '%s_%s' % (user_id, playlist_id)
333 webpage = self._download_webpage(
335 note='Downloading playlist page',
336 errnote='Unable to download playlist page')
338 title = self._html_search_regex(
339 r'<a[^>]+class="parent active
"[^>]*><b>\d+</b><span[^>]*>([^<]+)',
340 webpage, 'playlist title',
341 default=None) or self._og_search_title(webpage, fatal=False)
342 description = self._get_user_description(webpage)
344 entries = OnDemandPagedList(
346 self._tracks_page_func,
347 '%s/playlists/%s' % (user_id, playlist_id), video_id, 'tracklist'),
350 return self.playlist_result(entries, video_id, title, description)
353 class MixcloudStreamIE(MixcloudPlaylistBaseIE):
354 _VALID_URL = r'https?://(?:www\.)?mixcloud\.com/(?P<id>[^/]+)/stream/?$'
355 IE_NAME = 'mixcloud:stream'
358 'url': 'https://www.mixcloud.com/FirstEar/stream/',
361 'title': 'First Ear',
362 'description': 'Curators of good music\nfirstearmusic.com',
364 'playlist_mincount': 192,
367 def _real_extract(self, url):
368 user_id = self._match_id(url)
370 webpage = self._download_webpage(url, user_id)
375 def _handle_page(page):
376 entries.extend(self._find_urls_in_page(page))
377 return self._search_regex(
378 r'm-next-page-url="([^
"]+)"', page,
379 'next page URL
', default=None)
381 next_page_url = _handle_page(webpage)
383 for idx in itertools.count(0):
384 if not next_page_url or prev_page_url == next_page_url:
387 prev_page_url = next_page_url
388 current_page = int(self._search_regex(
389 r'\?page
=(\d
+)', next_page_url, 'next page number
'))
391 next_page_url = _handle_page(self._fetch_tracks_page(
392 '%s/stream
' % user_id, user_id, 'stream
', idx,
393 real_page_number=current_page))
395 username = self._og_search_title(webpage)
396 description = self._get_user_description(webpage)
398 return self.playlist_result(entries, user_id, username, description)