]>
Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/bilibili.py
2 from __future__
import unicode_literals
7 import xml
.etree
.ElementTree
as ET
9 from .common
import InfoExtractor
17 class BiliBiliIE(InfoExtractor
):
18 _VALID_URL
= r
'http://www\.bilibili\.(?:tv|com)/video/av(?P<id>[0-9]+)/'
21 'url': 'http://www.bilibili.tv/video/av1074402/',
22 'md5': '2c301e4dab317596e837c3e7633e7d86',
24 'id': '1074402_part1',
28 'upload_date': '20140420',
29 'thumbnail': 're:^https?://.+\.jpg',
32 'url': 'http://www.bilibili.com/video/av1041170/',
35 'title': '【BD1080P】刀语【诸神&异域】',
40 def _real_extract(self
, url
):
41 video_id
= self
._match
_id
(url
)
42 webpage
= self
._download
_webpage
(url
, video_id
)
44 if self
._search
_regex
(r
'(此视频不存在或被删除)', webpage
, 'error message', default
=None):
45 raise ExtractorError('The video does not exist or was deleted', expected
=True)
46 video_code
= self
._search
_regex
(
47 r
'(?s)<div itemprop="video".*?>(.*?)</div>', webpage
, 'video code')
49 title
= self
._html
_search
_meta
(
50 'media:title', video_code
, 'title', fatal
=True)
51 duration_str
= self
._html
_search
_meta
(
52 'duration', video_code
, 'duration')
53 if duration_str
is None:
56 duration_mobj
= re
.match(
57 r
'^T(?:(?P<hours>[0-9]+)H)?(?P<minutes>[0-9]+)M(?P<seconds>[0-9]+)S$',
60 int_or_none(duration_mobj
.group('hours'), default
=0) * 3600 +
61 int(duration_mobj
.group('minutes')) * 60 +
62 int(duration_mobj
.group('seconds')))
63 upload_date
= unified_strdate(self
._html
_search
_meta
(
64 'uploadDate', video_code
, fatal
=False))
65 thumbnail
= self
._html
_search
_meta
(
66 'thumbnailUrl', video_code
, 'thumbnail', fatal
=False)
68 cid
= self
._search
_regex
(r
'cid=(\d+)', webpage
, 'cid')
72 lq_page
= self
._download
_webpage
(
73 'http://interface.bilibili.com/v_cdn_play?appkey=1&cid=%s' % cid
,
75 note
='Downloading LQ video info'
78 err_info
= json
.loads(lq_page
)
80 'BiliBili said: ' + err_info
['error_text'], expected
=True)
84 lq_doc
= ET
.fromstring(lq_page
)
85 lq_durls
= lq_doc
.findall('./durl')
87 hq_doc
= self
._download
_xml
(
88 'http://interface.bilibili.com/playurl?appkey=1&cid=%s' % cid
,
90 note
='Downloading HQ video info',
93 if hq_doc
is not False:
94 hq_durls
= hq_doc
.findall('./durl')
95 assert len(lq_durls
) == len(hq_durls
)
97 hq_durls
= itertools
.repeat(None)
100 for lq_durl
, hq_durl
in zip(lq_durls
, hq_durls
):
104 'url': lq_durl
.find('./url').text
,
105 'filesize': int_or_none(
106 lq_durl
.find('./size'), get_attr
='text'),
113 'url': hq_durl
.find('./url').text
,
114 'filesize': int_or_none(
115 hq_durl
.find('./size'), get_attr
='text'),
117 self
._sort
_formats
(formats
)
120 'id': '%s_part%d' % (video_id
, i
),
123 'duration': duration
,
124 'upload_date': upload_date
,
125 'thumbnail': thumbnail
,
131 '_type': 'multi_video',