]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/bilibili.py
ecc17ebebca9e1819fc804f37d48dcceb80c44c5
[youtubedl] / youtube_dl / extractor / bilibili.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 import itertools
6 import json
7 import xml.etree.ElementTree as ET
8
9 from .common import InfoExtractor
10 from ..utils import (
11 int_or_none,
12 unified_strdate,
13 ExtractorError,
14 )
15
16
17 class BiliBiliIE(InfoExtractor):
18 _VALID_URL = r'http://www\.bilibili\.(?:tv|com)/video/av(?P<id>[0-9]+)/'
19
20 _TESTS = [{
21 'url': 'http://www.bilibili.tv/video/av1074402/',
22 'md5': '2c301e4dab317596e837c3e7633e7d86',
23 'info_dict': {
24 'id': '1074402_part1',
25 'ext': 'flv',
26 'title': '【金坷垃】金泡沫',
27 'duration': 308,
28 'upload_date': '20140420',
29 'thumbnail': 're:^https?://.+\.jpg',
30 },
31 }, {
32 'url': 'http://www.bilibili.com/video/av1041170/',
33 'info_dict': {
34 'id': '1041170',
35 'title': '【BD1080P】刀语【诸神&异域】',
36 },
37 'playlist_count': 9,
38 }]
39
40 def _real_extract(self, url):
41 video_id = self._match_id(url)
42 webpage = self._download_webpage(url, video_id)
43
44 if '(此视频不存在或被删除)' in webpage:
45 raise ExtractorError(
46 'The video does not exist or was deleted', expected=True)
47
48 if '>你没有权限浏览! 由于版权相关问题 我们不对您所在的地区提供服务<' in webpage:
49 raise ExtractorError(
50 'The video is not available in your region due to copyright reasons',
51 expected=True)
52
53 video_code = self._search_regex(
54 r'(?s)<div itemprop="video".*?>(.*?)</div>', webpage, 'video code')
55
56 title = self._html_search_meta(
57 'media:title', video_code, 'title', fatal=True)
58 duration_str = self._html_search_meta(
59 'duration', video_code, 'duration')
60 if duration_str is None:
61 duration = None
62 else:
63 duration_mobj = re.match(
64 r'^T(?:(?P<hours>[0-9]+)H)?(?P<minutes>[0-9]+)M(?P<seconds>[0-9]+)S$',
65 duration_str)
66 duration = (
67 int_or_none(duration_mobj.group('hours'), default=0) * 3600 +
68 int(duration_mobj.group('minutes')) * 60 +
69 int(duration_mobj.group('seconds')))
70 upload_date = unified_strdate(self._html_search_meta(
71 'uploadDate', video_code, fatal=False))
72 thumbnail = self._html_search_meta(
73 'thumbnailUrl', video_code, 'thumbnail', fatal=False)
74
75 cid = self._search_regex(r'cid=(\d+)', webpage, 'cid')
76
77 entries = []
78
79 lq_page = self._download_webpage(
80 'http://interface.bilibili.com/v_cdn_play?appkey=1&cid=%s' % cid,
81 video_id,
82 note='Downloading LQ video info'
83 )
84 try:
85 err_info = json.loads(lq_page)
86 raise ExtractorError(
87 'BiliBili said: ' + err_info['error_text'], expected=True)
88 except ValueError:
89 pass
90
91 lq_doc = ET.fromstring(lq_page)
92 lq_durls = lq_doc.findall('./durl')
93
94 hq_doc = self._download_xml(
95 'http://interface.bilibili.com/playurl?appkey=1&cid=%s' % cid,
96 video_id,
97 note='Downloading HQ video info',
98 fatal=False,
99 )
100 if hq_doc is not False:
101 hq_durls = hq_doc.findall('./durl')
102 assert len(lq_durls) == len(hq_durls)
103 else:
104 hq_durls = itertools.repeat(None)
105
106 i = 1
107 for lq_durl, hq_durl in zip(lq_durls, hq_durls):
108 formats = [{
109 'format_id': 'lq',
110 'quality': 1,
111 'url': lq_durl.find('./url').text,
112 'filesize': int_or_none(
113 lq_durl.find('./size'), get_attr='text'),
114 }]
115 if hq_durl is not None:
116 formats.append({
117 'format_id': 'hq',
118 'quality': 2,
119 'ext': 'flv',
120 'url': hq_durl.find('./url').text,
121 'filesize': int_or_none(
122 hq_durl.find('./size'), get_attr='text'),
123 })
124 self._sort_formats(formats)
125
126 entries.append({
127 'id': '%s_part%d' % (video_id, i),
128 'title': title,
129 'formats': formats,
130 'duration': duration,
131 'upload_date': upload_date,
132 'thumbnail': thumbnail,
133 })
134
135 i += 1
136
137 return {
138 '_type': 'multi_video',
139 'entries': entries,
140 'id': video_id,
141 'title': title
142 }