2 from __future__ import unicode_literals
7 from .common import InfoExtractor
9 compat_etree_fromstring,
18 class BiliBiliIE(InfoExtractor):
19 _VALID_URL = r'http://www\.bilibili\.(?:tv|com)/video/av(?P<id>\d+)(?:/index_(?P<page_num>\d+).html)?'
22 'url': 'http://www.bilibili.tv/video/av1074402/',
23 'md5': '2c301e4dab317596e837c3e7633e7d86',
29 'upload_date': '20140420',
30 'thumbnail': 're:^https?://.+\.jpg',
31 'description': 'md5:ce18c2a2d2193f0df2917d270f2e5923',
32 'timestamp': 1397983878,
36 'url': 'http://www.bilibili.com/video/av1041170/',
39 'title': '【BD1080P】刀语【诸神&异域】',
40 'description': '这是个神奇的故事~每个人不留弹幕不给走哦~切利哦!~',
42 'timestamp': 1396501299,
47 def _real_extract(self, url):
48 mobj = re.match(self._VALID_URL, url)
49 video_id = mobj.group('id')
50 page_num = mobj.group('page_num') or '1'
52 view_data = self._download_json(
53 'http://api.bilibili.com/view?type=json&appkey=8e9fc618fbd41e28&id=%s&page=%s' % (video_id, page_num),
55 if 'error' in view_data:
56 raise ExtractorError('%s said: %s' % (self.IE_NAME, view_data['error']), expected=True)
58 cid = view_data['cid']
59 title = unescapeHTML(view_data['title'])
61 page = self._download_webpage(
62 'http://interface.bilibili.com/v_cdn_play?appkey=8e9fc618fbd41e28&cid=%s' % cid,
64 'Downloading page %s/%s' % (page_num, view_data['pages'])
67 err_info = json.loads(page)
69 'BiliBili said: ' + err_info['error_text'], expected=True)
73 doc = compat_etree_fromstring(page)
77 for durl in doc.findall('./durl'):
78 size = durl.find('./filesize|./size')
80 'url': durl.find('./url').text,
81 'filesize': int_or_none(size.text) if size else None,
84 backup_urls = durl.find('./backup_url')
85 if backup_urls is not None:
86 for backup_url in backup_urls.findall('./url'):
87 formats.append({'url': backup_url.text})
91 'id': '%s_part%s' % (cid, durl.find('./order').text),
93 'duration': int_or_none(durl.find('./length').text) // 1000,
100 'description': view_data.get('description'),
101 'thumbnail': view_data.get('pic'),
102 'uploader': view_data.get('author'),
103 'timestamp': int_or_none(view_data.get('created')),
104 'view_count': view_data.get('play'),
105 'duration': int_or_none(doc.find('./timelength').text),
108 if len(entries) == 1:
109 entries[0].update(info)
113 '_type': 'multi_video',