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