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