2 from __future__ import unicode_literals
7 from .common import InfoExtractor
26 class BiliBiliIE(InfoExtractor):
29 (?:(?:www|bangumi)\.)?
34 anime/(?P<anime_id>\d+)/play\#
36 video/[bB][vV](?P<id>[^/?#&]+)
41 'url': 'http://www.bilibili.tv/video/av1074402/',
42 'md5': '5f7d29e1a2872f3df0cf76b1f87d3788',
47 'description': 'md5:ce18c2a2d2193f0df2917d270f2e5923',
49 'timestamp': 1398012678,
50 'upload_date': '20140420',
51 'thumbnail': r're:^https?://.+\.jpg',
53 'uploader_id': '156160',
56 # Tested in BiliBiliBangumiIE
57 'url': 'http://bangumi.bilibili.com/anime/1869/play#40062',
58 'only_matching': True,
60 'url': 'http://bangumi.bilibili.com/anime/5802/play#100643',
61 'md5': '3f721ad1e75030cc06faf73587cfec57',
65 'title': 'CHAOS;CHILD',
66 'description': '如果你是神明,并且能够让妄想成为现实。那你会进行怎么样的妄想?是淫靡的世界?独裁社会?毁灭性的制裁?还是……2015年,涩谷。从6年前发生的大灾害“涩谷地震”之后复兴了的这个街区里新设立的私立高中...',
68 'skip': 'Geo-restricted to China',
70 # Title with double quotes
71 'url': 'http://www.bilibili.com/video/av8903802/',
74 'title': '阿滴英文|英文歌分享#6 "Closer',
75 'description': '滴妹今天唱Closer給你聽! 有史以来,被推最多次也是最久的歌曲,其实歌词跟我原本想像差蛮多的,不过还是好听! 微博@阿滴英文',
79 'id': '8903802_part1',
81 'title': '阿滴英文|英文歌分享#6 "Closer',
82 'description': 'md5:3b1b9e25b78da4ef87e9b548b88ee76a',
84 'uploader_id': '65880958',
85 'timestamp': 1488382634,
86 'upload_date': '20170301',
89 'skip_download': True, # Test metadata only
93 'id': '8903802_part2',
95 'title': '阿滴英文|英文歌分享#6 "Closer',
96 'description': 'md5:3b1b9e25b78da4ef87e9b548b88ee76a',
98 'uploader_id': '65880958',
99 'timestamp': 1488382634,
100 'upload_date': '20170301',
103 'skip_download': True, # Test metadata only
107 # new BV video id format
108 'url': 'https://www.bilibili.com/video/BV1JE411F741',
109 'only_matching': True,
112 _APP_KEY = 'iVGUTjsxvpLeuDCf'
113 _BILIBILI_KEY = 'aHRmhWMLkdeMuILqORnYZocwMBpMEOdt'
115 def _report_error(self, result):
116 if 'message' in result:
117 raise ExtractorError('%s said: %s' % (self.IE_NAME, result['message']), expected=True)
118 elif 'code' in result:
119 raise ExtractorError('%s returns error %d' % (self.IE_NAME, result['code']), expected=True)
121 raise ExtractorError('Can\'t extract Bangumi episode ID')
123 def _real_extract(self, url):
124 url, smuggled_data = unsmuggle_url(url, {})
126 mobj = re.match(self._VALID_URL, url)
127 video_id = mobj.group('id') or mobj.group('id_bv')
128 anime_id = mobj.group('anime_id')
129 webpage = self._download_webpage(url, video_id)
131 if 'anime/' not in url:
132 cid = self._search_regex(
133 r'\bcid(?:["\']:|=)(\d+)', webpage, 'cid',
135 ) or compat_parse_qs(self._search_regex(
136 [r'EmbedPlayer\([^)]+,\s*"([^"]+)"\)',
137 r'EmbedPlayer\([^)]+,\s*\\"([^"]+)\\"\)',
138 r'<iframe[^>]+src="https://secure\.bilibili\.com/secure,([^"]+)"'],
139 webpage, 'player parameters'))['cid'][0]
141 if 'no_bangumi_tip' not in smuggled_data:
142 self.to_screen('Downloading episode %s. To download all videos in anime %s, re-run youtube-dl with %s' % (
143 video_id, anime_id, compat_urlparse.urljoin(url, '//bangumi.bilibili.com/anime/%s' % anime_id)))
145 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
148 headers.update(self.geo_verification_headers())
150 js = self._download_json(
151 'http://bangumi.bilibili.com/web_api/get_source', video_id,
152 data=urlencode_postdata({'episode_id': video_id}),
154 if 'result' not in js:
155 self._report_error(js)
156 cid = js['result']['cid']
159 'Accept': 'application/json',
162 headers.update(self.geo_verification_headers())
166 RENDITIONS = ('qn=80&quality=80&type=', 'quality=2&type=mp4')
167 for num, rendition in enumerate(RENDITIONS, start=1):
168 payload = 'appkey=%s&cid=%s&otype=json&%s' % (self._APP_KEY, cid, rendition)
169 sign = hashlib.md5((payload + self._BILIBILI_KEY).encode('utf-8')).hexdigest()
171 video_info = self._download_json(
172 'http://interface.bilibili.com/v2/playurl?%s&sign=%s' % (payload, sign),
173 video_id, note='Downloading video info page',
174 headers=headers, fatal=num == len(RENDITIONS))
179 if 'durl' not in video_info:
180 if num < len(RENDITIONS):
182 self._report_error(video_info)
184 for idx, durl in enumerate(video_info['durl']):
187 'filesize': int_or_none(durl['size']),
189 for backup_url in durl.get('backup_url', []):
192 # backup URLs have lower priorities
193 'preference': -2 if 'hd.mp4' in backup_url else -3,
196 for a_format in formats:
197 a_format.setdefault('http_headers', {}).update({
201 self._sort_formats(formats)
204 'id': '%s_part%s' % (video_id, idx),
205 'duration': float_or_none(durl.get('length'), 1000),
210 title = self._html_search_regex(
211 ('<h1[^>]+\btitle=(["\'])(?P<title>(?:(?!\1).)+)\1',
212 '(?s)<h1[^>]*>(?P<title>.+?)</h1>'), webpage, 'title',
214 description = self._html_search_meta('description', webpage)
215 timestamp = unified_timestamp(self._html_search_regex(
216 r'<time[^>]+datetime="([^"]+)"', webpage, 'upload time',
217 default=None) or self._html_search_meta(
218 'uploadDate', webpage, 'timestamp', default=None))
219 thumbnail = self._html_search_meta(['og:image', 'thumbnailUrl'], webpage)
221 # TODO 'view_count' requires deobfuscating Javascript
225 'description': description,
226 'timestamp': timestamp,
227 'thumbnail': thumbnail,
228 'duration': float_or_none(video_info.get('timelength'), scale=1000),
231 uploader_mobj = re.search(
232 r'<a[^>]+href="(?:https?:)?//space\.bilibili\.com/(?P<id>\d+)"[^>]*>(?P<name>[^<]+)',
236 'uploader': uploader_mobj.group('name'),
237 'uploader_id': uploader_mobj.group('id'),
239 if not info.get('uploader'):
240 info['uploader'] = self._html_search_meta(
241 'author', webpage, 'uploader', default=None)
243 for entry in entries:
246 if len(entries) == 1:
249 for idx, entry in enumerate(entries):
250 entry['id'] = '%s_part%d' % (video_id, (idx + 1))
253 '_type': 'multi_video',
256 'description': description,
261 class BiliBiliBangumiIE(InfoExtractor):
262 _VALID_URL = r'https?://bangumi\.bilibili\.com/anime/(?P<id>\d+)'
264 IE_NAME = 'bangumi.bilibili.com'
265 IE_DESC = 'BiliBili番剧'
268 'url': 'http://bangumi.bilibili.com/anime/1869',
272 'description': 'md5:6a9622b911565794c11f25f81d6a97d2',
274 'playlist_count': 26,
276 'url': 'http://bangumi.bilibili.com/anime/1869',
280 'description': 'md5:6a9622b911565794c11f25f81d6a97d2',
283 'md5': '91da8621454dd58316851c27c68b0c13',
288 'description': '故事发生在日本的江户时代。风是一个小酒馆的打工女。一日,酒馆里来了一群恶霸,虽然他们的举动令风十分不满,但是毕竟风只是一届女流,无法对他们采取什么行动,只能在心里嘟哝。这时,酒家里又进来了个“不良份子...',
289 'timestamp': 1414538739,
290 'upload_date': '20141028',
291 'episode': '疾风怒涛 Tempestuous Temperaments',
296 'playlist_items': '1',
301 def suitable(cls, url):
302 return False if BiliBiliIE.suitable(url) else super(BiliBiliBangumiIE, cls).suitable(url)
304 def _real_extract(self, url):
305 bangumi_id = self._match_id(url)
307 # Sometimes this API returns a JSONP response
308 season_info = self._download_json(
309 'http://bangumi.bilibili.com/jsonp/seasoninfo/%s.ver' % bangumi_id,
310 bangumi_id, transform_source=strip_jsonp)['result']
313 '_type': 'url_transparent',
314 'url': smuggle_url(episode['webplay_url'], {'no_bangumi_tip': 1}),
315 'ie_key': BiliBiliIE.ie_key(),
316 'timestamp': parse_iso8601(episode.get('update_time'), delimiter=' '),
317 'episode': episode.get('index_title'),
318 'episode_number': int_or_none(episode.get('index')),
319 } for episode in season_info['episodes']]
321 entries = sorted(entries, key=lambda entry: entry.get('episode_number'))
323 return self.playlist_result(
325 season_info.get('bangumi_title'), season_info.get('evaluate'))
328 class BilibiliAudioBaseIE(InfoExtractor):
329 def _call_api(self, path, sid, query=None):
332 return self._download_json(
333 'https://www.bilibili.com/audio/music-service-c/web/' + path,
334 sid, query=query)['data']
337 class BilibiliAudioIE(BilibiliAudioBaseIE):
338 _VALID_URL = r'https?://(?:www\.)?bilibili\.com/audio/au(?P<id>\d+)'
340 'url': 'https://www.bilibili.com/audio/au1003142',
341 'md5': 'fec4987014ec94ef9e666d4d158ad03b',
345 'title': '【tsukimi】YELLOW / 神山羊',
347 'comment_count': int,
348 'description': 'YELLOW的mp3版!',
355 'thumbnail': r're:^https?://.+\.jpg',
356 'timestamp': 1564836614,
357 'upload_date': '20190803',
358 'uploader': 'tsukimi-つきみぐー',
363 def _real_extract(self, url):
364 au_id = self._match_id(url)
366 play_data = self._call_api('url', au_id)
368 'url': play_data['cdns'][0],
369 'filesize': int_or_none(play_data.get('size')),
372 song = self._call_api('song/info', au_id)
373 title = song['title']
374 statistic = song.get('statistic') or {}
377 lyric = song.get('lyric')
389 'artist': song.get('author'),
390 'comment_count': int_or_none(statistic.get('comment')),
391 'description': song.get('intro'),
392 'duration': int_or_none(song.get('duration')),
393 'subtitles': subtitles,
394 'thumbnail': song.get('cover'),
395 'timestamp': int_or_none(song.get('passtime')),
396 'uploader': song.get('uname'),
397 'view_count': int_or_none(statistic.get('play')),
401 class BilibiliAudioAlbumIE(BilibiliAudioBaseIE):
402 _VALID_URL = r'https?://(?:www\.)?bilibili\.com/audio/am(?P<id>\d+)'
404 'url': 'https://www.bilibili.com/audio/am10624',
407 'title': '每日新曲推荐(每日11:00更新)',
408 'description': '每天11:00更新,为你推送最新音乐',
410 'playlist_count': 19,
413 def _real_extract(self, url):
414 am_id = self._match_id(url)
416 songs = self._call_api(
417 'song/of-menu', am_id, {'sid': am_id, 'pn': 1, 'ps': 100})['data']
421 sid = str_or_none(song.get('id'))
424 entries.append(self.url_result(
425 'https://www.bilibili.com/audio/au' + sid,
426 BilibiliAudioIE.ie_key(), sid))
429 album_data = self._call_api('menu/info', am_id) or {}
430 album_title = album_data.get('title')
432 for entry in entries:
433 entry['album'] = album_title
434 return self.playlist_result(
435 entries, am_id, album_title, album_data.get('intro'))
437 return self.playlist_result(entries, am_id)
440 class BiliBiliPlayerIE(InfoExtractor):
441 _VALID_URL = r'https?://player\.bilibili\.com/player\.html\?.*?\baid=(?P<id>\d+)'
443 'url': 'http://player.bilibili.com/player.html?aid=92494333&cid=157926707&page=1',
444 'only_matching': True,
447 def _real_extract(self, url):
448 video_id = self._match_id(url)
449 return self.url_result(
450 'http://www.bilibili.tv/video/av%s/' % video_id,
451 ie=BiliBiliIE.ie_key(), video_id=video_id)