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