1 from __future__
import unicode_literals
5 from .common
import InfoExtractor
11 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
= self
._match
_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) {
131 id''', track_id
, username
, slug
)
134 raise ExtractorError('Track not found', expected
=True)
136 reason
= cloudcast
.get('restrictedReason')
137 if reason
== 'tracklist':
138 raise ExtractorError('Track unavailable in your country due to licensing restrictions', expected
=True)
139 elif reason
== 'repeat_play':
140 raise ExtractorError('You have reached your play limit for this track', expected
=True)
142 raise ExtractorError('Track is restricted', expected
=True)
144 title
= cloudcast
['name']
146 stream_info
= cloudcast
['streamInfo']
149 for url_key
in ('url', 'hlsUrl', 'dashUrl'):
150 format_url
= stream_info
.get(url_key
)
153 decrypted
= self
._decrypt
_xor
_cipher
(
154 self
._DECRYPTION
_KEY
, compat_b64decode(format_url
))
155 if url_key
== 'hlsUrl':
156 formats
.extend(self
._extract
_m
3u8_formats
(
157 decrypted
, track_id
, 'mp4', entry_protocol
='m3u8_native',
158 m3u8_id
='hls', fatal
=False))
159 elif url_key
== 'dashUrl':
160 formats
.extend(self
._extract
_mpd
_formats
(
161 decrypted
, track_id
, mpd_id
='dash', fatal
=False))
166 'downloader_options': {
167 # Mixcloud starts throttling at >~5M
168 'http_chunk_size': 5242880,
172 if not formats
and cloudcast
.get('isExclusive'):
173 self
.raise_login_required(metadata_available
=True)
175 self
._sort
_formats
(formats
)
178 for edge
in (try_get(cloudcast
, lambda x
: x
['comments']['edges']) or []):
179 node
= edge
.get('node') or {}
180 text
= strip_or_none(node
.get('comment'))
183 user
= node
.get('user') or {}
185 'author': user
.get('displayName'),
186 'author_id': user
.get('username'),
188 'timestamp': parse_iso8601(node
.get('created')),
192 for t
in cloudcast
.get('tags'):
193 tag
= try_get(t
, lambda x
: x
['tag']['name'], compat_str
)
197 get_count
= lambda x
: int_or_none(try_get(cloudcast
, lambda y
: y
[x
]['totalCount']))
199 owner
= cloudcast
.get('owner') or {}
205 'description': cloudcast
.get('description'),
206 'thumbnail': try_get(cloudcast
, lambda x
: x
['picture']['url'], compat_str
),
207 'uploader': owner
.get('displayName'),
208 'timestamp': parse_iso8601(cloudcast
.get('publishDate')),
209 'uploader_id': owner
.get('username'),
210 'uploader_url': owner
.get('url'),
211 'duration': int_or_none(cloudcast
.get('audioLength')),
212 'view_count': int_or_none(cloudcast
.get('plays')),
213 'like_count': get_count('favorites'),
214 'repost_count': get_count('reposts'),
215 'comment_count': get_count('comments'),
216 'comments': comments
,
218 'artist': ', '.join(cloudcast
.get('featuringArtistList') or []) or None,
222 class MixcloudPlaylistBaseIE(MixcloudBaseIE
):
223 def _get_cloudcast(self
, node
):
226 def _get_playlist_title(self
, title
, slug
):
229 def _real_extract(self
, url
):
230 username
, slug
= self
._match
_valid
_url
(url
).groups()
231 username
= compat_urllib_parse_unquote(username
)
235 slug
= compat_urllib_parse_unquote(slug
)
236 playlist_id
= '%s_%s' % (username
, slug
)
238 is_playlist_type
= self
._ROOT
_TYPE
== 'playlist'
239 playlist_type
= 'items' if is_playlist_type
else slug
245 playlist
= self
._call
_api
(
246 self
._ROOT
_TYPE
, '''%s
258 }''' % (self
._TITLE
_KEY
, self
._DESCRIPTION
_KEY
, playlist_type
, list_filter
, self
._NODE
_TEMPLATE
),
259 playlist_id
, username
, slug
if is_playlist_type
else None)
261 items
= playlist
.get(playlist_type
) or {}
262 for edge
in items
.get('edges', []):
263 cloudcast
= self
._get
_cloudcast
(edge
.get('node') or {})
264 cloudcast_url
= cloudcast
.get('url')
265 if not cloudcast_url
:
267 slug
= try_get(cloudcast
, lambda x
: x
['slug'], compat_str
)
268 owner_username
= try_get(cloudcast
, lambda x
: x
['owner']['username'], compat_str
)
269 video_id
= '%s_%s' % (owner_username
, slug
) if slug
and owner_username
else None
270 entries
.append(self
.url_result(
271 cloudcast_url
, MixcloudIE
.ie_key(), video_id
))
273 page_info
= items
['pageInfo']
274 has_next_page
= page_info
['hasNextPage']
275 list_filter
= ', after: "%s"' % page_info
['endCursor']
277 return self
.playlist_result(
278 entries
, playlist_id
,
279 self
._get
_playlist
_title
(playlist
[self
._TITLE
_KEY
], slug
),
280 playlist
.get(self
._DESCRIPTION
_KEY
))
283 class MixcloudUserIE(MixcloudPlaylistBaseIE
):
284 _VALID_URL
= r
'https?://(?:www\.)?mixcloud\.com/(?P<id>[^/]+)/(?P<type>uploads|favorites|listens|stream)?/?$'
285 IE_NAME
= 'mixcloud:user'
288 'url': 'http://www.mixcloud.com/dholbach/',
290 'id': 'dholbach_uploads',
291 'title': 'Daniel Holbach (uploads)',
292 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
294 'playlist_mincount': 36,
296 'url': 'http://www.mixcloud.com/dholbach/uploads/',
298 'id': 'dholbach_uploads',
299 'title': 'Daniel Holbach (uploads)',
300 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
302 'playlist_mincount': 36,
304 'url': 'http://www.mixcloud.com/dholbach/favorites/',
306 'id': 'dholbach_favorites',
307 'title': 'Daniel Holbach (favorites)',
308 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
311 # 'playlist_items': '1-100',
313 'playlist_mincount': 396,
315 'url': 'http://www.mixcloud.com/dholbach/listens/',
317 'id': 'dholbach_listens',
318 'title': 'Daniel Holbach (listens)',
319 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
322 # 'playlist_items': '1-100',
324 'playlist_mincount': 1623,
325 'skip': 'Large list',
327 'url': 'https://www.mixcloud.com/FirstEar/stream/',
329 'id': 'FirstEar_stream',
330 'title': 'First Ear (stream)',
331 'description': 'Curators of good music\r\n\r\nfirstearmusic.com',
333 'playlist_mincount': 271,
336 _TITLE_KEY
= 'displayName'
337 _DESCRIPTION_KEY
= 'biog'
339 _NODE_TEMPLATE
= '''slug
341 owner { username }'''
343 def _get_playlist_title(self
, title
, slug
):
344 return '%s (%s)' % (title
, slug
)
347 class MixcloudPlaylistIE(MixcloudPlaylistBaseIE
):
348 _VALID_URL
= r
'https?://(?:www\.)?mixcloud\.com/(?P<user>[^/]+)/playlists/(?P<playlist>[^/]+)/?$'
349 IE_NAME
= 'mixcloud:playlist'
352 'url': 'https://www.mixcloud.com/maxvibes/playlists/jazzcat-on-ness-radio/',
354 'id': 'maxvibes_jazzcat-on-ness-radio',
355 'title': 'Ness Radio sessions',
357 'playlist_mincount': 59,
360 _DESCRIPTION_KEY
= 'description'
361 _ROOT_TYPE
= 'playlist'
362 _NODE_TEMPLATE
= '''cloudcast {
368 def _get_cloudcast(self
, node
):
369 return node
.get('cloudcast') or {}