[bilibili] Capture the video-not-exist message
[youtube-dl] / 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     ExtractorError,
11 )
12
13
14 class BiliBiliIE(InfoExtractor):
15     _VALID_URL = r'http://www\.bilibili\.(?:tv|com)/video/av(?P<id>[0-9]+)/'
16
17     _TEST = {
18         'url': 'http://www.bilibili.tv/video/av1074402/',
19         'md5': '2c301e4dab317596e837c3e7633e7d86',
20         'info_dict': {
21             'id': '1074402',
22             'ext': 'flv',
23             'title': '【金坷垃】金泡沫',
24             'duration': 308,
25             'upload_date': '20140420',
26             'thumbnail': 're:^https?://.+\.jpg',
27         },
28     }
29
30     def _real_extract(self, url):
31         video_id = self._match_id(url)
32         webpage = self._download_webpage(url, video_id)
33
34         if self._search_regex(r'(此视频不存在或被删除)', webpage, 'error message', default=None):
35             raise ExtractorError('The video does not exist or was deleted', expected=True)
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         cid = self._search_regex(r'cid=(\d+)', webpage, 'cid')
59
60         lq_doc = self._download_xml(
61             'http://interface.bilibili.com/v_cdn_play?appkey=1&cid=%s' % cid,
62             video_id,
63             note='Downloading LQ video info'
64         )
65         lq_durl = lq_doc.find('./durl')
66         formats = [{
67             'format_id': 'lq',
68             'quality': 1,
69             'url': lq_durl.find('./url').text,
70             'filesize': int_or_none(
71                 lq_durl.find('./size'), get_attr='text'),
72         }]
73
74         hq_doc = self._download_xml(
75             'http://interface.bilibili.com/playurl?appkey=1&cid=%s' % cid,
76             video_id,
77             note='Downloading HQ video info',
78             fatal=False,
79         )
80         if hq_doc is not False:
81             hq_durl = hq_doc.find('./durl')
82             formats.append({
83                 'format_id': 'hq',
84                 'quality': 2,
85                 'ext': 'flv',
86                 'url': hq_durl.find('./url').text,
87                 'filesize': int_or_none(
88                     hq_durl.find('./size'), get_attr='text'),
89             })
90
91         self._sort_formats(formats)
92         return {
93             'id': video_id,
94             'title': title,
95             'formats': formats,
96             'duration': duration,
97             'upload_date': upload_date,
98             'thumbnail': thumbnail,
99         }