]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/bilibili.py
debian/changelog: Finalize release.
[youtubedl] / youtube_dl / extractor / bilibili.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8 compat_parse_qs,
9 ExtractorError,
10 int_or_none,
11 unified_strdate,
12 )
13
14
15 class BiliBiliIE(InfoExtractor):
16 _VALID_URL = r'http://www\.bilibili\.(?:tv|com)/video/av(?P<id>[0-9]+)/'
17
18 _TEST = {
19 'url': 'http://www.bilibili.tv/video/av1074402/',
20 'md5': '2c301e4dab317596e837c3e7633e7d86',
21 'info_dict': {
22 'id': '1074402',
23 'ext': 'flv',
24 'title': '【金坷垃】金泡沫',
25 'duration': 308,
26 'upload_date': '20140420',
27 'thumbnail': 're:^https?://.+\.jpg',
28 },
29 }
30
31 def _real_extract(self, url):
32 mobj = re.match(self._VALID_URL, url)
33 video_id = mobj.group('id')
34
35 webpage = self._download_webpage(url, video_id)
36 video_code = self._search_regex(
37 r'(?s)<div itemprop="video".*?>(.*?)</div>', webpage, 'video code')
38
39 title = self._html_search_meta(
40 'media:title', video_code, 'title', fatal=True)
41 duration_str = self._html_search_meta(
42 'duration', video_code, 'duration')
43 if duration_str is None:
44 duration = None
45 else:
46 duration_mobj = re.match(
47 r'^T(?:(?P<hours>[0-9]+)H)?(?P<minutes>[0-9]+)M(?P<seconds>[0-9]+)S$',
48 duration_str)
49 duration = (
50 int_or_none(duration_mobj.group('hours'), default=0) * 3600 +
51 int(duration_mobj.group('minutes')) * 60 +
52 int(duration_mobj.group('seconds')))
53 upload_date = unified_strdate(self._html_search_meta(
54 'uploadDate', video_code, fatal=False))
55 thumbnail = self._html_search_meta(
56 'thumbnailUrl', video_code, 'thumbnail', fatal=False)
57
58 player_params = compat_parse_qs(self._html_search_regex(
59 r'<iframe .*?class="player" src="https://secure\.bilibili\.(?:tv|com)/secure,([^"]+)"',
60 webpage, 'player params'))
61
62 if 'cid' in player_params:
63 cid = player_params['cid'][0]
64
65 lq_doc = self._download_xml(
66 'http://interface.bilibili.cn/v_cdn_play?cid=%s' % cid,
67 video_id,
68 note='Downloading LQ video info'
69 )
70 lq_durl = lq_doc.find('.//durl')
71 formats = [{
72 'format_id': 'lq',
73 'quality': 1,
74 'url': lq_durl.find('./url').text,
75 'filesize': int_or_none(
76 lq_durl.find('./size'), get_attr='text'),
77 }]
78
79 hq_doc = self._download_xml(
80 'http://interface.bilibili.cn/playurl?cid=%s' % cid,
81 video_id,
82 note='Downloading HQ video info',
83 fatal=False,
84 )
85 if hq_doc is not False:
86 hq_durl = hq_doc.find('.//durl')
87 formats.append({
88 'format_id': 'hq',
89 'quality': 2,
90 'ext': 'flv',
91 'url': hq_durl.find('./url').text,
92 'filesize': int_or_none(
93 hq_durl.find('./size'), get_attr='text'),
94 })
95 else:
96 raise ExtractorError('Unsupported player parameters: %r' % (player_params,))
97
98 self._sort_formats(formats)
99 return {
100 'id': video_id,
101 'title': title,
102 'formats': formats,
103 'duration': duration,
104 'upload_date': upload_date,
105 'thumbnail': thumbnail,
106 }