2 from __future__ import unicode_literals
7 from .common import InfoExtractor
8 from ..compat import compat_parse_qs
16 class BiliBiliIE(InfoExtractor):
17 _VALID_URL = r'https?://www\.bilibili\.(?:tv|com)/video/av(?P<id>\d+)'
20 'url': 'http://www.bilibili.tv/video/av1074402/',
21 'md5': '9fa226fe2b8a9a4d5a69b4c6a183417e',
26 'description': 'md5:ce18c2a2d2193f0df2917d270f2e5923',
28 'timestamp': 1398012660,
29 'upload_date': '20140420',
30 'thumbnail': 're:^https?://.+\.jpg',
32 'uploader_id': '156160',
35 'url': 'http://www.bilibili.com/video/av1041170/',
39 'title': '【BD1080P】刀语【诸神&异域】',
40 'description': '这是个神奇的故事~每个人不留弹幕不给走哦~切利哦!~',
42 'timestamp': 1396530060,
43 'upload_date': '20140403',
44 'thumbnail': 're:^https?://.+\.jpg',
46 'uploader_id': '520116',
49 'url': 'http://www.bilibili.com/video/av4808130/',
53 'title': '【长篇】哆啦A梦443【钉铛】',
54 'description': '(2016.05.27)来组合客人的脸吧&amp;寻母六千里锭 抱歉,又轮到周日上班现在才到家 封面www.pixiv.net/member_illust.php?mode=medium&amp;illust_id=56912929',
56 'timestamp': 1464564180,
57 'upload_date': '20160529',
58 'thumbnail': 're:^https?://.+\.jpg',
60 'uploader_id': '151066',
64 'url': 'http://www.bilibili.com/video/av1867637/',
68 'title': '【HDTV】【喜剧】岳父岳母真难当 (2014)【法国票房冠军】',
69 'description': '一个信奉天主教的法国旧式传统资产阶级家庭中有四个女儿。三个女儿却分别找了阿拉伯、犹太、中国丈夫,老夫老妻唯独期盼剩下未嫁的小女儿能找一个信奉天主教的法国白人,结果没想到小女儿找了一位非裔黑人……【这次应该不会跳帧了】',
72 'uploader_id': '610729',
73 'thumbnail': 're:^https?://.+\.jpg',
76 # Just to test metadata extraction
77 'skip_download': True,
79 'expected_warnings': ['upload time'],
82 _APP_KEY = '6f90a59ac58a4123'
83 _BILIBILI_KEY = '0bfd84cc3940035173f35e6777508326'
85 def _real_extract(self, url):
86 video_id = self._match_id(url)
88 webpage = self._download_webpage(url, video_id)
90 cid = compat_parse_qs(self._search_regex(
91 [r'EmbedPlayer\([^)]+,\s*"([^"]+)"\)',
92 r'<iframe[^>]+src="https://secure\.bilibili\.com/secure,([^"]+)"'],
93 webpage, 'player parameters'))['cid'][0]
95 payload = 'appkey=%s&cid=%s&otype=json&quality=2&type=mp4' % (self._APP_KEY, cid)
96 sign = hashlib.md5((payload + self._BILIBILI_KEY).encode('utf-8')).hexdigest()
98 video_info = self._download_json(
99 'http://interface.bilibili.com/playurl?%s&sign=%s' % (payload, sign),
100 video_id, note='Downloading video info page')
104 for idx, durl in enumerate(video_info['durl']):
107 'filesize': int_or_none(durl['size']),
109 for backup_url in durl['backup_url']:
112 # backup URLs have lower priorities
113 'preference': -2 if 'hd.mp4' in backup_url else -3,
116 self._sort_formats(formats)
119 'id': '%s_part%s' % (video_id, idx),
120 'duration': float_or_none(durl.get('length'), 1000),
124 title = self._html_search_regex('<h1[^>]+title="([^"]+)">', webpage, 'title')
125 description = self._html_search_meta('description', webpage)
126 timestamp = unified_timestamp(self._html_search_regex(
127 r'<time[^>]+datetime="([^"]+)"', webpage, 'upload time', fatal=False))
129 # TODO 'view_count' requires deobfuscating Javascript
133 'description': description,
134 'timestamp': timestamp,
135 'thumbnail': self._html_search_meta('thumbnailUrl', webpage),
136 'duration': float_or_none(video_info.get('timelength'), scale=1000),
139 uploader_mobj = re.search(
140 r'<a[^>]+href="https?://space\.bilibili\.com/(?P<id>\d+)"[^>]+title="(?P<name>[^"]+)"',
144 'uploader': uploader_mobj.group('name'),
145 'uploader_id': uploader_mobj.group('id'),
148 for entry in entries:
151 if len(entries) == 1:
154 for idx, entry in enumerate(entries):
155 entry['id'] = '%s_part%d' % (video_id, (idx + 1))
158 '_type': 'multi_video',
161 'description': description,