1 from __future__ import unicode_literals
8 from .common import InfoExtractor
13 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 'return { requestAnimationFrame: function(callback) { callback(); }, innerHeight: 500 };',
59 'pleasedontdownloadourmusictheartistswontgetpaid',
60 'window.addEventListener = window.addEventListener || function() {};',
61 '(function() { return new Date().toLocaleDateString(); })()'
65 # See https://www.mixcloud.com/media/js2/www_js_2.9e23256562c080482435196ca3975ab5.js
66 def _decrypt_play_info(self, play_info, video_id):
67 play_info = base64.b64decode(play_info.encode('ascii'))
68 for num, key in enumerate(self._keys, start=1):
70 return self._parse_json(
72 compat_chr(compat_ord(ch) ^ compat_ord(key[idx % len(key)]))
73 for idx, ch in enumerate(play_info)]),
75 except ExtractorError:
76 if num == len(self._keys):
79 def _real_extract(self, url):
80 mobj = re.match(self._VALID_URL, url)
81 uploader = mobj.group(1)
82 cloudcast_name = mobj.group(2)
83 track_id = compat_urllib_parse_unquote('-'.join((uploader, cloudcast_name)))
85 webpage = self._download_webpage(url, track_id)
87 if not self._current_key:
88 js_url = self._search_regex(
89 r'<script[^>]+\bsrc=["\"](https://(?:www\.)?mixcloud\.com/media/js2/www_js_4\.[^>]+\.js)',
90 webpage, 'js url', default=None)
92 js = self._download_webpage(js_url, track_id, fatal=False)
94 key = self._search_regex(
95 r'player\s*:\s*{.*?\bvalue\s*:\s*(["\'])(?P<key>(?:(?!\1).)+)\1',
96 js, 'key', default=None, group='key')
97 if key and isinstance(key, compat_str):
98 self._keys.insert(0, key)
99 self._current_key = key
101 message = self._html_search_regex(
102 r'(?s)<div[^>]+class="global-message cloudcast-disabled-notice-light"[^>]*>(.+?)<(?:a|/div)',
103 webpage, 'error message', default=None)
105 encrypted_play_info = self._search_regex(
106 r'm-play-info="([^"]+)"', webpage, 'play info')
108 play_info = self._decrypt_play_info(encrypted_play_info, track_id)
110 if message and 'stream_url' not in play_info:
111 raise ExtractorError('%s said: %s' % (self.IE_NAME, message), expected=True)
113 song_url = play_info['stream_url']
115 title = self._html_search_regex(r'm-title="([^"]+)"', webpage, 'title')
116 thumbnail = self._proto_relative_url(self._html_search_regex(
117 r'm-thumbnail-url="([^"]+)"', webpage, 'thumbnail', fatal=False))
118 uploader = self._html_search_regex(
119 r'm-owner-name="([^"]+)"', webpage, 'uploader', fatal=False)
120 uploader_id = self._search_regex(
121 r'\s+"profile": "([^"]+)",', webpage, 'uploader id', fatal=False)
122 description = self._og_search_description(webpage)
123 view_count = str_to_int(self._search_regex(
124 [r'<meta itemprop="interactionCount" content="UserPlays:([0-9]+)"',
125 r'/listeners/?">([0-9,.]+)</a>',
126 r'(?:m|data)-tooltip=["\']([\d,.]+) plays'],
127 webpage, 'play count', default=None))
133 'description': description,
134 'thumbnail': thumbnail,
135 'uploader': uploader,
136 'uploader_id': uploader_id,
137 'view_count': view_count,
141 class MixcloudPlaylistBaseIE(InfoExtractor):
144 def _find_urls_in_page(self, page):
145 for url in re.findall(r'm-play-button m-url="(?P<url>[^"]+)"', page):
146 yield self.url_result(
147 compat_urlparse.urljoin('https://www.mixcloud.com', clean_html(url)),
150 def _fetch_tracks_page(self, path, video_id, page_name, current_page, real_page_number=None):
151 real_page_number = real_page_number or current_page + 1
152 return self._download_webpage(
153 'https://www.mixcloud.com/%s/' % path, video_id,
154 note='Download %s (page %d)' % (page_name, current_page + 1),
155 errnote='Unable to download %s' % page_name,
156 query={'page': real_page_number, 'list': 'main', '_ajax': '1'},
157 headers={'X-Requested-With': 'XMLHttpRequest'})
159 def _tracks_page_func(self, page, video_id, page_name, current_page):
160 resp = self._fetch_tracks_page(page, video_id, page_name, current_page)
162 for item in self._find_urls_in_page(resp):
165 def _get_user_description(self, page_content):
166 return self._html_search_regex(
167 r'<div[^>]+class="profile-bio"[^>]*>(.+?)</div>',
168 page_content, 'user description', fatal=False)
171 class MixcloudUserIE(MixcloudPlaylistBaseIE):
172 _VALID_URL = r'https?://(?:www\.)?mixcloud\.com/(?P<user>[^/]+)/(?P<type>uploads|favorites|listens)?/?$'
173 IE_NAME = 'mixcloud:user'
176 'url': 'http://www.mixcloud.com/dholbach/',
178 'id': 'dholbach_uploads',
179 'title': 'Daniel Holbach (uploads)',
180 'description': 'md5:def36060ac8747b3aabca54924897e47',
182 'playlist_mincount': 11,
184 'url': 'http://www.mixcloud.com/dholbach/uploads/',
186 'id': 'dholbach_uploads',
187 'title': 'Daniel Holbach (uploads)',
188 'description': 'md5:def36060ac8747b3aabca54924897e47',
190 'playlist_mincount': 11,
192 'url': 'http://www.mixcloud.com/dholbach/favorites/',
194 'id': 'dholbach_favorites',
195 'title': 'Daniel Holbach (favorites)',
196 'description': 'md5:def36060ac8747b3aabca54924897e47',
199 'playlist_items': '1-100',
201 'playlist_mincount': 100,
203 'url': 'http://www.mixcloud.com/dholbach/listens/',
205 'id': 'dholbach_listens',
206 'title': 'Daniel Holbach (listens)',
207 'description': 'md5:def36060ac8747b3aabca54924897e47',
210 'playlist_items': '1-100',
212 'playlist_mincount': 100,
215 def _real_extract(self, url):
216 mobj = re.match(self._VALID_URL, url)
217 user_id = mobj.group('user')
218 list_type = mobj.group('type')
220 # if only a profile URL was supplied, default to download all uploads
221 if list_type is None:
222 list_type = 'uploads'
224 video_id = '%s_%s' % (user_id, list_type)
226 profile = self._download_webpage(
227 'https://www.mixcloud.com/%s/' % user_id, video_id,
228 note='Downloading user profile',
229 errnote='Unable to download user profile')
231 username = self._og_search_title(profile)
232 description = self._get_user_description(profile)
234 entries = OnDemandPagedList(
236 self._tracks_page_func,
237 '%s/%s' % (user_id, list_type), video_id, 'list of %s' % list_type),
238 self._PAGE_SIZE, use_cache=True)
240 return self.playlist_result(
241 entries, video_id, '%s (%s)' % (username, list_type), description)
244 class MixcloudPlaylistIE(MixcloudPlaylistBaseIE):
245 _VALID_URL = r'https?://(?:www\.)?mixcloud\.com/(?P<user>[^/]+)/playlists/(?P<playlist>[^/]+)/?$'
246 IE_NAME = 'mixcloud:playlist'
249 'url': 'https://www.mixcloud.com/RedBullThre3style/playlists/tokyo-finalists-2015/',
251 'id': 'RedBullThre3style_tokyo-finalists-2015',
252 'title': 'National Champions 2015',
253 'description': 'md5:6ff5fb01ac76a31abc9b3939c16243a3',
255 'playlist_mincount': 16,
257 'url': 'https://www.mixcloud.com/maxvibes/playlists/jazzcat-on-ness-radio/',
258 'only_matching': True,
261 def _real_extract(self, url):
262 mobj = re.match(self._VALID_URL, url)
263 user_id = mobj.group('user')
264 playlist_id = mobj.group('playlist')
265 video_id = '%s_%s' % (user_id, playlist_id)
267 webpage = self._download_webpage(
269 note='Downloading playlist page',
270 errnote='Unable to download playlist page')
272 title = self._html_search_regex(
273 r'<a[^>]+class="parent active"[^>]*><b>\d+</b><span[^>]*>([^<]+)',
274 webpage, 'playlist title',
275 default=None) or self._og_search_title(webpage, fatal=False)
276 description = self._get_user_description(webpage)
278 entries = OnDemandPagedList(
280 self._tracks_page_func,
281 '%s/playlists/%s' % (user_id, playlist_id), video_id, 'tracklist'),
284 return self.playlist_result(entries, video_id, title, description)
287 class MixcloudStreamIE(MixcloudPlaylistBaseIE):
288 _VALID_URL = r'https?://(?:www\.)?mixcloud\.com/(?P<id>[^/]+)/stream/?$'
289 IE_NAME = 'mixcloud:stream'
292 'url': 'https://www.mixcloud.com/FirstEar/stream/',
295 'title': 'First Ear',
296 'description': 'Curators of good music\nfirstearmusic.com',
298 'playlist_mincount': 192,
301 def _real_extract(self, url):
302 user_id = self._match_id(url)
304 webpage = self._download_webpage(url, user_id)
309 def _handle_page(page):
310 entries.extend(self._find_urls_in_page(page))
311 return self._search_regex(
312 r'm-next-page-url="([^"]+)"', page,
313 'next page URL', default=None)
315 next_page_url = _handle_page(webpage)
317 for idx in itertools.count(0):
318 if not next_page_url or prev_page_url == next_page_url:
321 prev_page_url = next_page_url
322 current_page = int(self._search_regex(
323 r'\?page=(\d+)', next_page_url, 'next page number'))
325 next_page_url = _handle_page(self._fetch_tracks_page(
326 '%s/stream' % user_id, user_id, 'stream', idx,
327 real_page_number=current_page))
329 username = self._og_search_title(webpage)
330 description = self._get_user_description(webpage)
332 return self.playlist_result(entries, user_id, username, description)