]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/mixcloud.py
[youtube] Fix error reporting of "Incomplete data"
[yt-dlp.git] / yt_dlp / extractor / mixcloud.py
CommitLineData
e6da9240 1import itertools
80cbb6dd
PH
2
3from .common import InfoExtractor
c96eca42 4from ..compat import (
5d7d805c 5 compat_b64decode,
dd91dfcd 6 compat_ord,
095774e5 7 compat_str,
c96eca42 8 compat_urllib_parse_unquote,
c96eca42 9)
1cc79574 10from ..utils import (
9040e2d6 11 ExtractorError,
095774e5 12 int_or_none,
5d92b407
RA
13 parse_iso8601,
14 strip_or_none,
095774e5 15 try_get,
095774e5 16)
80cbb6dd
PH
17
18
5d92b407
RA
19class 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={
24 'query': '''{
25 %s(lookup: {username: "%s"%s}) {
26 %s
27 }
28}''' % (lookup_key, username, ', slug: "%s"' % slug if slug else '', object_fields)
29 })['data'][lookup_key]
30
31
32class MixcloudIE(MixcloudBaseIE):
655cb545 33 _VALID_URL = r'https?://(?:(?:www|beta|m)\.)?mixcloud\.com/([^/]+)/(?!stream|uploads|favorites|listens|playlists)([^/]+)'
d0390a0c 34 IE_NAME = 'mixcloud'
80cbb6dd 35
58ba6c01 36 _TESTS = [{
d0390a0c 37 'url': 'http://www.mixcloud.com/dholbach/cryptkeeper/',
d0390a0c 38 'info_dict': {
5d92b407 39 'id': 'dholbach_cryptkeeper',
f896e1cc 40 'ext': 'm4a',
d0390a0c
PH
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',
ec85ded8 45 'thumbnail': r're:https?://.*\.jpg',
57c7411f 46 'view_count': int,
5d92b407
RA
47 'timestamp': 1321359578,
48 'upload_date': '20111115',
19e1d359 49 },
58ba6c01
S
50 }, {
51 'url': 'http://www.mixcloud.com/gillespeterson/caribou-7-inch-vinyl-mix-chat/',
52 'info_dict': {
5d92b407 53 'id': 'gillespeterson_caribou-7-inch-vinyl-mix-chat',
7a757b71
JMF
54 'ext': 'mp3',
55 'title': 'Caribou 7 inch Vinyl Mix & Chat',
58ba6c01 56 'description': 'md5:2b8aec6adce69f9d41724647c65875e8',
7a757b71 57 'uploader': 'Gilles Peterson Worldwide',
58ba6c01 58 'uploader_id': 'gillespeterson',
dd91dfcd 59 'thumbnail': 're:https?://.*',
58ba6c01 60 'view_count': int,
5d92b407
RA
61 'timestamp': 1422987057,
62 'upload_date': '20150203',
58ba6c01 63 },
655cb545
S
64 }, {
65 'url': 'https://beta.mixcloud.com/RedLightRadio/nosedrip-15-red-light-radio-01-18-2016/',
66 'only_matching': True,
58ba6c01 67 }]
5d92b407 68 _DECRYPTION_KEY = 'IFYOUWANTTHEARTISTSTOGETPAIDDONOTDOWNLOADFROMMIXCLOUD'
80cbb6dd 69
2384f5a6
TI
70 @staticmethod
71 def _decrypt_xor_cipher(key, ciphertext):
72 """Encrypt/Decrypt XOR cipher. Both ways are possible because it's XOR."""
73 return ''.join([
ac668111 74 chr(compat_ord(ch) ^ compat_ord(k))
f9934b96 75 for ch, k in zip(ciphertext, itertools.cycle(key))])
2384f5a6 76
80cbb6dd 77 def _real_extract(self, url):
5ad28e7f 78 username, slug = self._match_valid_url(url).groups()
5d92b407
RA
79 username, slug = compat_urllib_parse_unquote(username), compat_urllib_parse_unquote(slug)
80 track_id = '%s_%s' % (username, slug)
81
82 cloudcast = self._call_api('cloudcast', '''audioLength
83 comments(first: 100) {
84 edges {
85 node {
86 comment
87 created
88 user {
89 displayName
90 username
91 }
92 }
93 }
94 totalCount
95 }
96 description
97 favorites {
98 totalCount
99 }
100 featuringArtistList
101 isExclusive
102 name
103 owner {
104 displayName
105 url
106 username
107 }
108 picture(width: 1024, height: 1024) {
109 url
110 }
111 plays
112 publishDate
113 reposts {
114 totalCount
115 }
116 streamInfo {
117 dashUrl
118 hlsUrl
119 url
120 }
121 tags {
122 tag {
123 name
124 }
9040e2d6
L
125 }
126 restrictedReason
127 id''', track_id, username, slug)
128
129 if not cloudcast:
130 raise ExtractorError('Track not found', expected=True)
131
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)
137 elif reason:
138 raise ExtractorError('Track is restricted', expected=True)
dd2535c3 139
5d92b407 140 title = cloudcast['name']
19e1d359 141
5d92b407
RA
142 stream_info = cloudcast['streamInfo']
143 formats = []
2384f5a6 144
5d92b407
RA
145 for url_key in ('url', 'hlsUrl', 'dashUrl'):
146 format_url = stream_info.get(url_key)
147 if not format_url:
148 continue
149 decrypted = self._decrypt_xor_cipher(
150 self._DECRYPTION_KEY, compat_b64decode(format_url))
151 if url_key == 'hlsUrl':
152 formats.extend(self._extract_m3u8_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))
2384f5a6 158 else:
5d92b407
RA
159 formats.append({
160 'format_id': 'http',
161 'url': decrypted,
162 'downloader_options': {
163 # Mixcloud starts throttling at >~5M
164 'http_chunk_size': 5242880,
165 },
166 })
167
168 if not formats and cloudcast.get('isExclusive'):
b7da73eb 169 self.raise_login_required(metadata_available=True)
5d92b407
RA
170
171 self._sort_formats(formats)
172
173 comments = []
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'))
177 if not text:
2384f5a6 178 continue
5d92b407
RA
179 user = node.get('user') or {}
180 comments.append({
181 'author': user.get('displayName'),
182 'author_id': user.get('username'),
183 'text': text,
184 'timestamp': parse_iso8601(node.get('created')),
185 })
2384f5a6 186
5d92b407
RA
187 tags = []
188 for t in cloudcast.get('tags'):
189 tag = try_get(t, lambda x: x['tag']['name'], compat_str)
190 if not tag:
191 tags.append(tag)
192
193 get_count = lambda x: int_or_none(try_get(cloudcast, lambda y: y[x]['totalCount']))
194
195 owner = cloudcast.get('owner') or {}
19e1d359
JMF
196
197 return {
198 'id': track_id,
57c7411f 199 'title': title,
2384f5a6 200 'formats': formats,
5d92b407
RA
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,
213 'tags': tags,
214 'artist': ', '.join(cloudcast.get('featuringArtistList') or []) or None,
19e1d359 215 }
c96eca42
PH
216
217
5d92b407
RA
218class MixcloudPlaylistBaseIE(MixcloudBaseIE):
219 def _get_cloudcast(self, node):
220 return node
c96eca42 221
5d92b407
RA
222 def _get_playlist_title(self, title, slug):
223 return title
224
225 def _real_extract(self, url):
5ad28e7f 226 username, slug = self._match_valid_url(url).groups()
5d92b407
RA
227 username = compat_urllib_parse_unquote(username)
228 if not slug:
229 slug = 'uploads'
230 else:
231 slug = compat_urllib_parse_unquote(slug)
232 playlist_id = '%s_%s' % (username, slug)
e6da9240 233
5d92b407
RA
234 is_playlist_type = self._ROOT_TYPE == 'playlist'
235 playlist_type = 'items' if is_playlist_type else slug
236 list_filter = ''
9c250931 237
5d92b407
RA
238 has_next_page = True
239 entries = []
240 while has_next_page:
241 playlist = self._call_api(
242 self._ROOT_TYPE, '''%s
243 %s
244 %s(first: 100%s) {
245 edges {
246 node {
247 %s
248 }
249 }
250 pageInfo {
251 endCursor
252 hasNextPage
253 }
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)
256
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:
262 continue
30a074c2 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
5d92b407 266 entries.append(self.url_result(
30a074c2 267 cloudcast_url, MixcloudIE.ie_key(), video_id))
e6da9240 268
5d92b407
RA
269 page_info = items['pageInfo']
270 has_next_page = page_info['hasNextPage']
271 list_filter = ', after: "%s"' % page_info['endCursor']
9c250931 272
5d92b407
RA
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))
9c250931
YCH
277
278
279class MixcloudUserIE(MixcloudPlaylistBaseIE):
5d92b407 280 _VALID_URL = r'https?://(?:www\.)?mixcloud\.com/(?P<id>[^/]+)/(?P<type>uploads|favorites|listens|stream)?/?$'
c96eca42
PH
281 IE_NAME = 'mixcloud:user'
282
283 _TESTS = [{
284 'url': 'http://www.mixcloud.com/dholbach/',
285 'info_dict': {
9c250931 286 'id': 'dholbach_uploads',
c96eca42 287 'title': 'Daniel Holbach (uploads)',
5d92b407 288 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
c96eca42 289 },
5d92b407 290 'playlist_mincount': 36,
c96eca42
PH
291 }, {
292 'url': 'http://www.mixcloud.com/dholbach/uploads/',
293 'info_dict': {
9c250931 294 'id': 'dholbach_uploads',
c96eca42 295 'title': 'Daniel Holbach (uploads)',
5d92b407 296 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
c96eca42 297 },
5d92b407 298 'playlist_mincount': 36,
c96eca42
PH
299 }, {
300 'url': 'http://www.mixcloud.com/dholbach/favorites/',
301 'info_dict': {
9c250931 302 'id': 'dholbach_favorites',
c96eca42 303 'title': 'Daniel Holbach (favorites)',
5d92b407 304 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
9c250931 305 },
5d92b407
RA
306 # 'params': {
307 # 'playlist_items': '1-100',
308 # },
309 'playlist_mincount': 396,
c96eca42
PH
310 }, {
311 'url': 'http://www.mixcloud.com/dholbach/listens/',
312 'info_dict': {
9c250931 313 'id': 'dholbach_listens',
c96eca42 314 'title': 'Daniel Holbach (listens)',
5d92b407 315 'description': 'md5:b60d776f0bab534c5dabe0a34e47a789',
c96eca42 316 },
5d92b407
RA
317 # 'params': {
318 # 'playlist_items': '1-100',
319 # },
320 'playlist_mincount': 1623,
321 'skip': 'Large list',
322 }, {
323 'url': 'https://www.mixcloud.com/FirstEar/stream/',
324 'info_dict': {
325 'id': 'FirstEar_stream',
326 'title': 'First Ear (stream)',
327 'description': 'Curators of good music\r\n\r\nfirstearmusic.com',
9c250931 328 },
5d92b407 329 'playlist_mincount': 271,
c96eca42
PH
330 }]
331
5d92b407
RA
332 _TITLE_KEY = 'displayName'
333 _DESCRIPTION_KEY = 'biog'
334 _ROOT_TYPE = 'user'
335 _NODE_TEMPLATE = '''slug
30a074c2 336 url
337 owner { username }'''
c96eca42 338
5d92b407
RA
339 def _get_playlist_title(self, title, slug):
340 return '%s (%s)' % (title, slug)
c96eca42 341
c96eca42 342
9c250931 343class MixcloudPlaylistIE(MixcloudPlaylistBaseIE):
29c67266 344 _VALID_URL = r'https?://(?:www\.)?mixcloud\.com/(?P<user>[^/]+)/playlists/(?P<playlist>[^/]+)/?$'
c96eca42
PH
345 IE_NAME = 'mixcloud:playlist'
346
347 _TESTS = [{
c96eca42 348 'url': 'https://www.mixcloud.com/maxvibes/playlists/jazzcat-on-ness-radio/',
e6da9240 349 'info_dict': {
5d92b407
RA
350 'id': 'maxvibes_jazzcat-on-ness-radio',
351 'title': 'Ness Radio sessions',
e6da9240 352 },
5d92b407
RA
353 'playlist_mincount': 59,
354 }]
355 _TITLE_KEY = 'name'
356 _DESCRIPTION_KEY = 'description'
357 _ROOT_TYPE = 'playlist'
358 _NODE_TEMPLATE = '''cloudcast {
359 slug
360 url
30a074c2 361 owner { username }
5d92b407
RA
362 }'''
363
364 def _get_cloudcast(self, node):
365 return node.get('cloudcast') or {}