]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/bilibili.py
Fix "invalid escape sequences" error on Python 3.6
[yt-dlp.git] / youtube_dl / extractor / bilibili.py
CommitLineData
28746fbd
PH
1# coding: utf-8
2from __future__ import unicode_literals
3
04b32c8f 4import hashlib
520e7533 5import re
28746fbd
PH
6
7from .common import InfoExtractor
04b32c8f 8from ..compat import compat_parse_qs
28746fbd 9from ..utils import (
6461f2b7
YCH
10 int_or_none,
11 float_or_none,
04b32c8f 12 unified_timestamp,
1f85029d 13 urlencode_postdata,
28746fbd
PH
14)
15
16
17class BiliBiliIE(InfoExtractor):
1f85029d 18 _VALID_URL = r'https?://(?:www\.|bangumi\.|)bilibili\.(?:tv|com)/(?:video/av|anime/v/)(?P<id>\d+)'
28746fbd 19
4875ff68 20 _TEST = {
28746fbd 21 'url': 'http://www.bilibili.tv/video/av1074402/',
412abb87 22 'md5': '9fa226fe2b8a9a4d5a69b4c6a183417e',
28746fbd 23 'info_dict': {
04b32c8f 24 'id': '1074402',
412abb87 25 'ext': 'mp4',
28746fbd 26 'title': '【金坷垃】金泡沫',
6461f2b7 27 'description': 'md5:ce18c2a2d2193f0df2917d270f2e5923',
412abb87 28 'duration': 308.315,
6461f2b7 29 'timestamp': 1398012660,
28746fbd 30 'upload_date': '20140420',
ec85ded8 31 'thumbnail': r're:^https?://.+\.jpg',
d90e4030 32 'uploader': '菊子桑',
6461f2b7 33 'uploader_id': '156160',
28746fbd 34 },
4875ff68 35 }
28746fbd 36
04b32c8f
YCH
37 _APP_KEY = '6f90a59ac58a4123'
38 _BILIBILI_KEY = '0bfd84cc3940035173f35e6777508326'
6461f2b7 39
520e7533 40 def _real_extract(self, url):
04b32c8f 41 video_id = self._match_id(url)
6461f2b7
YCH
42 webpage = self._download_webpage(url, video_id)
43
1f85029d 44 if 'anime/v' not in url:
7be15d40
P
45 cid = compat_parse_qs(self._search_regex(
46 [r'EmbedPlayer\([^)]+,\s*"([^"]+)"\)',
1f85029d 47 r'<iframe[^>]+src="https://secure\.bilibili\.com/secure,([^"]+)"'],
7be15d40
P
48 webpage, 'player parameters'))['cid'][0]
49 else:
1f85029d
YCH
50 js = self._download_json(
51 'http://bangumi.bilibili.com/web_api/get_source', video_id,
52 data=urlencode_postdata({'episode_id': video_id}),
53 headers={'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'})
7be15d40 54 cid = js['result']['cid']
04b32c8f
YCH
55
56 payload = 'appkey=%s&cid=%s&otype=json&quality=2&type=mp4' % (self._APP_KEY, cid)
57 sign = hashlib.md5((payload + self._BILIBILI_KEY).encode('utf-8')).hexdigest()
6d00a2dc 58
04b32c8f
YCH
59 video_info = self._download_json(
60 'http://interface.bilibili.com/playurl?%s&sign=%s' % (payload, sign),
61 video_id, note='Downloading video info page')
28746fbd 62
d90e4030 63 entries = []
c4a21bc9 64
04b32c8f 65 for idx, durl in enumerate(video_info['durl']):
497f5fd9 66 formats = [{
04b32c8f
YCH
67 'url': durl['url'],
68 'filesize': int_or_none(durl['size']),
497f5fd9 69 }]
86d68f90 70 for backup_url in durl.get('backup_url', []):
6461f2b7 71 formats.append({
04b32c8f 72 'url': backup_url,
6461f2b7 73 # backup URLs have lower priorities
04b32c8f 74 'preference': -2 if 'hd.mp4' in backup_url else -3,
6461f2b7
YCH
75 })
76
77 self._sort_formats(formats)
497f5fd9 78
c4a21bc9 79 entries.append({
04b32c8f
YCH
80 'id': '%s_part%s' % (video_id, idx),
81 'duration': float_or_none(durl.get('length'), 1000),
55af2b26 82 'formats': formats,
58a84b8c 83 })
28746fbd 84
6461f2b7
YCH
85 title = self._html_search_regex('<h1[^>]+title="([^"]+)">', webpage, 'title')
86 description = self._html_search_meta('description', webpage)
04b32c8f
YCH
87 timestamp = unified_timestamp(self._html_search_regex(
88 r'<time[^>]+datetime="([^"]+)"', webpage, 'upload time', fatal=False))
1f85029d 89 thumbnail = self._html_search_meta(['og:image', 'thumbnailUrl'], webpage)
6461f2b7
YCH
90
91 # TODO 'view_count' requires deobfuscating Javascript
d90e4030 92 info = {
04b32c8f 93 'id': video_id,
d90e4030 94 'title': title,
6461f2b7
YCH
95 'description': description,
96 'timestamp': timestamp,
7be15d40 97 'thumbnail': thumbnail,
04b32c8f 98 'duration': float_or_none(video_info.get('timelength'), scale=1000),
28746fbd 99 }
d90e4030 100
6461f2b7
YCH
101 uploader_mobj = re.search(
102 r'<a[^>]+href="https?://space\.bilibili\.com/(?P<id>\d+)"[^>]+title="(?P<name>[^"]+)"',
103 webpage)
104 if uploader_mobj:
105 info.update({
106 'uploader': uploader_mobj.group('name'),
107 'uploader_id': uploader_mobj.group('id'),
108 })
109
110 for entry in entries:
111 entry.update(info)
112
d90e4030 113 if len(entries) == 1:
d90e4030 114 return entries[0]
115 else:
ad73083f
YCH
116 for idx, entry in enumerate(entries):
117 entry['id'] = '%s_part%d' % (video_id, (idx + 1))
118
6461f2b7 119 return {
d90e4030 120 '_type': 'multi_video',
520e7533 121 'id': video_id,
6461f2b7
YCH
122 'title': title,
123 'description': description,
d90e4030 124 'entries': entries,
6461f2b7 125 }