]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/bilibili.py
[cleanup] Lint and misc cleanup
[yt-dlp.git] / yt_dlp / extractor / bilibili.py
1 import base64
2 import functools
3 import itertools
4 import math
5 import urllib.error
6 import urllib.parse
7
8 from .common import InfoExtractor, SearchInfoExtractor
9 from ..utils import (
10 ExtractorError,
11 GeoRestrictedError,
12 InAdvancePagedList,
13 OnDemandPagedList,
14 filter_dict,
15 float_or_none,
16 format_field,
17 int_or_none,
18 make_archive_id,
19 mimetype2ext,
20 parse_count,
21 parse_qs,
22 qualities,
23 srt_subtitles_timecode,
24 str_or_none,
25 traverse_obj,
26 url_or_none,
27 urlencode_postdata,
28 )
29
30
31 class BilibiliBaseIE(InfoExtractor):
32 def extract_formats(self, play_info):
33 format_names = {
34 r['quality']: traverse_obj(r, 'new_description', 'display_desc')
35 for r in traverse_obj(play_info, ('support_formats', lambda _, v: v['quality']))
36 }
37
38 audios = traverse_obj(play_info, ('dash', 'audio', ...))
39 flac_audio = traverse_obj(play_info, ('dash', 'flac', 'audio'))
40 if flac_audio:
41 audios.append(flac_audio)
42 formats = [{
43 'url': traverse_obj(audio, 'baseUrl', 'base_url', 'url'),
44 'ext': mimetype2ext(traverse_obj(audio, 'mimeType', 'mime_type')),
45 'acodec': audio.get('codecs'),
46 'vcodec': 'none',
47 'tbr': float_or_none(audio.get('bandwidth'), scale=1000),
48 'filesize': int_or_none(audio.get('size'))
49 } for audio in audios]
50
51 formats.extend({
52 'url': traverse_obj(video, 'baseUrl', 'base_url', 'url'),
53 'ext': mimetype2ext(traverse_obj(video, 'mimeType', 'mime_type')),
54 'fps': float_or_none(traverse_obj(video, 'frameRate', 'frame_rate')),
55 'width': int_or_none(video.get('width')),
56 'height': int_or_none(video.get('height')),
57 'vcodec': video.get('codecs'),
58 'acodec': 'none' if audios else None,
59 'tbr': float_or_none(video.get('bandwidth'), scale=1000),
60 'filesize': int_or_none(video.get('size')),
61 'quality': int_or_none(video.get('id')),
62 'format': format_names.get(video.get('id')),
63 } for video in traverse_obj(play_info, ('dash', 'video', ...)))
64
65 missing_formats = format_names.keys() - set(traverse_obj(formats, (..., 'quality')))
66 if missing_formats:
67 self.to_screen(f'Format(s) {", ".join(format_names[i] for i in missing_formats)} are missing; '
68 'you have to login or become premium member to download them')
69
70 self._sort_formats(formats)
71 return formats
72
73 def json2srt(self, json_data):
74 srt_data = ''
75 for idx, line in enumerate(json_data.get('body') or []):
76 srt_data += (f'{idx + 1}\n'
77 f'{srt_subtitles_timecode(line["from"])} --> {srt_subtitles_timecode(line["to"])}\n'
78 f'{line["content"]}\n\n')
79 return srt_data
80
81 def _get_subtitles(self, video_id, initial_state, cid):
82 subtitles = {
83 'danmaku': [{
84 'ext': 'xml',
85 'url': f'https://comment.bilibili.com/{cid}.xml',
86 }]
87 }
88
89 for s in traverse_obj(initial_state, ('videoData', 'subtitle', 'list')) or []:
90 subtitles.setdefault(s['lan'], []).append({
91 'ext': 'srt',
92 'data': self.json2srt(self._download_json(s['subtitle_url'], video_id))
93 })
94 return subtitles
95
96 def _get_chapters(self, aid, cid):
97 chapters = aid and cid and self._download_json(
98 'https://api.bilibili.com/x/player/v2', aid, query={'aid': aid, 'cid': cid},
99 note='Extracting chapters', fatal=False)
100 return traverse_obj(chapters, ('data', 'view_points', ..., {
101 'title': 'content',
102 'start_time': 'from',
103 'end_time': 'to',
104 })) or None
105
106 def _get_comments(self, aid):
107 for idx in itertools.count(1):
108 replies = traverse_obj(
109 self._download_json(
110 f'https://api.bilibili.com/x/v2/reply?pn={idx}&oid={aid}&type=1&jsonp=jsonp&sort=2&_=1567227301685',
111 aid, note=f'Extracting comments from page {idx}', fatal=False),
112 ('data', 'replies'))
113 if not replies:
114 return
115 for children in map(self._get_all_children, replies):
116 yield from children
117
118 def _get_all_children(self, reply):
119 yield {
120 'author': traverse_obj(reply, ('member', 'uname')),
121 'author_id': traverse_obj(reply, ('member', 'mid')),
122 'id': reply.get('rpid'),
123 'text': traverse_obj(reply, ('content', 'message')),
124 'timestamp': reply.get('ctime'),
125 'parent': reply.get('parent') or 'root',
126 }
127 for children in map(self._get_all_children, traverse_obj(reply, ('replies', ...))):
128 yield from children
129
130
131 class BiliBiliIE(BilibiliBaseIE):
132 _VALID_URL = r'https?://www\.bilibili\.com/video/[aAbB][vV](?P<id>[^/?#&]+)'
133
134 _TESTS = [{
135 'url': 'https://www.bilibili.com/video/BV13x41117TL',
136 'info_dict': {
137 'id': 'BV13x41117TL',
138 'title': '阿滴英文|英文歌分享#6 "Closer',
139 'ext': 'mp4',
140 'description': '滴妹今天唱Closer給你聽! 有史以来,被推最多次也是最久的歌曲,其实歌词跟我原本想像差蛮多的,不过还是好听! 微博@阿滴英文',
141 'uploader_id': '65880958',
142 'uploader': '阿滴英文',
143 'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
144 'duration': 554.117,
145 'tags': list,
146 'comment_count': int,
147 'upload_date': '20170301',
148 'timestamp': 1488353834,
149 'like_count': int,
150 'view_count': int,
151 },
152 }, {
153 # old av URL version
154 'url': 'http://www.bilibili.com/video/av1074402/',
155 'info_dict': {
156 'thumbnail': r're:^https?://.*\.(jpg|jpeg)$',
157 'ext': 'mp4',
158 'uploader': '菊子桑',
159 'uploader_id': '156160',
160 'id': 'BV11x411K7CN',
161 'title': '【金坷垃】金泡沫',
162 'duration': 308.36,
163 'upload_date': '20140420',
164 'timestamp': 1397983878,
165 'description': 'md5:ce18c2a2d2193f0df2917d270f2e5923',
166 'like_count': int,
167 'comment_count': int,
168 'view_count': int,
169 'tags': list,
170 },
171 'params': {'skip_download': True},
172 }, {
173 'note': 'Anthology',
174 'url': 'https://www.bilibili.com/video/BV1bK411W797',
175 'info_dict': {
176 'id': 'BV1bK411W797',
177 'title': '物语中的人物是如何吐槽自己的OP的'
178 },
179 'playlist_count': 18,
180 'playlist': [{
181 'info_dict': {
182 'id': 'BV1bK411W797_p1',
183 'ext': 'mp4',
184 'title': '物语中的人物是如何吐槽自己的OP的 p01 Staple Stable/战场原+羽川',
185 'tags': 'count:11',
186 'timestamp': 1589601697,
187 'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
188 'uploader': '打牌还是打桩',
189 'uploader_id': '150259984',
190 'like_count': int,
191 'comment_count': int,
192 'upload_date': '20200516',
193 'view_count': int,
194 'description': 'md5:e3c401cf7bc363118d1783dd74068a68',
195 'duration': 90.314,
196 }
197 }]
198 }, {
199 'note': 'Specific page of Anthology',
200 'url': 'https://www.bilibili.com/video/BV1bK411W797?p=1',
201 'info_dict': {
202 'id': 'BV1bK411W797_p1',
203 'ext': 'mp4',
204 'title': '物语中的人物是如何吐槽自己的OP的 p01 Staple Stable/战场原+羽川',
205 'tags': 'count:11',
206 'timestamp': 1589601697,
207 'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
208 'uploader': '打牌还是打桩',
209 'uploader_id': '150259984',
210 'like_count': int,
211 'comment_count': int,
212 'upload_date': '20200516',
213 'view_count': int,
214 'description': 'md5:e3c401cf7bc363118d1783dd74068a68',
215 'duration': 90.314,
216 }
217 }, {
218 'note': 'video has subtitles',
219 'url': 'https://www.bilibili.com/video/BV12N4y1M7rh',
220 'info_dict': {
221 'id': 'BV12N4y1M7rh',
222 'ext': 'mp4',
223 'title': 'md5:96e8bb42c2b432c0d4ce3434a61479c1',
224 'tags': list,
225 'description': 'md5:afde2b7ba9025c01d9e3dde10de221e4',
226 'duration': 313.557,
227 'upload_date': '20220709',
228 'uploader': '小夫Tech',
229 'timestamp': 1657347907,
230 'uploader_id': '1326814124',
231 'comment_count': int,
232 'view_count': int,
233 'like_count': int,
234 'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
235 'subtitles': 'count:2'
236 },
237 'params': {'listsubtitles': True},
238 }, {
239 'url': 'https://www.bilibili.com/video/av8903802/',
240 'info_dict': {
241 'id': 'BV13x41117TL',
242 'ext': 'mp4',
243 'title': '阿滴英文|英文歌分享#6 "Closer',
244 'upload_date': '20170301',
245 'description': 'md5:3b1b9e25b78da4ef87e9b548b88ee76a',
246 'timestamp': 1488353834,
247 'uploader_id': '65880958',
248 'uploader': '阿滴英文',
249 'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
250 'duration': 554.117,
251 'tags': list,
252 'comment_count': int,
253 'view_count': int,
254 'like_count': int,
255 },
256 'params': {
257 'skip_download': True,
258 },
259 }, {
260 'note': 'video has chapter',
261 'url': 'https://www.bilibili.com/video/BV1vL411G7N7/',
262 'info_dict': {
263 'id': 'BV1vL411G7N7',
264 'ext': 'mp4',
265 'title': '如何为你的B站视频添加进度条分段',
266 'timestamp': 1634554558,
267 'upload_date': '20211018',
268 'description': 'md5:a9a3d6702b3a94518d419b2e9c320a6d',
269 'tags': list,
270 'uploader': '爱喝咖啡的当麻',
271 'duration': 669.482,
272 'uploader_id': '1680903',
273 'chapters': 'count:6',
274 'comment_count': int,
275 'view_count': int,
276 'like_count': int,
277 'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
278 },
279 'params': {'skip_download': True},
280 }]
281
282 def _real_extract(self, url):
283 video_id = self._match_id(url)
284 webpage = self._download_webpage(url, video_id)
285 initial_state = self._search_json(r'window\.__INITIAL_STATE__\s*=', webpage, 'initial state', video_id)
286 play_info = self._search_json(r'window\.__playinfo__\s*=', webpage, 'play info', video_id)['data']
287
288 video_data = initial_state['videoData']
289 video_id, title = video_data['bvid'], video_data.get('title')
290
291 # Bilibili anthologies are similar to playlists but all videos share the same video ID as the anthology itself.
292 page_list_json = traverse_obj(
293 self._download_json(
294 'https://api.bilibili.com/x/player/pagelist', video_id,
295 fatal=False, query={'bvid': video_id, 'jsonp': 'jsonp'},
296 note='Extracting videos in anthology'),
297 'data', expected_type=list) or []
298 is_anthology = len(page_list_json) > 1
299
300 part_id = int_or_none(parse_qs(url).get('p', [None])[-1])
301 if is_anthology and not part_id and self._yes_playlist(video_id, video_id):
302 return self.playlist_from_matches(
303 page_list_json, video_id, title, ie=BiliBiliIE,
304 getter=lambda entry: f'https://www.bilibili.com/video/{video_id}?p={entry["page"]}')
305
306 if is_anthology:
307 title += f' p{part_id:02d} {traverse_obj(page_list_json, ((part_id or 1) - 1, "part")) or ""}'
308
309 aid = video_data.get('aid')
310 old_video_id = format_field(aid, None, f'%s_part{part_id or 1}')
311
312 cid = traverse_obj(video_data, ('pages', part_id - 1, 'cid')) if part_id else video_data.get('cid')
313
314 return {
315 'id': f'{video_id}{format_field(part_id, None, "_p%d")}',
316 'formats': self.extract_formats(play_info),
317 '_old_archive_ids': [make_archive_id(self, old_video_id)] if old_video_id else None,
318 'title': title,
319 'description': traverse_obj(initial_state, ('videoData', 'desc')),
320 'view_count': traverse_obj(initial_state, ('videoData', 'stat', 'view')),
321 'uploader': traverse_obj(initial_state, ('upData', 'name')),
322 'uploader_id': traverse_obj(initial_state, ('upData', 'mid')),
323 'like_count': traverse_obj(initial_state, ('videoData', 'stat', 'like')),
324 'comment_count': traverse_obj(initial_state, ('videoData', 'stat', 'reply')),
325 'tags': traverse_obj(initial_state, ('tags', ..., 'tag_name')),
326 'thumbnail': traverse_obj(initial_state, ('videoData', 'pic')),
327 'timestamp': traverse_obj(initial_state, ('videoData', 'pubdate')),
328 'duration': float_or_none(play_info.get('timelength'), scale=1000),
329 'chapters': self._get_chapters(aid, cid),
330 'subtitles': self.extract_subtitles(video_id, initial_state, cid),
331 '__post_extractor': self.extract_comments(aid),
332 'http_headers': {'Referer': url},
333 }
334
335
336 class BiliBiliBangumiIE(BilibiliBaseIE):
337 _VALID_URL = r'(?x)https?://www\.bilibili\.com/bangumi/play/(?P<id>(?:ss|ep)\d+)'
338
339 _TESTS = [{
340 'url': 'https://www.bilibili.com/bangumi/play/ss897',
341 'info_dict': {
342 'id': 'ss897',
343 'ext': 'mp4',
344 'series': '神的记事本',
345 'season': '神的记事本',
346 'season_id': 897,
347 'season_number': 1,
348 'episode': '你与旅行包',
349 'episode_number': 2,
350 'title': '神的记事本:第2话 你与旅行包',
351 'duration': 1428.487,
352 'timestamp': 1310809380,
353 'upload_date': '20110716',
354 'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
355 },
356 }, {
357 'url': 'https://www.bilibili.com/bangumi/play/ep508406',
358 'only_matching': True,
359 }]
360
361 def _real_extract(self, url):
362 video_id = self._match_id(url)
363 webpage = self._download_webpage(url, video_id)
364
365 if '您所在的地区无法观看本片' in webpage:
366 raise GeoRestrictedError('This video is restricted')
367 elif ('开通大会员观看' in webpage and '__playinfo__' not in webpage
368 or '正在观看预览,大会员免费看全片' in webpage):
369 self.raise_login_required('This video is for premium members only')
370
371 play_info = self._search_json(r'window\.__playinfo__\s*=', webpage, 'play info', video_id)['data']
372 formats = self.extract_formats(play_info)
373 if (not formats and '成为大会员抢先看' in webpage
374 and play_info.get('durl') and not play_info.get('dash')):
375 self.raise_login_required('This video is for premium members only')
376
377 initial_state = self._search_json(r'window\.__INITIAL_STATE__\s*=', webpage, 'initial state', video_id)
378
379 season_id = traverse_obj(initial_state, ('mediaInfo', 'season_id'))
380 season_number = season_id and next((
381 idx + 1 for idx, e in enumerate(
382 traverse_obj(initial_state, ('mediaInfo', 'seasons', ...)))
383 if e.get('season_id') == season_id
384 ), None)
385
386 return {
387 'id': video_id,
388 'formats': formats,
389 'title': traverse_obj(initial_state, 'h1Title'),
390 'episode': traverse_obj(initial_state, ('epInfo', 'long_title')),
391 'episode_number': int_or_none(traverse_obj(initial_state, ('epInfo', 'title'))),
392 'series': traverse_obj(initial_state, ('mediaInfo', 'series')),
393 'season': traverse_obj(initial_state, ('mediaInfo', 'season_title')),
394 'season_id': season_id,
395 'season_number': season_number,
396 'thumbnail': traverse_obj(initial_state, ('epInfo', 'cover')),
397 'timestamp': traverse_obj(initial_state, ('epInfo', 'pub_time')),
398 'duration': float_or_none(play_info.get('timelength'), scale=1000),
399 'subtitles': self.extract_subtitles(
400 video_id, initial_state, traverse_obj(initial_state, ('epInfo', 'cid'))),
401 '__post_extractor': self.extract_comments(traverse_obj(initial_state, ('epInfo', 'aid'))),
402 'http_headers': {'Referer': url, **self.geo_verification_headers()},
403 }
404
405
406 class BiliBiliBangumiMediaIE(InfoExtractor):
407 _VALID_URL = r'https?://www\.bilibili\.com/bangumi/media/md(?P<id>\d+)'
408 _TESTS = [{
409 'url': 'https://www.bilibili.com/bangumi/media/md24097891',
410 'info_dict': {
411 'id': '24097891',
412 },
413 'playlist_mincount': 25,
414 }]
415
416 def _real_extract(self, url):
417 media_id = self._match_id(url)
418 webpage = self._download_webpage(url, media_id)
419
420 initial_state = self._search_json(r'window\.__INITIAL_STATE__\s*=', webpage, 'initial_state', media_id)
421 episode_list = self._download_json(
422 'https://api.bilibili.com/pgc/web/season/section', media_id,
423 query={'season_id': initial_state['mediaInfo']['season_id']},
424 note='Downloading season info')['result']['main_section']['episodes']
425
426 return self.playlist_result((
427 self.url_result(entry['share_url'], BiliBiliBangumiIE, entry['aid'])
428 for entry in episode_list), media_id)
429
430
431 class BilibiliSpaceBaseIE(InfoExtractor):
432 def _extract_playlist(self, fetch_page, get_metadata, get_entries):
433 first_page = fetch_page(0)
434 metadata = get_metadata(first_page)
435
436 paged_list = InAdvancePagedList(
437 lambda idx: get_entries(fetch_page(idx) if idx else first_page),
438 metadata['page_count'], metadata['page_size'])
439
440 return metadata, paged_list
441
442
443 class BilibiliSpaceVideoIE(BilibiliSpaceBaseIE):
444 _VALID_URL = r'https?://space\.bilibili\.com/(?P<id>\d+)(?P<video>/video)?/?(?:[?#]|$)'
445 _TESTS = [{
446 'url': 'https://space.bilibili.com/3985676/video',
447 'info_dict': {
448 'id': '3985676',
449 },
450 'playlist_mincount': 178,
451 }]
452
453 def _real_extract(self, url):
454 playlist_id, is_video_url = self._match_valid_url(url).group('id', 'video')
455 if not is_video_url:
456 self.to_screen('A channel URL was given. Only the channel\'s videos will be downloaded. '
457 'To download audios, add a "/audio" to the URL')
458
459 def fetch_page(page_idx):
460 try:
461 response = self._download_json('https://api.bilibili.com/x/space/arc/search',
462 playlist_id, note=f'Downloading page {page_idx}',
463 query={'mid': playlist_id, 'pn': page_idx + 1, 'jsonp': 'jsonp'})
464 except ExtractorError as e:
465 if isinstance(e.cause, urllib.error.HTTPError) and e.cause.code == 412:
466 raise ExtractorError(
467 'Request is blocked by server (412), please add cookies, wait and try later.', expected=True)
468 raise
469 if response['code'] == -401:
470 raise ExtractorError(
471 'Request is blocked by server (401), please add cookies, wait and try later.', expected=True)
472 return response['data']
473
474 def get_metadata(page_data):
475 page_size = page_data['page']['ps']
476 entry_count = page_data['page']['count']
477 return {
478 'page_count': math.ceil(entry_count / page_size),
479 'page_size': page_size,
480 }
481
482 def get_entries(page_data):
483 for entry in traverse_obj(page_data, ('list', 'vlist')) or []:
484 yield self.url_result(f'https://www.bilibili.com/video/{entry["bvid"]}', BiliBiliIE, entry['bvid'])
485
486 metadata, paged_list = self._extract_playlist(fetch_page, get_metadata, get_entries)
487 return self.playlist_result(paged_list, playlist_id)
488
489
490 class BilibiliSpaceAudioIE(BilibiliSpaceBaseIE):
491 _VALID_URL = r'https?://space\.bilibili\.com/(?P<id>\d+)/audio'
492 _TESTS = [{
493 'url': 'https://space.bilibili.com/3985676/audio',
494 'info_dict': {
495 'id': '3985676',
496 },
497 'playlist_mincount': 1,
498 }]
499
500 def _real_extract(self, url):
501 playlist_id = self._match_id(url)
502
503 def fetch_page(page_idx):
504 return self._download_json(
505 'https://api.bilibili.com/audio/music-service/web/song/upper', playlist_id,
506 note=f'Downloading page {page_idx}',
507 query={'uid': playlist_id, 'pn': page_idx + 1, 'ps': 30, 'order': 1, 'jsonp': 'jsonp'})['data']
508
509 def get_metadata(page_data):
510 return {
511 'page_count': page_data['pageCount'],
512 'page_size': page_data['pageSize'],
513 }
514
515 def get_entries(page_data):
516 for entry in page_data.get('data', []):
517 yield self.url_result(f'https://www.bilibili.com/audio/au{entry["id"]}', BilibiliAudioIE, entry['id'])
518
519 metadata, paged_list = self._extract_playlist(fetch_page, get_metadata, get_entries)
520 return self.playlist_result(paged_list, playlist_id)
521
522
523 class BilibiliSpacePlaylistIE(BilibiliSpaceBaseIE):
524 _VALID_URL = r'https?://space.bilibili\.com/(?P<mid>\d+)/channel/collectiondetail\?sid=(?P<sid>\d+)'
525 _TESTS = [{
526 'url': 'https://space.bilibili.com/2142762/channel/collectiondetail?sid=57445',
527 'info_dict': {
528 'id': '2142762_57445',
529 'title': '《底特律 变人》'
530 },
531 'playlist_mincount': 31,
532 }]
533
534 def _real_extract(self, url):
535 mid, sid = self._match_valid_url(url).group('mid', 'sid')
536 playlist_id = f'{mid}_{sid}'
537
538 def fetch_page(page_idx):
539 return self._download_json(
540 'https://api.bilibili.com/x/polymer/space/seasons_archives_list',
541 playlist_id, note=f'Downloading page {page_idx}',
542 query={'mid': mid, 'season_id': sid, 'page_num': page_idx + 1, 'page_size': 30})['data']
543
544 def get_metadata(page_data):
545 page_size = page_data['page']['page_size']
546 entry_count = page_data['page']['total']
547 return {
548 'page_count': math.ceil(entry_count / page_size),
549 'page_size': page_size,
550 'title': traverse_obj(page_data, ('meta', 'name'))
551 }
552
553 def get_entries(page_data):
554 for entry in page_data.get('archives', []):
555 yield self.url_result(f'https://www.bilibili.com/video/{entry["bvid"]}',
556 BiliBiliIE, entry['bvid'])
557
558 metadata, paged_list = self._extract_playlist(fetch_page, get_metadata, get_entries)
559 return self.playlist_result(paged_list, playlist_id, metadata['title'])
560
561
562 class BilibiliCategoryIE(InfoExtractor):
563 IE_NAME = 'Bilibili category extractor'
564 _MAX_RESULTS = 1000000
565 _VALID_URL = r'https?://www\.bilibili\.com/v/[a-zA-Z]+\/[a-zA-Z]+'
566 _TESTS = [{
567 'url': 'https://www.bilibili.com/v/kichiku/mad',
568 'info_dict': {
569 'id': 'kichiku: mad',
570 'title': 'kichiku: mad'
571 },
572 'playlist_mincount': 45,
573 'params': {
574 'playlistend': 45
575 }
576 }]
577
578 def _fetch_page(self, api_url, num_pages, query, page_num):
579 parsed_json = self._download_json(
580 api_url, query, query={'Search_key': query, 'pn': page_num},
581 note='Extracting results from page %s of %s' % (page_num, num_pages))
582
583 video_list = traverse_obj(parsed_json, ('data', 'archives'), expected_type=list)
584 if not video_list:
585 raise ExtractorError('Failed to retrieve video list for page %d' % page_num)
586
587 for video in video_list:
588 yield self.url_result(
589 'https://www.bilibili.com/video/%s' % video['bvid'], 'BiliBili', video['bvid'])
590
591 def _entries(self, category, subcategory, query):
592 # map of categories : subcategories : RIDs
593 rid_map = {
594 'kichiku': {
595 'mad': 26,
596 'manual_vocaloid': 126,
597 'guide': 22,
598 'theatre': 216,
599 'course': 127
600 },
601 }
602
603 if category not in rid_map:
604 raise ExtractorError(
605 f'The category {category} isn\'t supported. Supported categories: {list(rid_map.keys())}')
606 if subcategory not in rid_map[category]:
607 raise ExtractorError(
608 f'The subcategory {subcategory} isn\'t supported for this category. Supported subcategories: {list(rid_map[category].keys())}')
609 rid_value = rid_map[category][subcategory]
610
611 api_url = 'https://api.bilibili.com/x/web-interface/newlist?rid=%d&type=1&ps=20&jsonp=jsonp' % rid_value
612 page_json = self._download_json(api_url, query, query={'Search_key': query, 'pn': '1'})
613 page_data = traverse_obj(page_json, ('data', 'page'), expected_type=dict)
614 count, size = int_or_none(page_data.get('count')), int_or_none(page_data.get('size'))
615 if count is None or not size:
616 raise ExtractorError('Failed to calculate either page count or size')
617
618 num_pages = math.ceil(count / size)
619
620 return OnDemandPagedList(functools.partial(
621 self._fetch_page, api_url, num_pages, query), size)
622
623 def _real_extract(self, url):
624 category, subcategory = urllib.parse.urlparse(url).path.split('/')[2:4]
625 query = '%s: %s' % (category, subcategory)
626
627 return self.playlist_result(self._entries(category, subcategory, query), query, query)
628
629
630 class BiliBiliSearchIE(SearchInfoExtractor):
631 IE_DESC = 'Bilibili video search'
632 _MAX_RESULTS = 100000
633 _SEARCH_KEY = 'bilisearch'
634
635 def _search_results(self, query):
636 for page_num in itertools.count(1):
637 videos = self._download_json(
638 'https://api.bilibili.com/x/web-interface/search/type', query,
639 note=f'Extracting results from page {page_num}', query={
640 'Search_key': query,
641 'keyword': query,
642 'page': page_num,
643 'context': '',
644 'duration': 0,
645 'tids_2': '',
646 '__refresh__': 'true',
647 'search_type': 'video',
648 'tids': 0,
649 'highlight': 1,
650 })['data'].get('result')
651 if not videos:
652 break
653 for video in videos:
654 yield self.url_result(video['arcurl'], 'BiliBili', str(video['aid']))
655
656
657 class BilibiliAudioBaseIE(InfoExtractor):
658 def _call_api(self, path, sid, query=None):
659 if not query:
660 query = {'sid': sid}
661 return self._download_json(
662 'https://www.bilibili.com/audio/music-service-c/web/' + path,
663 sid, query=query)['data']
664
665
666 class BilibiliAudioIE(BilibiliAudioBaseIE):
667 _VALID_URL = r'https?://(?:www\.)?bilibili\.com/audio/au(?P<id>\d+)'
668 _TEST = {
669 'url': 'https://www.bilibili.com/audio/au1003142',
670 'md5': 'fec4987014ec94ef9e666d4d158ad03b',
671 'info_dict': {
672 'id': '1003142',
673 'ext': 'm4a',
674 'title': '【tsukimi】YELLOW / 神山羊',
675 'artist': 'tsukimi',
676 'comment_count': int,
677 'description': 'YELLOW的mp3版!',
678 'duration': 183,
679 'subtitles': {
680 'origin': [{
681 'ext': 'lrc',
682 }],
683 },
684 'thumbnail': r're:^https?://.+\.jpg',
685 'timestamp': 1564836614,
686 'upload_date': '20190803',
687 'uploader': 'tsukimi-つきみぐー',
688 'view_count': int,
689 },
690 }
691
692 def _real_extract(self, url):
693 au_id = self._match_id(url)
694
695 play_data = self._call_api('url', au_id)
696 formats = [{
697 'url': play_data['cdns'][0],
698 'filesize': int_or_none(play_data.get('size')),
699 'vcodec': 'none'
700 }]
701
702 for a_format in formats:
703 a_format.setdefault('http_headers', {}).update({
704 'Referer': url,
705 })
706
707 song = self._call_api('song/info', au_id)
708 title = song['title']
709 statistic = song.get('statistic') or {}
710
711 subtitles = None
712 lyric = song.get('lyric')
713 if lyric:
714 subtitles = {
715 'origin': [{
716 'url': lyric,
717 }]
718 }
719
720 return {
721 'id': au_id,
722 'title': title,
723 'formats': formats,
724 'artist': song.get('author'),
725 'comment_count': int_or_none(statistic.get('comment')),
726 'description': song.get('intro'),
727 'duration': int_or_none(song.get('duration')),
728 'subtitles': subtitles,
729 'thumbnail': song.get('cover'),
730 'timestamp': int_or_none(song.get('passtime')),
731 'uploader': song.get('uname'),
732 'view_count': int_or_none(statistic.get('play')),
733 }
734
735
736 class BilibiliAudioAlbumIE(BilibiliAudioBaseIE):
737 _VALID_URL = r'https?://(?:www\.)?bilibili\.com/audio/am(?P<id>\d+)'
738 _TEST = {
739 'url': 'https://www.bilibili.com/audio/am10624',
740 'info_dict': {
741 'id': '10624',
742 'title': '每日新曲推荐(每日11:00更新)',
743 'description': '每天11:00更新,为你推送最新音乐',
744 },
745 'playlist_count': 19,
746 }
747
748 def _real_extract(self, url):
749 am_id = self._match_id(url)
750
751 songs = self._call_api(
752 'song/of-menu', am_id, {'sid': am_id, 'pn': 1, 'ps': 100})['data']
753
754 entries = []
755 for song in songs:
756 sid = str_or_none(song.get('id'))
757 if not sid:
758 continue
759 entries.append(self.url_result(
760 'https://www.bilibili.com/audio/au' + sid,
761 BilibiliAudioIE.ie_key(), sid))
762
763 if entries:
764 album_data = self._call_api('menu/info', am_id) or {}
765 album_title = album_data.get('title')
766 if album_title:
767 for entry in entries:
768 entry['album'] = album_title
769 return self.playlist_result(
770 entries, am_id, album_title, album_data.get('intro'))
771
772 return self.playlist_result(entries, am_id)
773
774
775 class BiliBiliPlayerIE(InfoExtractor):
776 _VALID_URL = r'https?://player\.bilibili\.com/player\.html\?.*?\baid=(?P<id>\d+)'
777 _TEST = {
778 'url': 'http://player.bilibili.com/player.html?aid=92494333&cid=157926707&page=1',
779 'only_matching': True,
780 }
781
782 def _real_extract(self, url):
783 video_id = self._match_id(url)
784 return self.url_result(
785 'http://www.bilibili.tv/video/av%s/' % video_id,
786 ie=BiliBiliIE.ie_key(), video_id=video_id)
787
788
789 class BiliIntlBaseIE(InfoExtractor):
790 _API_URL = 'https://api.bilibili.tv/intl/gateway'
791 _NETRC_MACHINE = 'biliintl'
792
793 def _call_api(self, endpoint, *args, **kwargs):
794 json = self._download_json(self._API_URL + endpoint, *args, **kwargs)
795 if json.get('code'):
796 if json['code'] in (10004004, 10004005, 10023006):
797 self.raise_login_required()
798 elif json['code'] == 10004001:
799 self.raise_geo_restricted()
800 else:
801 if json.get('message') and str(json['code']) != json['message']:
802 errmsg = f'{kwargs.get("errnote", "Unable to download JSON metadata")}: {self.IE_NAME} said: {json["message"]}'
803 else:
804 errmsg = kwargs.get('errnote', 'Unable to download JSON metadata')
805 if kwargs.get('fatal'):
806 raise ExtractorError(errmsg)
807 else:
808 self.report_warning(errmsg)
809 return json.get('data')
810
811 def json2srt(self, json):
812 data = '\n\n'.join(
813 f'{i + 1}\n{srt_subtitles_timecode(line["from"])} --> {srt_subtitles_timecode(line["to"])}\n{line["content"]}'
814 for i, line in enumerate(traverse_obj(json, (
815 'body', lambda _, l: l['content'] and l['from'] and l['to']))))
816 return data
817
818 def _get_subtitles(self, *, ep_id=None, aid=None):
819 sub_json = self._call_api(
820 '/web/v2/subtitle', ep_id or aid, fatal=False,
821 note='Downloading subtitles list', errnote='Unable to download subtitles list',
822 query=filter_dict({
823 'platform': 'web',
824 's_locale': 'en_US',
825 'episode_id': ep_id,
826 'aid': aid,
827 })) or {}
828 subtitles = {}
829 for sub in sub_json.get('subtitles') or []:
830 sub_url = sub.get('url')
831 if not sub_url:
832 continue
833 sub_data = self._download_json(
834 sub_url, ep_id or aid, errnote='Unable to download subtitles', fatal=False,
835 note='Downloading subtitles%s' % f' for {sub["lang"]}' if sub.get('lang') else '')
836 if not sub_data:
837 continue
838 subtitles.setdefault(sub.get('lang_key', 'en'), []).append({
839 'ext': 'srt',
840 'data': self.json2srt(sub_data)
841 })
842 return subtitles
843
844 def _get_formats(self, *, ep_id=None, aid=None):
845 video_json = self._call_api(
846 '/web/playurl', ep_id or aid, note='Downloading video formats',
847 errnote='Unable to download video formats', query=filter_dict({
848 'platform': 'web',
849 'ep_id': ep_id,
850 'aid': aid,
851 }))
852 video_json = video_json['playurl']
853 formats = []
854 for vid in video_json.get('video') or []:
855 video_res = vid.get('video_resource') or {}
856 video_info = vid.get('stream_info') or {}
857 if not video_res.get('url'):
858 continue
859 formats.append({
860 'url': video_res['url'],
861 'ext': 'mp4',
862 'format_note': video_info.get('desc_words'),
863 'width': video_res.get('width'),
864 'height': video_res.get('height'),
865 'vbr': video_res.get('bandwidth'),
866 'acodec': 'none',
867 'vcodec': video_res.get('codecs'),
868 'filesize': video_res.get('size'),
869 })
870 for aud in video_json.get('audio_resource') or []:
871 if not aud.get('url'):
872 continue
873 formats.append({
874 'url': aud['url'],
875 'ext': 'mp4',
876 'abr': aud.get('bandwidth'),
877 'acodec': aud.get('codecs'),
878 'vcodec': 'none',
879 'filesize': aud.get('size'),
880 })
881
882 self._sort_formats(formats)
883 return formats
884
885 def _extract_video_info(self, video_data, *, ep_id=None, aid=None):
886 return {
887 'id': ep_id or aid,
888 'title': video_data.get('title_display') or video_data.get('title'),
889 'thumbnail': video_data.get('cover'),
890 'episode_number': int_or_none(self._search_regex(
891 r'^E(\d+)(?:$| - )', video_data.get('title_display') or '', 'episode number', default=None)),
892 'formats': self._get_formats(ep_id=ep_id, aid=aid),
893 'subtitles': self._get_subtitles(ep_id=ep_id, aid=aid),
894 'extractor_key': BiliIntlIE.ie_key(),
895 }
896
897 def _perform_login(self, username, password):
898 try:
899 from Cryptodome.PublicKey import RSA
900 from Cryptodome.Cipher import PKCS1_v1_5
901 except ImportError:
902 try:
903 from Crypto.PublicKey import RSA
904 from Crypto.Cipher import PKCS1_v1_5
905 except ImportError:
906 raise ExtractorError('pycryptodomex not found. Please install', expected=True)
907
908 key_data = self._download_json(
909 'https://passport.bilibili.tv/x/intl/passport-login/web/key?lang=en-US', None,
910 note='Downloading login key', errnote='Unable to download login key')['data']
911
912 public_key = RSA.importKey(key_data['key'])
913 password_hash = PKCS1_v1_5.new(public_key).encrypt((key_data['hash'] + password).encode('utf-8'))
914 login_post = self._download_json(
915 'https://passport.bilibili.tv/x/intl/passport-login/web/login/password?lang=en-US', None, data=urlencode_postdata({
916 'username': username,
917 'password': base64.b64encode(password_hash).decode('ascii'),
918 'keep_me': 'true',
919 's_locale': 'en_US',
920 'isTrusted': 'true'
921 }), note='Logging in', errnote='Unable to log in')
922 if login_post.get('code'):
923 if login_post.get('message'):
924 raise ExtractorError(f'Unable to log in: {self.IE_NAME} said: {login_post["message"]}', expected=True)
925 else:
926 raise ExtractorError('Unable to log in')
927
928
929 class BiliIntlIE(BiliIntlBaseIE):
930 _VALID_URL = r'https?://(?:www\.)?bili(?:bili\.tv|intl\.com)/(?:[a-zA-Z]{2}/)?(play/(?P<season_id>\d+)/(?P<ep_id>\d+)|video/(?P<aid>\d+))'
931 _TESTS = [{
932 # Bstation page
933 'url': 'https://www.bilibili.tv/en/play/34613/341736',
934 'info_dict': {
935 'id': '341736',
936 'ext': 'mp4',
937 'title': 'E2 - The First Night',
938 'thumbnail': r're:^https://pic\.bstarstatic\.com/ogv/.+\.png$',
939 'episode_number': 2,
940 }
941 }, {
942 # Non-Bstation page
943 'url': 'https://www.bilibili.tv/en/play/1033760/11005006',
944 'info_dict': {
945 'id': '11005006',
946 'ext': 'mp4',
947 'title': 'E3 - Who?',
948 'thumbnail': r're:^https://pic\.bstarstatic\.com/ogv/.+\.png$',
949 'episode_number': 3,
950 }
951 }, {
952 # Subtitle with empty content
953 'url': 'https://www.bilibili.tv/en/play/1005144/10131790',
954 'info_dict': {
955 'id': '10131790',
956 'ext': 'mp4',
957 'title': 'E140 - Two Heartbeats: Kabuto\'s Trap',
958 'thumbnail': r're:^https://pic\.bstarstatic\.com/ogv/.+\.png$',
959 'episode_number': 140,
960 },
961 'skip': 'According to the copyright owner\'s request, you may only watch the video after you log in.'
962 }, {
963 'url': 'https://www.biliintl.com/en/play/34613/341736',
964 'only_matching': True,
965 }, {
966 # User-generated content (as opposed to a series licensed from a studio)
967 'url': 'https://bilibili.tv/en/video/2019955076',
968 'only_matching': True,
969 }, {
970 # No language in URL
971 'url': 'https://www.bilibili.tv/video/2019955076',
972 'only_matching': True,
973 }, {
974 # Uppercase language in URL
975 'url': 'https://www.bilibili.tv/EN/video/2019955076',
976 'only_matching': True,
977 }]
978
979 def _real_extract(self, url):
980 season_id, ep_id, aid = self._match_valid_url(url).group('season_id', 'ep_id', 'aid')
981 video_id = ep_id or aid
982 webpage = self._download_webpage(url, video_id)
983 # Bstation layout
984 initial_data = (
985 self._search_json(r'window\.__INITIAL_(?:DATA|STATE)__\s*=', webpage, 'preload state', video_id, default={})
986 or self._search_nuxt_data(webpage, video_id, '__initialState', fatal=False, traverse=None))
987 video_data = traverse_obj(
988 initial_data, ('OgvVideo', 'epDetail'), ('UgcVideo', 'videoData'), ('ugc', 'archive'), expected_type=dict)
989
990 if season_id and not video_data:
991 # Non-Bstation layout, read through episode list
992 season_json = self._call_api(f'/web/v2/ogv/play/episodes?season_id={season_id}&platform=web', video_id)
993 video_data = traverse_obj(season_json,
994 ('sections', ..., 'episodes', lambda _, v: str(v['episode_id']) == ep_id),
995 expected_type=dict, get_all=False)
996 return self._extract_video_info(video_data or {}, ep_id=ep_id, aid=aid)
997
998
999 class BiliIntlSeriesIE(BiliIntlBaseIE):
1000 _VALID_URL = r'https?://(?:www\.)?bili(?:bili\.tv|intl\.com)/(?:[a-zA-Z]{2}/)?play/(?P<id>\d+)/?(?:[?#]|$)'
1001 _TESTS = [{
1002 'url': 'https://www.bilibili.tv/en/play/34613',
1003 'playlist_mincount': 15,
1004 'info_dict': {
1005 'id': '34613',
1006 'title': 'Fly Me to the Moon',
1007 'description': 'md5:a861ee1c4dc0acfad85f557cc42ac627',
1008 'categories': ['Romance', 'Comedy', 'Slice of life'],
1009 'thumbnail': r're:^https://pic\.bstarstatic\.com/ogv/.+\.png$',
1010 'view_count': int,
1011 },
1012 'params': {
1013 'skip_download': True,
1014 },
1015 }, {
1016 'url': 'https://www.biliintl.com/en/play/34613',
1017 'only_matching': True,
1018 }, {
1019 'url': 'https://www.biliintl.com/EN/play/34613',
1020 'only_matching': True,
1021 }]
1022
1023 def _entries(self, series_id):
1024 series_json = self._call_api(f'/web/v2/ogv/play/episodes?season_id={series_id}&platform=web', series_id)
1025 for episode in traverse_obj(series_json, ('sections', ..., 'episodes', ...), expected_type=dict, default=[]):
1026 episode_id = str(episode.get('episode_id'))
1027 yield self._extract_video_info(episode, ep_id=episode_id)
1028
1029 def _real_extract(self, url):
1030 series_id = self._match_id(url)
1031 series_info = self._call_api(f'/web/v2/ogv/play/season_info?season_id={series_id}&platform=web', series_id).get('season') or {}
1032 return self.playlist_result(
1033 self._entries(series_id), series_id, series_info.get('title'), series_info.get('description'),
1034 categories=traverse_obj(series_info, ('styles', ..., 'title'), expected_type=str_or_none),
1035 thumbnail=url_or_none(series_info.get('horizontal_cover')), view_count=parse_count(series_info.get('view')))
1036
1037
1038 class BiliLiveIE(InfoExtractor):
1039 _VALID_URL = r'https?://live.bilibili.com/(?P<id>\d+)'
1040
1041 _TESTS = [{
1042 'url': 'https://live.bilibili.com/196',
1043 'info_dict': {
1044 'id': '33989',
1045 'description': "周六杂谈回,其他时候随机游戏。 | \n录播:@下播型泛式录播组。 | \n直播通知群(全员禁言):666906670,902092584,59971⑧481 (功能一样,别多加)",
1046 'ext': 'flv',
1047 'title': "太空狼人杀联动,不被爆杀就算赢",
1048 'thumbnail': "https://i0.hdslb.com/bfs/live/new_room_cover/e607bc1529057ef4b332e1026e62cf46984c314d.jpg",
1049 'timestamp': 1650802769,
1050 },
1051 'skip': 'not live'
1052 }, {
1053 'url': 'https://live.bilibili.com/196?broadcast_type=0&is_room_feed=1?spm_id_from=333.999.space_home.strengthen_live_card.click',
1054 'only_matching': True
1055 }]
1056
1057 _FORMATS = {
1058 80: {'format_id': 'low', 'format_note': '流畅'},
1059 150: {'format_id': 'high_res', 'format_note': '高清'},
1060 250: {'format_id': 'ultra_high_res', 'format_note': '超清'},
1061 400: {'format_id': 'blue_ray', 'format_note': '蓝光'},
1062 10000: {'format_id': 'source', 'format_note': '原画'},
1063 20000: {'format_id': '4K', 'format_note': '4K'},
1064 30000: {'format_id': 'dolby', 'format_note': '杜比'},
1065 }
1066
1067 _quality = staticmethod(qualities(list(_FORMATS)))
1068
1069 def _call_api(self, path, room_id, query):
1070 api_result = self._download_json(f'https://api.live.bilibili.com/{path}', room_id, query=query)
1071 if api_result.get('code') != 0:
1072 raise ExtractorError(api_result.get('message') or 'Unable to download JSON metadata')
1073 return api_result.get('data') or {}
1074
1075 def _parse_formats(self, qn, fmt):
1076 for codec in fmt.get('codec') or []:
1077 if codec.get('current_qn') != qn:
1078 continue
1079 for url_info in codec['url_info']:
1080 yield {
1081 'url': f'{url_info["host"]}{codec["base_url"]}{url_info["extra"]}',
1082 'ext': fmt.get('format_name'),
1083 'vcodec': codec.get('codec_name'),
1084 'quality': self._quality(qn),
1085 **self._FORMATS[qn],
1086 }
1087
1088 def _real_extract(self, url):
1089 room_id = self._match_id(url)
1090 room_data = self._call_api('room/v1/Room/get_info', room_id, {'id': room_id})
1091 if room_data.get('live_status') == 0:
1092 raise ExtractorError('Streamer is not live', expected=True)
1093
1094 formats = []
1095 for qn in self._FORMATS.keys():
1096 stream_data = self._call_api('xlive/web-room/v2/index/getRoomPlayInfo', room_id, {
1097 'room_id': room_id,
1098 'qn': qn,
1099 'codec': '0,1',
1100 'format': '0,2',
1101 'mask': '0',
1102 'no_playurl': '0',
1103 'platform': 'web',
1104 'protocol': '0,1',
1105 })
1106 for fmt in traverse_obj(stream_data, ('playurl_info', 'playurl', 'stream', ..., 'format', ...)) or []:
1107 formats.extend(self._parse_formats(qn, fmt))
1108 self._sort_formats(formats)
1109
1110 return {
1111 'id': room_id,
1112 'title': room_data.get('title'),
1113 'description': room_data.get('description'),
1114 'thumbnail': room_data.get('user_cover'),
1115 'timestamp': stream_data.get('live_time'),
1116 'formats': formats,
1117 'http_headers': {
1118 'Referer': url,
1119 },
1120 }