3 from .common
import InfoExtractor
8 compat_urllib_parse_unquote
,
19 class MixcloudBaseIE(InfoExtractor
):
20 def _call_api(self
, object_type
, object_fields
, display_id
, username
, slug
=None):
21 lookup_key
= object_type
+ 'Lookup'
22 return self
._download
_json
(
23 'https://www.mixcloud.com/graphql', display_id
, query
={
25 %s(lookup: {username: "%s"%s}) {
28 }''' % (lookup_key
, username
, ', slug: "%s"' % slug
if slug
else '', object_fields
)
29 })['data'][lookup_key
]
32 class MixcloudIE(MixcloudBaseIE
):
33 _VALID_URL
= r
'https?://(?:(?:www|beta|m)\.)?mixcloud\.com/([^/]+)/(?!stream|uploads|favorites|listens|playlists)([^/]+)'
37 'url': 'http://www.mixcloud.com/dholbach/cryptkeeper/',
39 'id': 'dholbach_cryptkeeper',
41 'title': 'Cryptkeeper',
42 'description': 'After quite a long silence from myself, finally another Drum\'n\'Bass mix with my favourite current dance floor bangers.',
43 'uploader': 'Daniel Holbach',
44 'uploader_id': 'dholbach',
45 'thumbnail': r
're:https?://.*\.jpg',
47 'timestamp': 1321359578,
48 'upload_date': '20111115',
51 'url': 'http://www.mixcloud.com/gillespeterson/caribou-7-inch-vinyl-mix-chat/',
53 'id': 'gillespeterson_caribou-7-inch-vinyl-mix-chat',
55 'title': 'Caribou 7 inch Vinyl Mix & Chat',
56 'description': 'md5:2b8aec6adce69f9d41724647c65875e8',
57 'uploader': 'Gilles Peterson Worldwide',
58 'uploader_id': 'gillespeterson',
59 'thumbnail': 're:https?://.*',
61 'timestamp': 1422987057,
62 'upload_date': '20150203',
65 'url': 'https://beta.mixcloud.com/RedLightRadio/nosedrip-15-red-light-radio-01-18-2016/',
66 'only_matching': True,
68 _DECRYPTION_KEY
= 'IFYOUWANTTHEARTISTSTOGETPAIDDONOTDOWNLOADFROMMIXCLOUD'
71 def _decrypt_xor_cipher(key
, ciphertext
):
72 """Encrypt/Decrypt XOR cipher. Both ways are possible because it's XOR."""
74 chr(compat_ord(ch
) ^
compat_ord(k
))
75 for ch
, k
in zip(ciphertext
, itertools
.cycle(key
))])
77 def _real_extract(self
, url
):
78 username
, slug
= self
._match
_valid
_url
(url
).groups()
79 username
, slug
= compat_urllib_parse_unquote(username
), compat_urllib_parse_unquote(slug
)
80 track_id
= '%s_%s' % (username
, slug
)
82 cloudcast
= self
._call
_api
('cloudcast', '''audioLength
83 comments(first: 100) {
108 picture(width: 1024, height: 1024) {
127 id''', track_id
, username
, slug
)
130 raise ExtractorError('Track not found', expected
=True)
132 reason
= cloudcast
.get('restrictedReason')
133 if reason
== 'tracklist':
134 raise ExtractorError('Track unavailable in your country due to licensing restrictions', expected
=True)
135 elif reason
== 'repeat_play':
136 raise ExtractorError('You have reached your play limit for this track', expected
=True)
138 raise ExtractorError('Track is restricted', expected
=True)
140 title
= cloudcast
['name']
142 stream_info
= cloudcast
['streamInfo']
145 for url_key
in ('url', 'hlsUrl', 'dashUrl'):
146 format_url
= stream_info
.get(url_key
)
149 decrypted
= self
._decrypt
_xor
_cipher
(
150 self
._DECRYPTION
_KEY
, compat_b64decode(format_url
))
151 if url_key
== 'hlsUrl':
152 formats
.extend(self
._extract
_m
3u8_formats
(
153 decrypted
, track_id
, 'mp4', entry_protocol
='m3u8_native',
154 m3u8_id
='hls', fatal
=False))
155 elif url_key
== 'dashUrl':
156 formats
.extend(self
._extract
_mpd
_formats
(
157 decrypted
, track_id
, mpd_id
='dash', fatal
=False))
163 'downloader_options': {
164 # Mixcloud starts throttling at >~5M
165 'http_chunk_size': 5242880,
169 if not formats
and cloudcast
.get('isExclusive'):
170 self
.raise_login_required(metadata_available
=True)
173 for edge
in (try_get(cloudcast
, lambda x
: x
['comments']['edges']) or []):
174 node
= edge
.get('node') or {}
175 text
= strip_or_none(node
.get('comment'))
178 user
= node
.get('user') or {}
180 'author': user
.get('displayName'),
181 'author_id': user
.get('username'),
183 'timestamp': parse_iso8601(node
.get('created')),
187 for t
in cloudcast
.get('tags'):
188 tag
= try_get(t
, lambda x
: x
['tag']['name'], compat_str
)
192 get_count
= lambda x
: int_or_none(try_get(cloudcast
, lambda y
: y
[x
]['totalCount']))
194 owner
= cloudcast
.get('owner') or {}
200 'description': cloudcast
.get('description'),
201 'thumbnail': try_get(cloudcast
, lambda x
: x
['picture']['url'], compat_str
),
202 'uploader': owner
.get('displayName'),
203 'timestamp': parse_iso8601(cloudcast
.get('publishDate')),
204 'uploader_id': owner
.get('username'),
205 'uploader_url': owner
.get('url'),
206 'duration': int_or_none(cloudcast
.get('audioLength')),
207 'view_count': int_or_none(cloudcast
.get('plays')),
208 'like_count': get_count('favorites'),
209 'repost_count': get_count('reposts'),
210 'comment_count': get_count('comments'),
211 'comments': comments
,
213 'artist': ', '.join(cloudcast
.get('featuringArtistList') or []) or None,
217 class MixcloudPlaylistBaseIE(MixcloudBaseIE
):
218 def _get_cloudcast(self
, node
):
221 def _get_playlist_title(self
, title
, slug
):
224 def _real_extract(self
, url
):
225 username
, slug
= self
._match
_valid
_url
(url
).groups()
226 username
= compat_urllib_parse_unquote(username
)
230 slug
= compat_urllib_parse_unquote(slug
)
231 playlist_id
= '%s_%s' % (username
, slug
)
233 is_playlist_type
= self
._ROOT
_TYPE
== 'playlist'
234 playlist_type
= 'items' if is_playlist_type
else slug
240 playlist
= self
._call
_api
(
241 self
._ROOT
_TYPE
, '''%s
253 }''' % (self
._TITLE
_KEY
, self
._DESCRIPTION
_KEY
, playlist_type
, list_filter
, self
._NODE
_TEMPLATE
),
254 playlist_id
, username
, slug
if is_playlist_type
else None)
256 items
= playlist
.get(playlist_type
) or {}
257 for edge
in items
.get('edges', []):
258 cloudcast
= self
._get
_cloudcast
(edge
.get('node') or {})
259 cloudcast_url
= cloudcast
.get('url')
260 if not cloudcast_url
:
262 slug
= try_get(cloudcast
, lambda x
: x
['slug'], compat_str
)
263 owner_username
= try_get(cloudcast
, lambda x
: x
['owner']['username'], compat_str
)
264 video_id
= '%s_%s' % (owner_username
, slug
) if slug
and owner_username
else None
265 entries
.append(self
.url_result(
266 cloudcast_url
, MixcloudIE
.ie_key(), video_id
))
268 page_info
= items
['pageInfo']
269 has_next_page
= page_info
['hasNextPage']
270 list_filter
= ', after: "%s"' % page_info
['endCursor']
272 return self
.playlist_result(
273 entries
, playlist_id
,
274 self
._get
_playlist
_title
(playlist
[self
._TITLE
_KEY
], slug
),
275 playlist
.get(self
._DESCRIPTION
_KEY
))
278 class MixcloudUserIE(MixcloudPlaylistBaseIE
):
279 _VALID_URL
= r
'https?://(?:www\.)?mixcloud\.com/(?P<id>[^/]+)/(?P<type>uploads|favorites|listens|stream)?/?$'
280 IE_NAME
= 'mixcloud:user'
283 'url': 'http://www.mixcloud.com/dholbach/',
285 'id': 'dholbach_uploads',
286 'title': 'Daniel Holbach (uploads)',
287 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
289 'playlist_mincount': 36,
291 'url': 'http://www.mixcloud.com/dholbach/uploads/',
293 'id': 'dholbach_uploads',
294 'title': 'Daniel Holbach (uploads)',
295 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
297 'playlist_mincount': 36,
299 'url': 'http://www.mixcloud.com/dholbach/favorites/',
301 'id': 'dholbach_favorites',
302 'title': 'Daniel Holbach (favorites)',
303 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
306 # 'playlist_items': '1-100',
308 'playlist_mincount': 396,
310 'url': 'http://www.mixcloud.com/dholbach/listens/',
312 'id': 'dholbach_listens',
313 'title': 'Daniel Holbach (listens)',
314 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
317 # 'playlist_items': '1-100',
319 'playlist_mincount': 1623,
320 'skip': 'Large list',
322 'url': 'https://www.mixcloud.com/FirstEar/stream/',
324 'id': 'FirstEar_stream',
325 'title': 'First Ear (stream)',
326 'description': 'Curators of good music\r\n\r\nfirstearmusic.com',
328 'playlist_mincount': 271,
331 _TITLE_KEY
= 'displayName'
332 _DESCRIPTION_KEY
= 'biog'
334 _NODE_TEMPLATE
= '''slug
336 owner { username }'''
338 def _get_playlist_title(self
, title
, slug
):
339 return '%s (%s)' % (title
, slug
)
342 class MixcloudPlaylistIE(MixcloudPlaylistBaseIE
):
343 _VALID_URL
= r
'https?://(?:www\.)?mixcloud\.com/(?P<user>[^/]+)/playlists/(?P<playlist>[^/]+)/?$'
344 IE_NAME
= 'mixcloud:playlist'
347 'url': 'https://www.mixcloud.com/maxvibes/playlists/jazzcat-on-ness-radio/',
349 'id': 'maxvibes_jazzcat-on-ness-radio',
350 'title': 'Ness Radio sessions',
352 'playlist_mincount': 59,
355 _DESCRIPTION_KEY
= 'description'
356 _ROOT_TYPE
= 'playlist'
357 _NODE_TEMPLATE
= '''cloudcast {
363 def _get_cloudcast(self
, node
):
364 return node
.get('cloudcast') or {}