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_RE_TEMPLATE
= r
'player\s*:\s*{.*?\b%s\s*:\s*(["\'])(?P
<key
>(?
:(?
!\
1).)+)\
1'
95 for key_name in ('value
', 'key_value
'):
96 key = self._search_regex(
97 KEY_RE_TEMPLATE % key_name, js, 'key
',
98 default=None, group='key
')
99 if key and isinstance(key, compat_str):
100 self._keys.insert(0, key)
101 self._current_key = key
103 message = self._html_search_regex(
104 r'(?s
)<div
[^
>]+class="global-message cloudcast-disabled-notice-light"[^
>]*>(.+?
)<(?
:a|
/div
)',
105 webpage, 'error message
', default=None)
107 encrypted_play_info = self._search_regex(
108 r'm
-play
-info
="([^"]+)"', webpage, 'play info')
110 play_info = self._decrypt_play_info(encrypted_play_info, track_id)
112 if message and 'stream_url' not in play_info:
113 raise ExtractorError('%s said: %s' % (self.IE_NAME, message), expected=True)
115 song_url = play_info['stream_url']
117 title = self._html_search_regex(r'm-title="([^
"]+)"', webpage, 'title
')
118 thumbnail = self._proto_relative_url(self._html_search_regex(
119 r'm
-thumbnail
-url
="([^"]+)"', webpage, 'thumbnail', fatal=False))
120 uploader = self._html_search_regex(
121 r'm-owner-name="([^
"]+)"', webpage, 'uploader
', fatal=False)
122 uploader_id = self._search_regex(
123 r'\s
+"profile": "([^"]+)",', webpage, 'uploader id', fatal=False)
124 description = self._og_search_description(webpage)
125 view_count = str_to_int(self._search_regex(
126 [r'<meta itemprop="interactionCount
" content="UserPlays
:([0-9]+)"',
127 r'/listeners/?">([0-9,.]+)</a
>',
128 r'(?
:m|data
)-tooltip
=["\']([\d,.]+) plays'],
129 webpage, 'play count', default=None))
135 'description': description,
136 'thumbnail': thumbnail,
137 'uploader': uploader,
138 'uploader_id': uploader_id,
139 'view_count': view_count,
143 class MixcloudPlaylistBaseIE(InfoExtractor):
146 def _find_urls_in_page(self, page):
147 for url in re.findall(r'm-play-button m-url="(?P
<url
>[^
"]+)"', page):
148 yield self.url_result(
149 compat_urlparse.urljoin('https
://www
.mixcloud
.com
', clean_html(url)),
152 def _fetch_tracks_page(self, path, video_id, page_name, current_page, real_page_number=None):
153 real_page_number = real_page_number or current_page + 1
154 return self._download_webpage(
155 'https
://www
.mixcloud
.com
/%s/' % path, video_id,
156 note='Download
%s (page
%d)' % (page_name, current_page + 1),
157 errnote='Unable to download
%s' % page_name,
158 query={'page': real_page_number, 'list': 'main', '_ajax': '1'},
159 headers={'X-Requested-With': 'XMLHttpRequest'})
161 def _tracks_page_func(self, page, video_id, page_name, current_page):
162 resp = self._fetch_tracks_page(page, video_id, page_name, current_page)
164 for item in self._find_urls_in_page(resp):
167 def _get_user_description(self, page_content):
168 return self._html_search_regex(
169 r'<div
[^
>]+class="profile-bio"[^
>]*>(.+?
)</div
>',
170 page_content, 'user description
', fatal=False)
173 class MixcloudUserIE(MixcloudPlaylistBaseIE):
174 _VALID_URL = r'https?
://(?
:www\
.)?mixcloud\
.com
/(?P
<user
>[^
/]+)/(?P
<type>uploads|favorites|listens
)?
/?$
'
175 IE_NAME = 'mixcloud
:user
'
178 'url
': 'http
://www
.mixcloud
.com
/dholbach
/',
180 'id': 'dholbach_uploads
',
181 'title
': 'Daniel
Holbach (uploads
)',
182 'description
': 'md5
:def36060ac8747b3aabca54924897e47
',
184 'playlist_mincount
': 11,
186 'url
': 'http
://www
.mixcloud
.com
/dholbach
/uploads
/',
188 'id': 'dholbach_uploads
',
189 'title
': 'Daniel
Holbach (uploads
)',
190 'description
': 'md5
:def36060ac8747b3aabca54924897e47
',
192 'playlist_mincount
': 11,
194 'url
': 'http
://www
.mixcloud
.com
/dholbach
/favorites
/',
196 'id': 'dholbach_favorites
',
197 'title
': 'Daniel
Holbach (favorites
)',
198 'description
': 'md5
:def36060ac8747b3aabca54924897e47
',
201 'playlist_items
': '1-100',
203 'playlist_mincount
': 100,
205 'url
': 'http
://www
.mixcloud
.com
/dholbach
/listens
/',
207 'id': 'dholbach_listens
',
208 'title
': 'Daniel
Holbach (listens
)',
209 'description
': 'md5
:def36060ac8747b3aabca54924897e47
',
212 'playlist_items
': '1-100',
214 'playlist_mincount
': 100,
217 def _real_extract(self, url):
218 mobj = re.match(self._VALID_URL, url)
219 user_id = mobj.group('user
')
220 list_type = mobj.group('type')
222 # if only a profile URL was supplied, default to download all uploads
223 if list_type is None:
224 list_type = 'uploads
'
226 video_id = '%s_%s' % (user_id, list_type)
228 profile = self._download_webpage(
229 'https
://www
.mixcloud
.com
/%s/' % user_id, video_id,
230 note='Downloading user profile
',
231 errnote='Unable to download user profile
')
233 username = self._og_search_title(profile)
234 description = self._get_user_description(profile)
236 entries = OnDemandPagedList(
238 self._tracks_page_func,
239 '%s/%s' % (user_id, list_type), video_id, 'list of
%s' % list_type),
240 self._PAGE_SIZE, use_cache=True)
242 return self.playlist_result(
243 entries, video_id, '%s (%s)' % (username, list_type), description)
246 class MixcloudPlaylistIE(MixcloudPlaylistBaseIE):
247 _VALID_URL = r'https?
://(?
:www\
.)?mixcloud\
.com
/(?P
<user
>[^
/]+)/playlists
/(?P
<playlist
>[^
/]+)/?$
'
248 IE_NAME = 'mixcloud
:playlist
'
251 'url
': 'https
://www
.mixcloud
.com
/RedBullThre3style
/playlists
/tokyo
-finalists
-2015/',
253 'id': 'RedBullThre3style_tokyo
-finalists
-2015',
254 'title
': 'National Champions
2015',
255 'description
': 'md5
:6ff5fb01ac76a31abc9b3939c16243a3
',
257 'playlist_mincount
': 16,
259 'url
': 'https
://www
.mixcloud
.com
/maxvibes
/playlists
/jazzcat
-on
-ness
-radio
/',
260 'only_matching
': True,
263 def _real_extract(self, url):
264 mobj = re.match(self._VALID_URL, url)
265 user_id = mobj.group('user
')
266 playlist_id = mobj.group('playlist
')
267 video_id = '%s_%s' % (user_id, playlist_id)
269 webpage = self._download_webpage(
271 note='Downloading playlist page
',
272 errnote='Unable to download playlist page
')
274 title = self._html_search_regex(
275 r'<a
[^
>]+class="parent active"[^
>]*><b
>\d
+</b
><span
[^
>]*>([^
<]+)',
276 webpage, 'playlist title
',
277 default=None) or self._og_search_title(webpage, fatal=False)
278 description = self._get_user_description(webpage)
280 entries = OnDemandPagedList(
282 self._tracks_page_func,
283 '%s/playlists
/%s' % (user_id, playlist_id), video_id, 'tracklist
'),
286 return self.playlist_result(entries, video_id, title, description)
289 class MixcloudStreamIE(MixcloudPlaylistBaseIE):
290 _VALID_URL = r'https?
://(?
:www\
.)?mixcloud\
.com
/(?P
<id>[^
/]+)/stream
/?$
'
291 IE_NAME = 'mixcloud
:stream
'
294 'url
': 'https
://www
.mixcloud
.com
/FirstEar
/stream
/',
297 'title
': 'First Ear
',
298 'description
': 'Curators of good music
\nfirstearmusic
.com
',
300 'playlist_mincount
': 192,
303 def _real_extract(self, url):
304 user_id = self._match_id(url)
306 webpage = self._download_webpage(url, user_id)
311 def _handle_page(page):
312 entries.extend(self._find_urls_in_page(page))
313 return self._search_regex(
314 r'm
-next
-page
-url
="([^"]+)"', page,
315 'next page URL', default=None)
317 next_page_url = _handle_page(webpage)
319 for idx in itertools.count(0):
320 if not next_page_url or prev_page_url == next_page_url:
323 prev_page_url = next_page_url
324 current_page = int(self._search_regex(
325 r'\?page=(\d+)', next_page_url, 'next page number'))
327 next_page_url = _handle_page(self._fetch_tracks_page(
328 '%s/stream' % user_id, user_id, 'stream', idx,
329 real_page_number=current_page))
331 username = self._og_search_title(webpage)
332 description = self._get_user_description(webpage)
334 return self.playlist_result(entries, user_id, username, description)