]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/douyutv.py
[extractor/youtube] Extract DRC formats
[yt-dlp.git] / yt_dlp / extractor / douyutv.py
1 import time
2 import hashlib
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7 ExtractorError,
8 unescapeHTML,
9 unified_strdate,
10 urljoin,
11 )
12
13
14 class DouyuTVIE(InfoExtractor):
15 IE_DESC = '斗鱼'
16 _VALID_URL = r'https?://(?:www\.)?douyu(?:tv)?\.com/(?:[^/]+/)*(?P<id>[A-Za-z0-9]+)'
17 _TESTS = [{
18 'url': 'http://www.douyutv.com/iseven',
19 'info_dict': {
20 'id': '17732',
21 'display_id': 'iseven',
22 'ext': 'flv',
23 'title': 're:^清晨醒脑!根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
24 'description': r're:.*m7show@163\.com.*',
25 'thumbnail': r're:^https?://.*\.jpg$',
26 'uploader': '7师傅',
27 'is_live': True,
28 },
29 'params': {
30 'skip_download': True,
31 },
32 }, {
33 'url': 'http://www.douyutv.com/85982',
34 'info_dict': {
35 'id': '85982',
36 'display_id': '85982',
37 'ext': 'flv',
38 'title': 're:^小漠从零单排记!——CSOL2躲猫猫 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
39 'description': 'md5:746a2f7a253966a06755a912f0acc0d2',
40 'thumbnail': r're:^https?://.*\.jpg$',
41 'uploader': 'douyu小漠',
42 'is_live': True,
43 },
44 'params': {
45 'skip_download': True,
46 },
47 'skip': 'Room not found',
48 }, {
49 'url': 'http://www.douyutv.com/17732',
50 'info_dict': {
51 'id': '17732',
52 'display_id': '17732',
53 'ext': 'flv',
54 'title': 're:^清晨醒脑!根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
55 'description': r're:.*m7show@163\.com.*',
56 'thumbnail': r're:^https?://.*\.jpg$',
57 'uploader': '7师傅',
58 'is_live': True,
59 },
60 'params': {
61 'skip_download': True,
62 },
63 }, {
64 'url': 'http://www.douyu.com/xiaocang',
65 'only_matching': True,
66 }, {
67 # \"room_id\"
68 'url': 'http://www.douyu.com/t/lpl',
69 'only_matching': True,
70 }]
71
72 def _real_extract(self, url):
73 video_id = self._match_id(url)
74
75 if video_id.isdigit():
76 room_id = video_id
77 else:
78 page = self._download_webpage(url, video_id)
79 room_id = self._html_search_regex(
80 r'"room_id\\?"\s*:\s*(\d+),', page, 'room id')
81
82 # Grab metadata from mobile API
83 room = self._download_json(
84 'http://m.douyu.com/html5/live?roomId=%s' % room_id, video_id,
85 note='Downloading room info')['data']
86
87 # 1 = live, 2 = offline
88 if room.get('show_status') == '2':
89 raise ExtractorError('Live stream is offline', expected=True)
90
91 # Grab the URL from PC client API
92 # The m3u8 url from mobile API requires re-authentication every 5 minutes
93 tt = int(time.time())
94 signContent = 'lapi/live/thirdPart/getPlay/%s?aid=pcclient&rate=0&time=%d9TUk5fjjUjg9qIMH3sdnh' % (room_id, tt)
95 sign = hashlib.md5(signContent.encode('ascii')).hexdigest()
96 video_url = self._download_json(
97 'http://coapi.douyucdn.cn/lapi/live/thirdPart/getPlay/' + room_id,
98 video_id, note='Downloading video URL info',
99 query={'rate': 0}, headers={
100 'auth': sign,
101 'time': str(tt),
102 'aid': 'pcclient'
103 })['data']['live_url']
104
105 title = unescapeHTML(room['room_name'])
106 description = room.get('show_details')
107 thumbnail = room.get('room_src')
108 uploader = room.get('nickname')
109
110 return {
111 'id': room_id,
112 'display_id': video_id,
113 'url': video_url,
114 'title': title,
115 'description': description,
116 'thumbnail': thumbnail,
117 'uploader': uploader,
118 'is_live': True,
119 }
120
121
122 class DouyuShowIE(InfoExtractor):
123 _VALID_URL = r'https?://v(?:mobile)?\.douyu\.com/show/(?P<id>[0-9a-zA-Z]+)'
124
125 _TESTS = [{
126 'url': 'https://v.douyu.com/show/rjNBdvnVXNzvE2yw',
127 'md5': '0c2cfd068ee2afe657801269b2d86214',
128 'info_dict': {
129 'id': 'rjNBdvnVXNzvE2yw',
130 'ext': 'mp4',
131 'title': '陈一发儿:砒霜 我有个室友系列!04-01 22点场',
132 'duration': 7150.08,
133 'thumbnail': r're:^https?://.*\.jpg$',
134 'uploader': '陈一发儿',
135 'uploader_id': 'XrZwYelr5wbK',
136 'uploader_url': 'https://v.douyu.com/author/XrZwYelr5wbK',
137 'upload_date': '20170402',
138 },
139 }, {
140 'url': 'https://vmobile.douyu.com/show/rjNBdvnVXNzvE2yw',
141 'only_matching': True,
142 }]
143
144 def _real_extract(self, url):
145 url = url.replace('vmobile.', 'v.')
146 video_id = self._match_id(url)
147
148 webpage = self._download_webpage(url, video_id)
149
150 room_info = self._parse_json(self._search_regex(
151 r'var\s+\$ROOM\s*=\s*({.+});', webpage, 'room info'), video_id)
152
153 video_info = None
154
155 for trial in range(5):
156 # Sometimes Douyu rejects our request. Let's try it more times
157 try:
158 video_info = self._download_json(
159 'https://vmobile.douyu.com/video/getInfo', video_id,
160 query={'vid': video_id},
161 headers={
162 'Referer': url,
163 'x-requested-with': 'XMLHttpRequest',
164 })
165 break
166 except ExtractorError:
167 self._sleep(1, video_id)
168
169 if not video_info:
170 raise ExtractorError('Can\'t fetch video info')
171
172 formats = self._extract_m3u8_formats(
173 video_info['data']['video_url'], video_id,
174 entry_protocol='m3u8_native', ext='mp4')
175
176 upload_date = unified_strdate(self._html_search_regex(
177 r'<em>上传时间:</em><span>([^<]+)</span>', webpage,
178 'upload date', fatal=False))
179
180 uploader = uploader_id = uploader_url = None
181 mobj = re.search(
182 r'(?m)<a[^>]+href="/author/([0-9a-zA-Z]+)".+?<strong[^>]+title="([^"]+)"',
183 webpage)
184 if mobj:
185 uploader_id, uploader = mobj.groups()
186 uploader_url = urljoin(url, '/author/' + uploader_id)
187
188 return {
189 'id': video_id,
190 'title': room_info['name'],
191 'formats': formats,
192 'duration': room_info.get('duration'),
193 'thumbnail': room_info.get('pic'),
194 'upload_date': upload_date,
195 'uploader': uploader,
196 'uploader_id': uploader_id,
197 'uploader_url': uploader_url,
198 }