1 from __future__
import unicode_literals
5 from .common
import InfoExtractor
11 compat_urllib_parse_unquote
,
22 class MixcloudBaseIE(InfoExtractor
):
23 def _call_api(self
, object_type
, object_fields
, display_id
, username
, slug
=None):
24 lookup_key
= object_type
+ 'Lookup'
25 return self
._download
_json
(
26 'https://www.mixcloud.com/graphql', display_id
, query
={
28 %s(lookup: {username: "%s"%s}) {
31 }''' % (lookup_key
, username
, ', slug: "%s"' % slug
if slug
else '', object_fields
)
32 })['data'][lookup_key
]
35 class MixcloudIE(MixcloudBaseIE
):
36 _VALID_URL
= r
'https?://(?:(?:www|beta|m)\.)?mixcloud\.com/([^/]+)/(?!stream|uploads|favorites|listens|playlists)([^/]+)'
40 'url': 'http://www.mixcloud.com/dholbach/cryptkeeper/',
42 'id': 'dholbach_cryptkeeper',
44 'title': 'Cryptkeeper',
45 'description': 'After quite a long silence from myself, finally another Drum\'n\'Bass mix with my favourite current dance floor bangers.',
46 'uploader': 'Daniel Holbach',
47 'uploader_id': 'dholbach',
48 'thumbnail': r
're:https?://.*\.jpg',
50 'timestamp': 1321359578,
51 'upload_date': '20111115',
54 'url': 'http://www.mixcloud.com/gillespeterson/caribou-7-inch-vinyl-mix-chat/',
56 'id': 'gillespeterson_caribou-7-inch-vinyl-mix-chat',
58 'title': 'Caribou 7 inch Vinyl Mix & Chat',
59 'description': 'md5:2b8aec6adce69f9d41724647c65875e8',
60 'uploader': 'Gilles Peterson Worldwide',
61 'uploader_id': 'gillespeterson',
62 'thumbnail': 're:https?://.*',
64 'timestamp': 1422987057,
65 'upload_date': '20150203',
68 'url': 'https://beta.mixcloud.com/RedLightRadio/nosedrip-15-red-light-radio-01-18-2016/',
69 'only_matching': True,
71 _DECRYPTION_KEY
= 'IFYOUWANTTHEARTISTSTOGETPAIDDONOTDOWNLOADFROMMIXCLOUD'
74 def _decrypt_xor_cipher(key
, ciphertext
):
75 """Encrypt/Decrypt XOR cipher. Both ways are possible because it's XOR."""
77 compat_chr(compat_ord(ch
) ^
compat_ord(k
))
78 for ch
, k
in compat_zip(ciphertext
, itertools
.cycle(key
))])
80 def _real_extract(self
, url
):
81 username
, slug
= self
._match
_valid
_url
(url
).groups()
82 username
, slug
= compat_urllib_parse_unquote(username
), compat_urllib_parse_unquote(slug
)
83 track_id
= '%s_%s' % (username
, slug
)
85 cloudcast
= self
._call
_api
('cloudcast', '''audioLength
86 comments(first: 100) {
111 picture(width: 1024, height: 1024) {
128 }''', track_id
, username
, slug
)
130 title
= cloudcast
['name']
132 stream_info
= cloudcast
['streamInfo']
135 for url_key
in ('url', 'hlsUrl', 'dashUrl'):
136 format_url
= stream_info
.get(url_key
)
139 decrypted
= self
._decrypt
_xor
_cipher
(
140 self
._DECRYPTION
_KEY
, compat_b64decode(format_url
))
141 if url_key
== 'hlsUrl':
142 formats
.extend(self
._extract
_m
3u8_formats
(
143 decrypted
, track_id
, 'mp4', entry_protocol
='m3u8_native',
144 m3u8_id
='hls', fatal
=False))
145 elif url_key
== 'dashUrl':
146 formats
.extend(self
._extract
_mpd
_formats
(
147 decrypted
, track_id
, mpd_id
='dash', fatal
=False))
152 'downloader_options': {
153 # Mixcloud starts throttling at >~5M
154 'http_chunk_size': 5242880,
158 if not formats
and cloudcast
.get('isExclusive'):
159 self
.raise_login_required(metadata_available
=True)
161 self
._sort
_formats
(formats
)
164 for edge
in (try_get(cloudcast
, lambda x
: x
['comments']['edges']) or []):
165 node
= edge
.get('node') or {}
166 text
= strip_or_none(node
.get('comment'))
169 user
= node
.get('user') or {}
171 'author': user
.get('displayName'),
172 'author_id': user
.get('username'),
174 'timestamp': parse_iso8601(node
.get('created')),
178 for t
in cloudcast
.get('tags'):
179 tag
= try_get(t
, lambda x
: x
['tag']['name'], compat_str
)
183 get_count
= lambda x
: int_or_none(try_get(cloudcast
, lambda y
: y
[x
]['totalCount']))
185 owner
= cloudcast
.get('owner') or {}
191 'description': cloudcast
.get('description'),
192 'thumbnail': try_get(cloudcast
, lambda x
: x
['picture']['url'], compat_str
),
193 'uploader': owner
.get('displayName'),
194 'timestamp': parse_iso8601(cloudcast
.get('publishDate')),
195 'uploader_id': owner
.get('username'),
196 'uploader_url': owner
.get('url'),
197 'duration': int_or_none(cloudcast
.get('audioLength')),
198 'view_count': int_or_none(cloudcast
.get('plays')),
199 'like_count': get_count('favorites'),
200 'repost_count': get_count('reposts'),
201 'comment_count': get_count('comments'),
202 'comments': comments
,
204 'artist': ', '.join(cloudcast
.get('featuringArtistList') or []) or None,
208 class MixcloudPlaylistBaseIE(MixcloudBaseIE
):
209 def _get_cloudcast(self
, node
):
212 def _get_playlist_title(self
, title
, slug
):
215 def _real_extract(self
, url
):
216 username
, slug
= self
._match
_valid
_url
(url
).groups()
217 username
= compat_urllib_parse_unquote(username
)
221 slug
= compat_urllib_parse_unquote(slug
)
222 playlist_id
= '%s_%s' % (username
, slug
)
224 is_playlist_type
= self
._ROOT
_TYPE
== 'playlist'
225 playlist_type
= 'items' if is_playlist_type
else slug
231 playlist
= self
._call
_api
(
232 self
._ROOT
_TYPE
, '''%s
244 }''' % (self
._TITLE
_KEY
, self
._DESCRIPTION
_KEY
, playlist_type
, list_filter
, self
._NODE
_TEMPLATE
),
245 playlist_id
, username
, slug
if is_playlist_type
else None)
247 items
= playlist
.get(playlist_type
) or {}
248 for edge
in items
.get('edges', []):
249 cloudcast
= self
._get
_cloudcast
(edge
.get('node') or {})
250 cloudcast_url
= cloudcast
.get('url')
251 if not cloudcast_url
:
253 slug
= try_get(cloudcast
, lambda x
: x
['slug'], compat_str
)
254 owner_username
= try_get(cloudcast
, lambda x
: x
['owner']['username'], compat_str
)
255 video_id
= '%s_%s' % (owner_username
, slug
) if slug
and owner_username
else None
256 entries
.append(self
.url_result(
257 cloudcast_url
, MixcloudIE
.ie_key(), video_id
))
259 page_info
= items
['pageInfo']
260 has_next_page
= page_info
['hasNextPage']
261 list_filter
= ', after: "%s"' % page_info
['endCursor']
263 return self
.playlist_result(
264 entries
, playlist_id
,
265 self
._get
_playlist
_title
(playlist
[self
._TITLE
_KEY
], slug
),
266 playlist
.get(self
._DESCRIPTION
_KEY
))
269 class MixcloudUserIE(MixcloudPlaylistBaseIE
):
270 _VALID_URL
= r
'https?://(?:www\.)?mixcloud\.com/(?P<id>[^/]+)/(?P<type>uploads|favorites|listens|stream)?/?$'
271 IE_NAME
= 'mixcloud:user'
274 'url': 'http://www.mixcloud.com/dholbach/',
276 'id': 'dholbach_uploads',
277 'title': 'Daniel Holbach (uploads)',
278 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
280 'playlist_mincount': 36,
282 'url': 'http://www.mixcloud.com/dholbach/uploads/',
284 'id': 'dholbach_uploads',
285 'title': 'Daniel Holbach (uploads)',
286 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
288 'playlist_mincount': 36,
290 'url': 'http://www.mixcloud.com/dholbach/favorites/',
292 'id': 'dholbach_favorites',
293 'title': 'Daniel Holbach (favorites)',
294 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
297 # 'playlist_items': '1-100',
299 'playlist_mincount': 396,
301 'url': 'http://www.mixcloud.com/dholbach/listens/',
303 'id': 'dholbach_listens',
304 'title': 'Daniel Holbach (listens)',
305 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
308 # 'playlist_items': '1-100',
310 'playlist_mincount': 1623,
311 'skip': 'Large list',
313 'url': 'https://www.mixcloud.com/FirstEar/stream/',
315 'id': 'FirstEar_stream',
316 'title': 'First Ear (stream)',
317 'description': 'Curators of good music\r\n\r\nfirstearmusic.com',
319 'playlist_mincount': 271,
322 _TITLE_KEY
= 'displayName'
323 _DESCRIPTION_KEY
= 'biog'
325 _NODE_TEMPLATE
= '''slug
327 owner { username }'''
329 def _get_playlist_title(self
, title
, slug
):
330 return '%s (%s)' % (title
, slug
)
333 class MixcloudPlaylistIE(MixcloudPlaylistBaseIE
):
334 _VALID_URL
= r
'https?://(?:www\.)?mixcloud\.com/(?P<user>[^/]+)/playlists/(?P<playlist>[^/]+)/?$'
335 IE_NAME
= 'mixcloud:playlist'
338 'url': 'https://www.mixcloud.com/maxvibes/playlists/jazzcat-on-ness-radio/',
340 'id': 'maxvibes_jazzcat-on-ness-radio',
341 'title': 'Ness Radio sessions',
343 'playlist_mincount': 59,
346 _DESCRIPTION_KEY
= 'description'
347 _ROOT_TYPE
= 'playlist'
348 _NODE_TEMPLATE
= '''cloudcast {
354 def _get_cloudcast(self
, node
):
355 return node
.get('cloudcast') or {}