[bilibili] fix info extraction
[youtube-dl] / youtube_dl / extractor / bilibili.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5 import xml.etree.ElementTree as ET
6
7 from .common import InfoExtractor
8 from ..utils import (
9     int_or_none,
10     ExtractorError,
11 )
12
13
14 class BiliBiliIE(InfoExtractor):
15     _VALID_URL = r'http://www\.bilibili\.(?:tv|com)/video/av(?P<id>[0-9]+)/'
16
17     _TESTS = [{
18         'url': 'http://www.bilibili.tv/video/av1074402/',
19         'md5': '2c301e4dab317596e837c3e7633e7d86',
20         'info_dict': {
21             'id': '1554319',
22             'ext': 'flv',
23             'title': '【金坷垃】金泡沫',
24             'duration': 308313,
25             'upload_date': '20140420',
26             'thumbnail': 're:^https?://.+\.jpg',
27             'description': 'md5:ce18c2a2d2193f0df2917d270f2e5923',
28             'timestamp': 1397983878,
29             'uploader': '菊子桑',
30         },
31     }, {
32         'url': 'http://www.bilibili.com/video/av1041170/',
33         'info_dict': {
34             'id': '1041170',
35             'title': '【BD1080P】刀语【诸神&异域】',
36         },
37         'playlist_count': 12,
38     }]
39
40     def _extract_video_info(self, cid, view_data, page_num=1, num_pages=1):
41         title = view_data['title']
42
43         page = self._download_webpage(
44             'http://interface.bilibili.com/v_cdn_play?appkey=8e9fc618fbd41e28&cid=%s' % cid,
45             cid,
46             'Downloading page %d/%d' % (page_num, num_pages)
47         )
48         try:
49             err_info = json.loads(page)
50             raise ExtractorError(
51                 'BiliBili said: ' + err_info['error_text'], expected=True)
52         except ValueError:
53             pass
54
55         doc = ET.fromstring(page)
56         durls = doc.findall('./durl')
57
58         entries = []
59
60         for durl in durls:
61             entries.append({
62                 'id': '%s_part%s' % (cid, durl.find('./order').text),
63                 'title': title,
64                 'url': durl.find('./url').text,
65                 'filesize': int_or_none(durl.find('./filesize').text),
66                 'ext': 'flv',
67                 'duration': int_or_none(durl.find('./length').text) // 1000,
68             })
69
70         info = {
71             'id': cid,
72             'title': title,
73             'description': view_data.get('description'),
74             'thumbnail': view_data.get('pic'),
75             'uploader': view_data.get('author'),
76             'timestamp': int_or_none(view_data.get('created')),
77             'view_count': view_data.get('play'),
78             'duration': int_or_none(doc.find('./timelength').text),
79         }
80
81         if len(entries) == 1:
82             entries[0].update(info)
83             return entries[0]
84         else:
85             info.update({
86                 '_type': 'multi_video',
87                 'entries': entries,
88             })
89             return info
90
91     def _real_extract(self, url):
92         video_id = self._match_id(url)
93         view_data = self._download_json('http://api.bilibili.com/view?type=json&appkey=8e9fc618fbd41e28&id=%s' % video_id, video_id)
94
95         num_pages = int_or_none(view_data['pages'])
96         if num_pages > 1:
97             play_list_title = view_data['title']
98             page_list = self._download_json('http://www.bilibili.com/widget/getPageList?aid=%s' % video_id, video_id, 'Downloading page list metadata')
99             entries = []
100             for page in page_list:
101                 view_data['title'] = page['pagename']
102                 entries.append(self._extract_video_info(str(page['cid']), view_data, page['page'], num_pages))
103             return self.playlist_result(entries, video_id, play_list_title, view_data.get('description'))
104         else:
105             return self._extract_video_info(str(view_data['cid']), view_data)