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))
162 'downloader_options': {
163 # Mixcloud starts throttling at >~5M
164 'http_chunk_size': 5242880,
168 if not formats
and cloudcast
.get('isExclusive'):
169 self
.raise_login_required(metadata_available
=True)
171 self
._sort
_formats
(formats
)
174 for edge
in (try_get(cloudcast
, lambda x
: x
['comments']['edges']) or []):
175 node
= edge
.get('node') or {}
176 text
= strip_or_none(node
.get('comment'))
179 user
= node
.get('user') or {}
181 'author': user
.get('displayName'),
182 'author_id': user
.get('username'),
184 'timestamp': parse_iso8601(node
.get('created')),
188 for t
in cloudcast
.get('tags'):
189 tag
= try_get(t
, lambda x
: x
['tag']['name'], compat_str
)
193 get_count
= lambda x
: int_or_none(try_get(cloudcast
, lambda y
: y
[x
]['totalCount']))
195 owner
= cloudcast
.get('owner') or {}
201 'description': cloudcast
.get('description'),
202 'thumbnail': try_get(cloudcast
, lambda x
: x
['picture']['url'], compat_str
),
203 'uploader': owner
.get('displayName'),
204 'timestamp': parse_iso8601(cloudcast
.get('publishDate')),
205 'uploader_id': owner
.get('username'),
206 'uploader_url': owner
.get('url'),
207 'duration': int_or_none(cloudcast
.get('audioLength')),
208 'view_count': int_or_none(cloudcast
.get('plays')),
209 'like_count': get_count('favorites'),
210 'repost_count': get_count('reposts'),
211 'comment_count': get_count('comments'),
212 'comments': comments
,
214 'artist': ', '.join(cloudcast
.get('featuringArtistList') or []) or None,
218 class MixcloudPlaylistBaseIE(MixcloudBaseIE
):
219 def _get_cloudcast(self
, node
):
222 def _get_playlist_title(self
, title
, slug
):
225 def _real_extract(self
, url
):
226 username
, slug
= self
._match
_valid
_url
(url
).groups()
227 username
= compat_urllib_parse_unquote(username
)
231 slug
= compat_urllib_parse_unquote(slug
)
232 playlist_id
= '%s_%s' % (username
, slug
)
234 is_playlist_type
= self
._ROOT
_TYPE
== 'playlist'
235 playlist_type
= 'items' if is_playlist_type
else slug
241 playlist
= self
._call
_api
(
242 self
._ROOT
_TYPE
, '''%s
254 }''' % (self
._TITLE
_KEY
, self
._DESCRIPTION
_KEY
, playlist_type
, list_filter
, self
._NODE
_TEMPLATE
),
255 playlist_id
, username
, slug
if is_playlist_type
else None)
257 items
= playlist
.get(playlist_type
) or {}
258 for edge
in items
.get('edges', []):
259 cloudcast
= self
._get
_cloudcast
(edge
.get('node') or {})
260 cloudcast_url
= cloudcast
.get('url')
261 if not cloudcast_url
:
263 slug
= try_get(cloudcast
, lambda x
: x
['slug'], compat_str
)
264 owner_username
= try_get(cloudcast
, lambda x
: x
['owner']['username'], compat_str
)
265 video_id
= '%s_%s' % (owner_username
, slug
) if slug
and owner_username
else None
266 entries
.append(self
.url_result(
267 cloudcast_url
, MixcloudIE
.ie_key(), video_id
))
269 page_info
= items
['pageInfo']
270 has_next_page
= page_info
['hasNextPage']
271 list_filter
= ', after: "%s"' % page_info
['endCursor']
273 return self
.playlist_result(
274 entries
, playlist_id
,
275 self
._get
_playlist
_title
(playlist
[self
._TITLE
_KEY
], slug
),
276 playlist
.get(self
._DESCRIPTION
_KEY
))
279 class MixcloudUserIE(MixcloudPlaylistBaseIE
):
280 _VALID_URL
= r
'https?://(?:www\.)?mixcloud\.com/(?P<id>[^/]+)/(?P<type>uploads|favorites|listens|stream)?/?$'
281 IE_NAME
= 'mixcloud:user'
284 'url': 'http://www.mixcloud.com/dholbach/',
286 'id': 'dholbach_uploads',
287 'title': 'Daniel Holbach (uploads)',
288 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
290 'playlist_mincount': 36,
292 'url': 'http://www.mixcloud.com/dholbach/uploads/',
294 'id': 'dholbach_uploads',
295 'title': 'Daniel Holbach (uploads)',
296 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
298 'playlist_mincount': 36,
300 'url': 'http://www.mixcloud.com/dholbach/favorites/',
302 'id': 'dholbach_favorites',
303 'title': 'Daniel Holbach (favorites)',
304 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
307 # 'playlist_items': '1-100',
309 'playlist_mincount': 396,
311 'url': 'http://www.mixcloud.com/dholbach/listens/',
313 'id': 'dholbach_listens',
314 'title': 'Daniel Holbach (listens)',
315 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
318 # 'playlist_items': '1-100',
320 'playlist_mincount': 1623,
321 'skip': 'Large list',
323 'url': 'https://www.mixcloud.com/FirstEar/stream/',
325 'id': 'FirstEar_stream',
326 'title': 'First Ear (stream)',
327 'description': 'Curators of good music\r\n\r\nfirstearmusic.com',
329 'playlist_mincount': 271,
332 _TITLE_KEY
= 'displayName'
333 _DESCRIPTION_KEY
= 'biog'
335 _NODE_TEMPLATE
= '''slug
337 owner { username }'''
339 def _get_playlist_title(self
, title
, slug
):
340 return '%s (%s)' % (title
, slug
)
343 class MixcloudPlaylistIE(MixcloudPlaylistBaseIE
):
344 _VALID_URL
= r
'https?://(?:www\.)?mixcloud\.com/(?P<user>[^/]+)/playlists/(?P<playlist>[^/]+)/?$'
345 IE_NAME
= 'mixcloud:playlist'
348 'url': 'https://www.mixcloud.com/maxvibes/playlists/jazzcat-on-ness-radio/',
350 'id': 'maxvibes_jazzcat-on-ness-radio',
351 'title': 'Ness Radio sessions',
353 'playlist_mincount': 59,
356 _DESCRIPTION_KEY
= 'description'
357 _ROOT_TYPE
= 'playlist'
358 _NODE_TEMPLATE
= '''cloudcast {
364 def _get_cloudcast(self
, node
):
365 return node
.get('cloudcast') or {}