1 from __future__ import unicode_literals
8 from .common import InfoExtractor
12 compat_urllib_parse_unquote,
24 class MixcloudIE(InfoExtractor):
25 _VALID_URL = r'https?://(?:(?:www|beta|m)\.)?mixcloud\.com/([^/]+)/(?!stream|uploads|favorites|listens|playlists)([^/]+)'
29 'url': 'http://www.mixcloud.com/dholbach/cryptkeeper/',
31 'id': 'dholbach-cryptkeeper',
33 'title': 'Cryptkeeper',
34 'description': 'After quite a long silence from myself, finally another Drum\'n\'Bass mix with my favourite current dance floor bangers.',
35 'uploader': 'Daniel Holbach',
36 'uploader_id': 'dholbach',
37 'thumbnail': r're:https?://.*\.jpg',
41 'url': 'http://www.mixcloud.com/gillespeterson/caribou-7-inch-vinyl-mix-chat/',
43 'id': 'gillespeterson-caribou-7-inch-vinyl-mix-chat',
45 'title': 'Caribou 7 inch Vinyl Mix & Chat',
46 'description': 'md5:2b8aec6adce69f9d41724647c65875e8',
47 'uploader': 'Gilles Peterson Worldwide',
48 'uploader_id': 'gillespeterson',
49 'thumbnail': 're:https?://.*',
53 'url': 'https://beta.mixcloud.com/RedLightRadio/nosedrip-15-red-light-radio-01-18-2016/',
54 'only_matching': True,
58 def _decrypt_xor_cipher(key, ciphertext):
59 """Encrypt/Decrypt XOR cipher. Both ways are possible because it's XOR."""
61 compat_chr(compat_ord(ch) ^ compat_ord(k))
62 for ch, k in compat_zip(ciphertext, itertools.cycle(key))])
65 def _decrypt_and_extend(stream_info, url_key, getter, key, formats):
66 maybe_url = stream_info.get(url_key)
67 if maybe_url is not None:
68 decrypted = MixcloudIE._decrypt_xor_cipher(key, base64.b64decode(maybe_url))
69 formats.extend(getter(decrypted))
71 def _real_extract(self, url):
72 mobj = re.match(self._VALID_URL, url)
73 uploader = mobj.group(1)
74 cloudcast_name = mobj.group(2)
75 track_id = compat_urllib_parse_unquote('-'.join((uploader, cloudcast_name)))
77 webpage = self._download_webpage(url, track_id)
80 encrypted_play_info = self._search_regex(
81 r'm-play-info="([^"]+)"', webpage, 'play info', default=None)
83 if encrypted_play_info is not None:
85 encrypted_play_info = base64.b64decode(encrypted_play_info)
88 full_info_json = self._parse_json(self._html_search_regex(
89 r'<script id="relay-data" type="text/x-mixcloud">([^<]+)</script>', webpage, 'play info'), 'play info')
90 for item in full_info_json:
91 item_data = try_get(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)',
104 webpage, 'js url', default=None)
106 js_url = self._search_regex(
107 r'<script[^>]+\bsrc=["\"](https://(?:www\.)?mixcloud\.com/media/js/www\.[^>]+\.js)',
109 js = self._download_webpage(js_url, track_id)
110 # Known plaintext attack
111 if encrypted_play_info:
112 kps = ['{"stream_url":']
113 kpa_target = encrypted_play_info
115 kps = ['https://', 'http://']
116 kpa_target = base64.b64decode(info_json['streamInfo']['url'])
118 partial_key = self._decrypt_xor_cipher(kpa_target, kp)
119 for quote in ["'", '"']:
120 key = self._search_regex(r'{0}({1}[^{0}]*){0}'.format(quote, re.escape(partial_key)), js,
121 "encryption key", default=None)
128 raise ExtractorError('Failed to extract encryption key')
130 if encrypted_play_info is not None:
131 play_info = self._parse_json(self._decrypt_xor_cipher(key, encrypted_play_info), 'play info')
132 if message and 'stream_url' not in play_info:
133 raise ExtractorError('%s said: %s' % (self.IE_NAME, message), expected=True)
134 song_url = play_info['stream_url']
136 'format_id': 'normal',
140 title = self._html_search_regex(r'm-title="([^"]+)"', webpage, 'title')
141 thumbnail = self._proto_relative_url(self._html_search_regex(
142 r'm-thumbnail-url="([^"]+)"', webpage, 'thumbnail', fatal=False))
143 uploader = self._html_search_regex(
144 r'm-owner-name="([^"]+)"', webpage, 'uploader', fatal=False)
145 uploader_id = self._search_regex(
146 r'\s+"profile": "([^"]+)",', webpage, 'uploader id', fatal=False)
147 description = self._og_search_description(webpage)
148 view_count = str_to_int(self._search_regex(
149 [r'<meta itemprop="interactionCount" content="UserPlays:([0-9]+)"',
150 r'/listeners/?">([0-9,.]+)</a>',
151 r'(?:m|data)-tooltip=["\']([\d,.]+) plays'],
152 webpage, 'play count', default=None))
155 title = info_json['name']
156 thumbnail = try_get(info_json,
157 lambda x: 'https://thumbnailer.mixcloud.com/unsafe/600x600/' + x['picture']['urlRoot'])
158 uploader = try_get(info_json, lambda x: x['owner']['displayName'])
159 uploader_id = try_get(info_json, lambda x: x['owner']['username'])
160 description = try_get(info_json, lambda x: x['description'])
161 view_count = try_get(info_json, lambda x: x['plays'])
163 stream_info = info_json['streamInfo']
165 self._decrypt_and_extend(stream_info, 'url', lambda x: [{
166 'format_id': 'normal',
169 self._decrypt_and_extend(stream_info, 'hlsUrl', lambda x: self._extract_m3u8_formats(x, title), key,
171 self._decrypt_and_extend(stream_info, 'dashUrl', lambda x: self._extract_mpd_formats(x, title), key,
178 'description': description,
179 'thumbnail': thumbnail,
180 'uploader': uploader,
181 'uploader_id': uploader_id,
182 'view_count': view_count,
186 class MixcloudPlaylistBaseIE(InfoExtractor):
189 def _find_urls_in_page(self, page):
190 for url in re.findall(r'm-play-button m-url="(?P<url>[^"]+)"', page):
191 yield self.url_result(
192 compat_urlparse.urljoin('https://www.mixcloud.com', clean_html(url)),
195 def _fetch_tracks_page(self, path, video_id, page_name, current_page, real_page_number=None):
196 real_page_number = real_page_number or current_page + 1
197 return self._download_webpage(
198 'https://www.mixcloud.com/%s/' % path, video_id,
199 note='Download %s (page %d)' % (page_name, current_page + 1),
200 errnote='Unable to download %s' % page_name,
201 query={'page': real_page_number, 'list': 'main', '_ajax': '1'},
202 headers={'X-Requested-With': 'XMLHttpRequest'})
204 def _tracks_page_func(self, page, video_id, page_name, current_page):
205 resp = self._fetch_tracks_page(page, video_id, page_name, current_page)
207 for item in self._find_urls_in_page(resp):
210 def _get_user_description(self, page_content):
211 return self._html_search_regex(
212 r'<div[^>]+class="profile-bio"[^>]*>(.+?)</div>',
213 page_content, 'user description', fatal=False)
216 class MixcloudUserIE(MixcloudPlaylistBaseIE):
217 _VALID_URL = r'https?://(?:www\.)?mixcloud\.com/(?P<user>[^/]+)/(?P<type>uploads|favorites|listens)?/?$'
218 IE_NAME = 'mixcloud:user'
221 'url': 'http://www.mixcloud.com/dholbach/',
223 'id': 'dholbach_uploads',
224 'title': 'Daniel Holbach (uploads)',
225 'description': 'md5:def36060ac8747b3aabca54924897e47',
227 'playlist_mincount': 11,
229 'url': 'http://www.mixcloud.com/dholbach/uploads/',
231 'id': 'dholbach_uploads',
232 'title': 'Daniel Holbach (uploads)',
233 'description': 'md5:def36060ac8747b3aabca54924897e47',
235 'playlist_mincount': 11,
237 'url': 'http://www.mixcloud.com/dholbach/favorites/',
239 'id': 'dholbach_favorites',
240 'title': 'Daniel Holbach (favorites)',
241 'description': 'md5:def36060ac8747b3aabca54924897e47',
244 'playlist_items': '1-100',
246 'playlist_mincount': 100,
248 'url': 'http://www.mixcloud.com/dholbach/listens/',
250 'id': 'dholbach_listens',
251 'title': 'Daniel Holbach (listens)',
252 'description': 'md5:def36060ac8747b3aabca54924897e47',
255 'playlist_items': '1-100',
257 'playlist_mincount': 100,
260 def _real_extract(self, url):
261 mobj = re.match(self._VALID_URL, url)
262 user_id = mobj.group('user')
263 list_type = mobj.group('type')
265 # if only a profile URL was supplied, default to download all uploads
266 if list_type is None:
267 list_type = 'uploads'
269 video_id = '%s_%s' % (user_id, list_type)
271 profile = self._download_webpage(
272 'https://www.mixcloud.com/%s/' % user_id, video_id,
273 note='Downloading user profile',
274 errnote='Unable to download user profile')
276 username = self._og_search_title(profile)
277 description = self._get_user_description(profile)
279 entries = OnDemandPagedList(
281 self._tracks_page_func,
282 '%s/%s' % (user_id, list_type), video_id, 'list of %s' % list_type),
283 self._PAGE_SIZE, use_cache=True)
285 return self.playlist_result(
286 entries, video_id, '%s (%s)' % (username, list_type), description)
289 class MixcloudPlaylistIE(MixcloudPlaylistBaseIE):
290 _VALID_URL = r'https?://(?:www\.)?mixcloud\.com/(?P<user>[^/]+)/playlists/(?P<playlist>[^/]+)/?$'
291 IE_NAME = 'mixcloud:playlist'
294 'url': 'https://www.mixcloud.com/RedBullThre3style/playlists/tokyo-finalists-2015/',
296 'id': 'RedBullThre3style_tokyo-finalists-2015',
297 'title': 'National Champions 2015',
298 'description': 'md5:6ff5fb01ac76a31abc9b3939c16243a3',
300 'playlist_mincount': 16,
302 'url': 'https://www.mixcloud.com/maxvibes/playlists/jazzcat-on-ness-radio/',
303 'only_matching': True,
306 def _real_extract(self, url):
307 mobj = re.match(self._VALID_URL, url)
308 user_id = mobj.group('user')
309 playlist_id = mobj.group('playlist')
310 video_id = '%s_%s' % (user_id, playlist_id)
312 webpage = self._download_webpage(
314 note='Downloading playlist page',
315 errnote='Unable to download playlist page')
317 title = self._html_search_regex(
318 r'<a[^>]+class="parent active"[^>]*><b>\d+</b><span[^>]*>([^<]+)',
319 webpage, 'playlist title',
320 default=None) or self._og_search_title(webpage, fatal=False)
321 description = self._get_user_description(webpage)
323 entries = OnDemandPagedList(
325 self._tracks_page_func,
326 '%s/playlists/%s' % (user_id, playlist_id), video_id, 'tracklist'),
329 return self.playlist_result(entries, video_id, title, description)
332 class MixcloudStreamIE(MixcloudPlaylistBaseIE):
333 _VALID_URL = r'https?://(?:www\.)?mixcloud\.com/(?P<id>[^/]+)/stream/?$'
334 IE_NAME = 'mixcloud:stream'
337 'url': 'https://www.mixcloud.com/FirstEar/stream/',
340 'title': 'First Ear',
341 'description': 'Curators of good music\nfirstearmusic.com',
343 'playlist_mincount': 192,
346 def _real_extract(self, url):
347 user_id = self._match_id(url)
349 webpage = self._download_webpage(url, user_id)
354 def _handle_page(page):
355 entries.extend(self._find_urls_in_page(page))
356 return self._search_regex(
357 r'm-next-page-url="([^"]+)"', page,
358 'next page URL', default=None)
360 next_page_url = _handle_page(webpage)
362 for idx in itertools.count(0):
363 if not next_page_url or prev_page_url == next_page_url:
366 prev_page_url = next_page_url
367 current_page = int(self._search_regex(
368 r'\?page=(\d+)', next_page_url, 'next page number'))
370 next_page_url = _handle_page(self._fetch_tracks_page(
371 '%s/stream' % user_id, user_id, 'stream', idx,
372 real_page_number=current_page))
374 username = self._og_search_title(webpage)
375 description = self._get_user_description(webpage)
377 return self.playlist_result(entries, user_id, username, description)