2 from __future__ import unicode_literals
6 import xml.etree.ElementTree as ET
8 from .common import InfoExtractor
16 class BiliBiliIE(InfoExtractor):
17 _VALID_URL = r'http://www\.bilibili\.(?:tv|com)/video/av(?P<id>\d+)(?:/index_(?P<page_num>\d+).html)?'
20 'url': 'http://www.bilibili.tv/video/av1074402/',
21 'md5': '2c301e4dab317596e837c3e7633e7d86',
27 'upload_date': '20140420',
28 'thumbnail': 're:^https?://.+\.jpg',
29 'description': 'md5:ce18c2a2d2193f0df2917d270f2e5923',
30 'timestamp': 1397983878,
34 'url': 'http://www.bilibili.com/video/av1041170/',
37 'title': '【BD1080P】刀语【诸神&异域】',
38 'description': '这是个神奇的故事~每个人不留弹幕不给走哦~切利哦!~',
40 'timestamp': 1396501299,
45 def _real_extract(self, url):
46 mobj = re.match(self._VALID_URL, url)
47 video_id = mobj.group('id')
48 page_num = mobj.group('page_num') or '1'
50 view_data = self._download_json(
51 'http://api.bilibili.com/view?type=json&appkey=8e9fc618fbd41e28&id=%s&page=%s' % (video_id, page_num),
53 if 'error' in view_data:
54 raise ExtractorError('%s said: %s' % (self.IE_NAME, view_data['error']), expected=True)
56 cid = view_data['cid']
57 title = unescapeHTML(view_data['title'])
59 page = self._download_webpage(
60 'http://interface.bilibili.com/v_cdn_play?appkey=8e9fc618fbd41e28&cid=%s' % cid,
62 'Downloading page %s/%s' % (page_num, view_data['pages'])
65 err_info = json.loads(page)
67 'BiliBili said: ' + err_info['error_text'], expected=True)
71 doc = ET.fromstring(page)
72 durls = doc.findall('./durl')
78 backup_url = durl.find('./backup_url')
79 if backup_url is not None:
80 formats.append({'url': backup_url.find('./url').text})
81 size = durl.find('./filesize|./size')
83 'url': durl.find('./url').text,
84 'filesize': int_or_none(size.text) if size else None,
88 'id': '%s_part%s' % (cid, durl.find('./order').text),
90 'duration': int_or_none(durl.find('./length').text) // 1000,
97 'description': view_data.get('description'),
98 'thumbnail': view_data.get('pic'),
99 'uploader': view_data.get('author'),
100 'timestamp': int_or_none(view_data.get('created')),
101 'view_count': view_data.get('play'),
102 'duration': int_or_none(doc.find('./timelength').text),
105 if len(entries) == 1:
106 entries[0].update(info)
110 '_type': 'multi_video',