[bilibili] Fix extraction (closes #10375)
[youtube-dl] / youtube_dl / extractor / bilibili.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import hashlib
5 import re
6
7 from .common import InfoExtractor
8 from ..compat import compat_parse_qs
9 from ..utils import (
10     int_or_none,
11     float_or_none,
12     unified_timestamp,
13 )
14
15
16 class BiliBiliIE(InfoExtractor):
17     _VALID_URL = r'https?://www\.bilibili\.(?:tv|com)/video/av(?P<id>\d+)'
18
19     _TESTS = [{
20         'url': 'http://www.bilibili.tv/video/av1074402/',
21         'md5': '9fa226fe2b8a9a4d5a69b4c6a183417e',
22         'info_dict': {
23             'id': '1074402',
24             'ext': 'mp4',
25             'title': '【金坷垃】金泡沫',
26             'description': 'md5:ce18c2a2d2193f0df2917d270f2e5923',
27             'duration': 308.315,
28             'timestamp': 1398012660,
29             'upload_date': '20140420',
30             'thumbnail': 're:^https?://.+\.jpg',
31             'uploader': '菊子桑',
32             'uploader_id': '156160',
33         },
34     }, {
35         'url': 'http://www.bilibili.com/video/av1041170/',
36         'info_dict': {
37             'id': '1041170',
38             'ext': 'mp4',
39             'title': '【BD1080P】刀语【诸神&异域】',
40             'description': '这是个神奇的故事~每个人不留弹幕不给走哦~切利哦!~',
41             'duration': 3382.259,
42             'timestamp': 1396530060,
43             'upload_date': '20140403',
44             'thumbnail': 're:^https?://.+\.jpg',
45             'uploader': '枫叶逝去',
46             'uploader_id': '520116',
47         },
48     }, {
49         'url': 'http://www.bilibili.com/video/av4808130/',
50         'info_dict': {
51             'id': '4808130',
52             'ext': 'mp4',
53             'title': '【长篇】哆啦A梦443【钉铛】',
54             'description': '(2016.05.27)来组合客人的脸吧&amp;amp;寻母六千里锭 抱歉,又轮到周日上班现在才到家 封面www.pixiv.net/member_illust.php?mode=medium&amp;amp;illust_id=56912929',
55             'duration': 1493.995,
56             'timestamp': 1464564180,
57             'upload_date': '20160529',
58             'thumbnail': 're:^https?://.+\.jpg',
59             'uploader': '喜欢拉面',
60             'uploader_id': '151066',
61         },
62     }, {
63         # Missing upload time
64         'url': 'http://www.bilibili.com/video/av1867637/',
65         'info_dict': {
66             'id': '1867637',
67             'ext': 'mp4',
68             'title': '【HDTV】【喜剧】岳父岳母真难当 (2014)【法国票房冠军】',
69             'description': '一个信奉天主教的法国旧式传统资产阶级家庭中有四个女儿。三个女儿却分别找了阿拉伯、犹太、中国丈夫,老夫老妻唯独期盼剩下未嫁的小女儿能找一个信奉天主教的法国白人,结果没想到小女儿找了一位非裔黑人……【这次应该不会跳帧了】',
70             'duration': 5760.0,
71             'uploader': '黑夜为猫',
72             'uploader_id': '610729',
73             'thumbnail': 're:^https?://.+\.jpg',
74         },
75         'params': {
76             # Just to test metadata extraction
77             'skip_download': True,
78         },
79         'expected_warnings': ['upload time'],
80     }]
81
82     _APP_KEY = '6f90a59ac58a4123'
83     _BILIBILI_KEY = '0bfd84cc3940035173f35e6777508326'
84
85     def _real_extract(self, url):
86         video_id = self._match_id(url)
87
88         webpage = self._download_webpage(url, video_id)
89
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]
94
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()
97
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')
101
102         entries = []
103
104         for idx, durl in enumerate(video_info['durl']):
105             formats = [{
106                 'url': durl['url'],
107                 'filesize': int_or_none(durl['size']),
108             }]
109             for backup_url in durl['backup_url']:
110                 formats.append({
111                     'url': backup_url,
112                     # backup URLs have lower priorities
113                     'preference': -2 if 'hd.mp4' in backup_url else -3,
114                 })
115
116             self._sort_formats(formats)
117
118             entries.append({
119                 'id': '%s_part%s' % (video_id, idx),
120                 'duration': float_or_none(durl.get('length'), 1000),
121                 'formats': formats,
122             })
123
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))
128
129         # TODO 'view_count' requires deobfuscating Javascript
130         info = {
131             'id': video_id,
132             'title': title,
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),
137         }
138
139         uploader_mobj = re.search(
140             r'<a[^>]+href="https?://space\.bilibili\.com/(?P<id>\d+)"[^>]+title="(?P<name>[^"]+)"',
141             webpage)
142         if uploader_mobj:
143             info.update({
144                 'uploader': uploader_mobj.group('name'),
145                 'uploader_id': uploader_mobj.group('id'),
146             })
147
148         for entry in entries:
149             entry.update(info)
150
151         if len(entries) == 1:
152             return entries[0]
153         else:
154             for idx, entry in enumerate(entries):
155                 entry['id'] = '%s_part%d' % (video_id, (idx + 1))
156
157             return {
158                 '_type': 'multi_video',
159                 'id': video_id,
160                 'title': title,
161                 'description': description,
162                 'entries': entries,
163             }