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)
172 self
._sort
_formats
(formats
)
175 for edge
in (try_get(cloudcast
, lambda x
: x
['comments']['edges']) or []):
176 node
= edge
.get('node') or {}
177 text
= strip_or_none(node
.get('comment'))
180 user
= node
.get('user') or {}
182 'author': user
.get('displayName'),
183 'author_id': user
.get('username'),
185 'timestamp': parse_iso8601(node
.get('created')),
189 for t
in cloudcast
.get('tags'):
190 tag
= try_get(t
, lambda x
: x
['tag']['name'], compat_str
)
194 get_count
= lambda x
: int_or_none(try_get(cloudcast
, lambda y
: y
[x
]['totalCount']))
196 owner
= cloudcast
.get('owner') or {}
202 'description': cloudcast
.get('description'),
203 'thumbnail': try_get(cloudcast
, lambda x
: x
['picture']['url'], compat_str
),
204 'uploader': owner
.get('displayName'),
205 'timestamp': parse_iso8601(cloudcast
.get('publishDate')),
206 'uploader_id': owner
.get('username'),
207 'uploader_url': owner
.get('url'),
208 'duration': int_or_none(cloudcast
.get('audioLength')),
209 'view_count': int_or_none(cloudcast
.get('plays')),
210 'like_count': get_count('favorites'),
211 'repost_count': get_count('reposts'),
212 'comment_count': get_count('comments'),
213 'comments': comments
,
215 'artist': ', '.join(cloudcast
.get('featuringArtistList') or []) or None,
219 class MixcloudPlaylistBaseIE(MixcloudBaseIE
):
220 def _get_cloudcast(self
, node
):
223 def _get_playlist_title(self
, title
, slug
):
226 def _real_extract(self
, url
):
227 username
, slug
= self
._match
_valid
_url
(url
).groups()
228 username
= compat_urllib_parse_unquote(username
)
232 slug
= compat_urllib_parse_unquote(slug
)
233 playlist_id
= '%s_%s' % (username
, slug
)
235 is_playlist_type
= self
._ROOT
_TYPE
== 'playlist'
236 playlist_type
= 'items' if is_playlist_type
else slug
242 playlist
= self
._call
_api
(
243 self
._ROOT
_TYPE
, '''%s
255 }''' % (self
._TITLE
_KEY
, self
._DESCRIPTION
_KEY
, playlist_type
, list_filter
, self
._NODE
_TEMPLATE
),
256 playlist_id
, username
, slug
if is_playlist_type
else None)
258 items
= playlist
.get(playlist_type
) or {}
259 for edge
in items
.get('edges', []):
260 cloudcast
= self
._get
_cloudcast
(edge
.get('node') or {})
261 cloudcast_url
= cloudcast
.get('url')
262 if not cloudcast_url
:
264 slug
= try_get(cloudcast
, lambda x
: x
['slug'], compat_str
)
265 owner_username
= try_get(cloudcast
, lambda x
: x
['owner']['username'], compat_str
)
266 video_id
= '%s_%s' % (owner_username
, slug
) if slug
and owner_username
else None
267 entries
.append(self
.url_result(
268 cloudcast_url
, MixcloudIE
.ie_key(), video_id
))
270 page_info
= items
['pageInfo']
271 has_next_page
= page_info
['hasNextPage']
272 list_filter
= ', after: "%s"' % page_info
['endCursor']
274 return self
.playlist_result(
275 entries
, playlist_id
,
276 self
._get
_playlist
_title
(playlist
[self
._TITLE
_KEY
], slug
),
277 playlist
.get(self
._DESCRIPTION
_KEY
))
280 class MixcloudUserIE(MixcloudPlaylistBaseIE
):
281 _VALID_URL
= r
'https?://(?:www\.)?mixcloud\.com/(?P<id>[^/]+)/(?P<type>uploads|favorites|listens|stream)?/?$'
282 IE_NAME
= 'mixcloud:user'
285 'url': 'http://www.mixcloud.com/dholbach/',
287 'id': 'dholbach_uploads',
288 'title': 'Daniel Holbach (uploads)',
289 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
291 'playlist_mincount': 36,
293 'url': 'http://www.mixcloud.com/dholbach/uploads/',
295 'id': 'dholbach_uploads',
296 'title': 'Daniel Holbach (uploads)',
297 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
299 'playlist_mincount': 36,
301 'url': 'http://www.mixcloud.com/dholbach/favorites/',
303 'id': 'dholbach_favorites',
304 'title': 'Daniel Holbach (favorites)',
305 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
308 # 'playlist_items': '1-100',
310 'playlist_mincount': 396,
312 'url': 'http://www.mixcloud.com/dholbach/listens/',
314 'id': 'dholbach_listens',
315 'title': 'Daniel Holbach (listens)',
316 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
319 # 'playlist_items': '1-100',
321 'playlist_mincount': 1623,
322 'skip': 'Large list',
324 'url': 'https://www.mixcloud.com/FirstEar/stream/',
326 'id': 'FirstEar_stream',
327 'title': 'First Ear (stream)',
328 'description': 'Curators of good music\r\n\r\nfirstearmusic.com',
330 'playlist_mincount': 271,
333 _TITLE_KEY
= 'displayName'
334 _DESCRIPTION_KEY
= 'biog'
336 _NODE_TEMPLATE
= '''slug
338 owner { username }'''
340 def _get_playlist_title(self
, title
, slug
):
341 return '%s (%s)' % (title
, slug
)
344 class MixcloudPlaylistIE(MixcloudPlaylistBaseIE
):
345 _VALID_URL
= r
'https?://(?:www\.)?mixcloud\.com/(?P<user>[^/]+)/playlists/(?P<playlist>[^/]+)/?$'
346 IE_NAME
= 'mixcloud:playlist'
349 'url': 'https://www.mixcloud.com/maxvibes/playlists/jazzcat-on-ness-radio/',
351 'id': 'maxvibes_jazzcat-on-ness-radio',
352 'title': 'Ness Radio sessions',
354 'playlist_mincount': 59,
357 _DESCRIPTION_KEY
= 'description'
358 _ROOT_TYPE
= 'playlist'
359 _NODE_TEMPLATE
= '''cloudcast {
365 def _get_cloudcast(self
, node
):
366 return node
.get('cloudcast') or {}