1 from __future__ import unicode_literals
6 from .common import InfoExtractor
12 compat_urllib_parse_unquote,
23 class MixcloudBaseIE(InfoExtractor):
24 def _call_api(self, object_type, object_fields, display_id, username, slug=None):
25 lookup_key = object_type + 'Lookup'
26 return self._download_json(
27 'https://www.mixcloud.com/graphql', display_id, query={
29 %s(lookup: {username: "%s"%s}) {
32 }''' % (lookup_key, username, ', slug: "%s"' % slug if slug else '', object_fields)
33 })['data'][lookup_key]
36 class MixcloudIE(MixcloudBaseIE):
37 _VALID_URL = r'https?://(?:(?:www|beta|m)\.)?mixcloud\.com/([^/]+)/(?!stream|uploads|favorites|listens|playlists)([^/]+)'
41 'url': 'http://www.mixcloud.com/dholbach/cryptkeeper/',
43 'id': 'dholbach_cryptkeeper',
45 'title': 'Cryptkeeper',
46 'description': 'After quite a long silence from myself, finally another Drum\'n\'Bass mix with my favourite current dance floor bangers.',
47 'uploader': 'Daniel Holbach',
48 'uploader_id': 'dholbach',
49 'thumbnail': r're:https?://.*\.jpg',
51 'timestamp': 1321359578,
52 'upload_date': '20111115',
55 'url': 'http://www.mixcloud.com/gillespeterson/caribou-7-inch-vinyl-mix-chat/',
57 'id': 'gillespeterson_caribou-7-inch-vinyl-mix-chat',
59 'title': 'Caribou 7 inch Vinyl Mix & Chat',
60 'description': 'md5:2b8aec6adce69f9d41724647c65875e8',
61 'uploader': 'Gilles Peterson Worldwide',
62 'uploader_id': 'gillespeterson',
63 'thumbnail': 're:https?://.*',
65 'timestamp': 1422987057,
66 'upload_date': '20150203',
69 'url': 'https://beta.mixcloud.com/RedLightRadio/nosedrip-15-red-light-radio-01-18-2016/',
70 'only_matching': True,
72 _DECRYPTION_KEY = 'IFYOUWANTTHEARTISTSTOGETPAIDDONOTDOWNLOADFROMMIXCLOUD'
75 def _decrypt_xor_cipher(key, ciphertext):
76 """Encrypt/Decrypt XOR cipher. Both ways are possible because it's XOR."""
78 compat_chr(compat_ord(ch) ^ compat_ord(k))
79 for ch, k in compat_zip(ciphertext, itertools.cycle(key))])
81 def _real_extract(self, url):
82 username, slug = re.match(self._VALID_URL, url).groups()
83 username, slug = compat_urllib_parse_unquote(username), compat_urllib_parse_unquote(slug)
84 track_id = '%s_%s' % (username, slug)
86 cloudcast = self._call_api('cloudcast', '''audioLength
87 comments(first: 100) {
112 picture(width: 1024, height: 1024) {
129 }''', track_id, username, slug)
131 title = cloudcast['name']
133 stream_info = cloudcast['streamInfo']
136 for url_key in ('url', 'hlsUrl', 'dashUrl'):
137 format_url = stream_info.get(url_key)
140 decrypted = self._decrypt_xor_cipher(
141 self._DECRYPTION_KEY, compat_b64decode(format_url))
142 if url_key == 'hlsUrl':
143 formats.extend(self._extract_m3u8_formats(
144 decrypted, track_id, 'mp4', entry_protocol='m3u8_native',
145 m3u8_id='hls', fatal=False))
146 elif url_key == 'dashUrl':
147 formats.extend(self._extract_mpd_formats(
148 decrypted, track_id, mpd_id='dash', fatal=False))
153 'downloader_options': {
154 # Mixcloud starts throttling at >~5M
155 'http_chunk_size': 5242880,
159 if not formats and cloudcast.get('isExclusive'):
160 self.raise_login_required()
162 self._sort_formats(formats)
165 for edge in (try_get(cloudcast, lambda x: x['comments']['edges']) or []):
166 node = edge.get('node') or {}
167 text = strip_or_none(node.get('comment'))
170 user = node.get('user') or {}
172 'author': user.get('displayName'),
173 'author_id': user.get('username'),
175 'timestamp': parse_iso8601(node.get('created')),
179 for t in cloudcast.get('tags'):
180 tag = try_get(t, lambda x: x['tag']['name'], compat_str)
184 get_count = lambda x: int_or_none(try_get(cloudcast, lambda y: y[x]['totalCount']))
186 owner = cloudcast.get('owner') or {}
192 'description': cloudcast.get('description'),
193 'thumbnail': try_get(cloudcast, lambda x: x['picture']['url'], compat_str),
194 'uploader': owner.get('displayName'),
195 'timestamp': parse_iso8601(cloudcast.get('publishDate')),
196 'uploader_id': owner.get('username'),
197 'uploader_url': owner.get('url'),
198 'duration': int_or_none(cloudcast.get('audioLength')),
199 'view_count': int_or_none(cloudcast.get('plays')),
200 'like_count': get_count('favorites'),
201 'repost_count': get_count('reposts'),
202 'comment_count': get_count('comments'),
203 'comments': comments,
205 'artist': ', '.join(cloudcast.get('featuringArtistList') or []) or None,
209 class MixcloudPlaylistBaseIE(MixcloudBaseIE):
210 def _get_cloudcast(self, node):
213 def _get_playlist_title(self, title, slug):
216 def _real_extract(self, url):
217 username, slug = re.match(self._VALID_URL, url).groups()
218 username = compat_urllib_parse_unquote(username)
222 slug = compat_urllib_parse_unquote(slug)
223 playlist_id = '%s_%s' % (username, slug)
225 is_playlist_type = self._ROOT_TYPE == 'playlist'
226 playlist_type = 'items' if is_playlist_type else slug
232 playlist = self._call_api(
233 self._ROOT_TYPE, '''%s
245 }''' % (self._TITLE_KEY, self._DESCRIPTION_KEY, playlist_type, list_filter, self._NODE_TEMPLATE),
246 playlist_id, username, slug if is_playlist_type else None)
248 items = playlist.get(playlist_type) or {}
249 for edge in items.get('edges', []):
250 cloudcast = self._get_cloudcast(edge.get('node') or {})
251 cloudcast_url = cloudcast.get('url')
252 if not cloudcast_url:
254 entries.append(self.url_result(
255 cloudcast_url, MixcloudIE.ie_key(), cloudcast.get('slug')))
257 page_info = items['pageInfo']
258 has_next_page = page_info['hasNextPage']
259 list_filter = ', after: "%s"' % page_info['endCursor']
261 return self.playlist_result(
262 entries, playlist_id,
263 self._get_playlist_title(playlist[self._TITLE_KEY], slug),
264 playlist.get(self._DESCRIPTION_KEY))
267 class MixcloudUserIE(MixcloudPlaylistBaseIE):
268 _VALID_URL = r'https?://(?:www\.)?mixcloud\.com/(?P<id>[^/]+)/(?P<type>uploads|favorites|listens|stream)?/?$'
269 IE_NAME = 'mixcloud:user'
272 'url': 'http://www.mixcloud.com/dholbach/',
274 'id': 'dholbach_uploads',
275 'title': 'Daniel Holbach (uploads)',
276 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
278 'playlist_mincount': 36,
280 'url': 'http://www.mixcloud.com/dholbach/uploads/',
282 'id': 'dholbach_uploads',
283 'title': 'Daniel Holbach (uploads)',
284 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
286 'playlist_mincount': 36,
288 'url': 'http://www.mixcloud.com/dholbach/favorites/',
290 'id': 'dholbach_favorites',
291 'title': 'Daniel Holbach (favorites)',
292 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
295 # 'playlist_items': '1-100',
297 'playlist_mincount': 396,
299 'url': 'http://www.mixcloud.com/dholbach/listens/',
301 'id': 'dholbach_listens',
302 'title': 'Daniel Holbach (listens)',
303 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
306 # 'playlist_items': '1-100',
308 'playlist_mincount': 1623,
309 'skip': 'Large list',
311 'url': 'https://www.mixcloud.com/FirstEar/stream/',
313 'id': 'FirstEar_stream',
314 'title': 'First Ear (stream)',
315 'description': 'Curators of good music\r\n\r\nfirstearmusic.com',
317 'playlist_mincount': 271,
320 _TITLE_KEY = 'displayName'
321 _DESCRIPTION_KEY = 'biog'
323 _NODE_TEMPLATE = '''slug
326 def _get_playlist_title(self, title, slug):
327 return '%s (%s)' % (title, slug)
330 class MixcloudPlaylistIE(MixcloudPlaylistBaseIE):
331 _VALID_URL = r'https?://(?:www\.)?mixcloud\.com/(?P<user>[^/]+)/playlists/(?P<playlist>[^/]+)/?$'
332 IE_NAME = 'mixcloud:playlist'
335 'url': 'https://www.mixcloud.com/maxvibes/playlists/jazzcat-on-ness-radio/',
337 'id': 'maxvibes_jazzcat-on-ness-radio',
338 'title': 'Ness Radio sessions',
340 'playlist_mincount': 59,
343 _DESCRIPTION_KEY = 'description'
344 _ROOT_TYPE = 'playlist'
345 _NODE_TEMPLATE = '''cloudcast {
350 def _get_cloudcast(self, node):
351 return node.get('cloudcast') or {}