5 from .common
import InfoExtractor
6 from ..compat
import compat_ord
16 class MixcloudBaseIE(InfoExtractor
):
17 def _call_api(self
, object_type
, object_fields
, display_id
, username
, slug
=None):
18 lookup_key
= object_type
+ 'Lookup'
19 return self
._download
_json
(
20 'https://app.mixcloud.com/graphql', display_id
, query
={
22 %s(lookup: {username: "%s"%s}) {
25 }''' % (lookup_key
, username
, f
', slug: "{slug}"' if slug
else '', object_fields
), # noqa: UP031
26 })['data'][lookup_key
]
29 class MixcloudIE(MixcloudBaseIE
):
30 _VALID_URL
= r
'https?://(?:(?:www|beta|m)\.)?mixcloud\.com/([^/]+)/(?!stream|uploads|favorites|listens|playlists)([^/]+)'
34 'url': 'http://www.mixcloud.com/dholbach/cryptkeeper/',
36 'id': 'dholbach_cryptkeeper',
38 'title': 'Cryptkeeper',
39 'description': 'After quite a long silence from myself, finally another Drum\'n\'Bass mix with my favourite current dance floor bangers.',
40 'uploader': 'Daniel Holbach',
41 'uploader_id': 'dholbach',
42 'thumbnail': r
're:https?://.*\.jpg',
44 'timestamp': 1321359578,
45 'upload_date': '20111115',
46 'uploader_url': 'https://www.mixcloud.com/dholbach/',
47 'artist': 'Submorphics & Chino , Telekinesis, Porter Robinson, Enei, Breakage ft Jess Mills',
54 'params': {'skip_download': 'm3u8'},
56 'url': 'http://www.mixcloud.com/gillespeterson/caribou-7-inch-vinyl-mix-chat/',
58 'id': 'gillespeterson_caribou-7-inch-vinyl-mix-chat',
60 'title': 'Caribou 7 inch Vinyl Mix & Chat',
61 'description': 'md5:2b8aec6adce69f9d41724647c65875e8',
62 'uploader': 'Gilles Peterson Worldwide',
63 'uploader_id': 'gillespeterson',
64 'thumbnail': 're:https?://.*',
66 'timestamp': 1422987057,
67 'upload_date': '20150203',
68 'uploader_url': 'https://www.mixcloud.com/gillespeterson/',
75 'params': {'skip_download': '404 playback error on site'},
77 'url': 'https://beta.mixcloud.com/RedLightRadio/nosedrip-15-red-light-radio-01-18-2016/',
78 'only_matching': True,
80 _DECRYPTION_KEY
= 'IFYOUWANTTHEARTISTSTOGETPAIDDONOTDOWNLOADFROMMIXCLOUD'
83 def _decrypt_xor_cipher(key
, ciphertext
):
84 """Encrypt/Decrypt XOR cipher. Both ways are possible because it's XOR."""
86 chr(compat_ord(ch
) ^
compat_ord(k
))
87 for ch
, k
in zip(ciphertext
, itertools
.cycle(key
))])
89 def _real_extract(self
, url
):
90 username
, slug
= self
._match
_valid
_url
(url
).groups()
91 username
, slug
= urllib
.parse
.unquote(username
), urllib
.parse
.unquote(slug
)
92 track_id
= f
'{username}_{slug}'
94 cloudcast
= self
._call
_api
('cloudcast', '''audioLength
95 comments(first: 100) {
120 picture(width: 1024, height: 1024) {
139 id''', track_id
, username
, slug
)
142 raise ExtractorError('Track not found', expected
=True)
144 reason
= cloudcast
.get('restrictedReason')
145 if reason
== 'tracklist':
146 raise ExtractorError('Track unavailable in your country due to licensing restrictions', expected
=True)
147 elif reason
== 'repeat_play':
148 raise ExtractorError('You have reached your play limit for this track', expected
=True)
150 raise ExtractorError('Track is restricted', expected
=True)
152 title
= cloudcast
['name']
154 stream_info
= cloudcast
['streamInfo']
157 for url_key
in ('url', 'hlsUrl', 'dashUrl'):
158 format_url
= stream_info
.get(url_key
)
161 decrypted
= self
._decrypt
_xor
_cipher
(
162 self
._DECRYPTION
_KEY
, base64
.b64decode(format_url
))
163 if url_key
== 'hlsUrl':
164 formats
.extend(self
._extract
_m
3u8_formats
(
165 decrypted
, track_id
, 'mp4', entry_protocol
='m3u8_native',
166 m3u8_id
='hls', fatal
=False))
167 elif url_key
== 'dashUrl':
168 formats
.extend(self
._extract
_mpd
_formats
(
169 decrypted
, track_id
, mpd_id
='dash', fatal
=False))
175 'downloader_options': {
176 # Mixcloud starts throttling at >~5M
177 'http_chunk_size': 5242880,
181 if not formats
and cloudcast
.get('isExclusive'):
182 self
.raise_login_required(metadata_available
=True)
185 for edge
in (try_get(cloudcast
, lambda x
: x
['comments']['edges']) or []):
186 node
= edge
.get('node') or {}
187 text
= strip_or_none(node
.get('comment'))
190 user
= node
.get('user') or {}
192 'author': user
.get('displayName'),
193 'author_id': user
.get('username'),
195 'timestamp': parse_iso8601(node
.get('created')),
199 for t
in cloudcast
.get('tags'):
200 tag
= try_get(t
, lambda x
: x
['tag']['name'], str)
204 get_count
= lambda x
: int_or_none(try_get(cloudcast
, lambda y
: y
[x
]['totalCount']))
206 owner
= cloudcast
.get('owner') or {}
212 'description': cloudcast
.get('description'),
213 'thumbnail': try_get(cloudcast
, lambda x
: x
['picture']['url'], str),
214 'uploader': owner
.get('displayName'),
215 'timestamp': parse_iso8601(cloudcast
.get('publishDate')),
216 'uploader_id': owner
.get('username'),
217 'uploader_url': owner
.get('url'),
218 'duration': int_or_none(cloudcast
.get('audioLength')),
219 'view_count': int_or_none(cloudcast
.get('plays')),
220 'like_count': get_count('favorites'),
221 'repost_count': get_count('reposts'),
222 'comment_count': get_count('comments'),
223 'comments': comments
,
225 'artist': ', '.join(cloudcast
.get('featuringArtistList') or []) or None,
229 class MixcloudPlaylistBaseIE(MixcloudBaseIE
):
230 def _get_cloudcast(self
, node
):
233 def _get_playlist_title(self
, title
, slug
):
236 def _real_extract(self
, url
):
237 username
, slug
= self
._match
_valid
_url
(url
).groups()
238 username
= urllib
.parse
.unquote(username
)
242 slug
= urllib
.parse
.unquote(slug
)
243 playlist_id
= f
'{username}_{slug}'
245 is_playlist_type
= self
._ROOT
_TYPE
== 'playlist'
246 playlist_type
= 'items' if is_playlist_type
else slug
252 playlist
= self
._call
_api
(
253 self
._ROOT
_TYPE
, '''%s
265 }''' % (self
._TITLE
_KEY
, self
._DESCRIPTION
_KEY
, playlist_type
, list_filter
, self
._NODE
_TEMPLATE
), # noqa: UP031
266 playlist_id
, username
, slug
if is_playlist_type
else None)
268 items
= playlist
.get(playlist_type
) or {}
269 for edge
in items
.get('edges', []):
270 cloudcast
= self
._get
_cloudcast
(edge
.get('node') or {})
271 cloudcast_url
= cloudcast
.get('url')
272 if not cloudcast_url
:
274 item_slug
= try_get(cloudcast
, lambda x
: x
['slug'], str)
275 owner_username
= try_get(cloudcast
, lambda x
: x
['owner']['username'], str)
276 video_id
= f
'{owner_username}_{item_slug}' if item_slug
and owner_username
else None
277 entries
.append(self
.url_result(
278 cloudcast_url
, MixcloudIE
.ie_key(), video_id
))
280 page_info
= items
['pageInfo']
281 has_next_page
= page_info
['hasNextPage']
282 list_filter
= ', after: "{}"'.format(page_info
['endCursor'])
284 return self
.playlist_result(
285 entries
, playlist_id
,
286 self
._get
_playlist
_title
(playlist
[self
._TITLE
_KEY
], slug
),
287 playlist
.get(self
._DESCRIPTION
_KEY
))
290 class MixcloudUserIE(MixcloudPlaylistBaseIE
):
291 _VALID_URL
= r
'https?://(?:www\.)?mixcloud\.com/(?P<id>[^/]+)/(?P<type>uploads|favorites|listens|stream)?/?$'
292 IE_NAME
= 'mixcloud:user'
295 'url': 'http://www.mixcloud.com/dholbach/',
297 'id': 'dholbach_uploads',
298 'title': 'Daniel Holbach (uploads)',
299 'description': 'md5:a3f468a60ac8c3e1f8616380fc469b2b',
301 'playlist_mincount': 36,
303 'url': 'http://www.mixcloud.com/dholbach/uploads/',
305 'id': 'dholbach_uploads',
306 'title': 'Daniel Holbach (uploads)',
307 'description': 'md5:a3f468a60ac8c3e1f8616380fc469b2b',
309 'playlist_mincount': 36,
311 'url': 'http://www.mixcloud.com/dholbach/favorites/',
313 'id': 'dholbach_favorites',
314 'title': 'Daniel Holbach (favorites)',
315 'description': 'md5:a3f468a60ac8c3e1f8616380fc469b2b',
318 # 'playlist_items': '1-100',
320 'playlist_mincount': 396,
322 'url': 'http://www.mixcloud.com/dholbach/listens/',
324 'id': 'dholbach_listens',
325 'title': 'Daniel Holbach (listens)',
326 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
329 # 'playlist_items': '1-100',
331 'playlist_mincount': 1623,
332 'skip': 'Large list',
334 'url': 'https://www.mixcloud.com/FirstEar/stream/',
336 'id': 'FirstEar_stream',
337 'title': 'First Ear (stream)',
338 'description': 'we maraud for ears',
340 'playlist_mincount': 269,
343 _TITLE_KEY
= 'displayName'
344 _DESCRIPTION_KEY
= 'biog'
346 _NODE_TEMPLATE
= '''slug
348 owner { username }'''
350 def _get_playlist_title(self
, title
, slug
):
351 return f
'{title} ({slug})'
354 class MixcloudPlaylistIE(MixcloudPlaylistBaseIE
):
355 _VALID_URL
= r
'https?://(?:www\.)?mixcloud\.com/(?P<user>[^/]+)/playlists/(?P<playlist>[^/]+)/?$'
356 IE_NAME
= 'mixcloud:playlist'
359 'url': 'https://www.mixcloud.com/maxvibes/playlists/jazzcat-on-ness-radio/',
361 'id': 'maxvibes_jazzcat-on-ness-radio',
362 'title': 'Ness Radio sessions',
364 'playlist_mincount': 59,
367 _DESCRIPTION_KEY
= 'description'
368 _ROOT_TYPE
= 'playlist'
369 _NODE_TEMPLATE
= '''cloudcast {
375 def _get_cloudcast(self
, node
):
376 return node
.get('cloudcast') or {}