]>
Commit | Line | Data |
---|---|---|
6f4e4132 YCH |
1 | import time |
2 | import hashlib | |
7274f3d0 | 3 | import re |
6f4e4132 | 4 | |
a172d962 | 5 | from .common import InfoExtractor |
3b4b82d4 YCH |
6 | from ..utils import ( |
7 | ExtractorError, | |
8 | unescapeHTML, | |
7274f3d0 YCH |
9 | unified_strdate, |
10 | urljoin, | |
3b4b82d4 | 11 | ) |
a172d962 | 12 | |
a172d962 | 13 | |
2ca1c5aa | 14 | class DouyuTVIE(InfoExtractor): |
513cbdda | 15 | IE_DESC = '斗鱼' |
33da98f4 | 16 | _VALID_URL = r'https?://(?:www\.)?douyu(?:tv)?\.com/(?:[^/]+/)*(?P<id>[A-Za-z0-9]+)' |
8343a033 | 17 | _TESTS = [{ |
a172d962 | 18 | 'url': 'http://www.douyutv.com/iseven', |
19 | 'info_dict': { | |
8343a033 YCH |
20 | 'id': '17732', |
21 | 'display_id': 'iseven', | |
6f4e4132 | 22 | 'ext': 'flv', |
c6fe5a7e | 23 | 'title': 're:^清晨醒脑!根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$', |
ec85ded8 YCH |
24 | 'description': r're:.*m7show@163\.com.*', |
25 | 'thumbnail': r're:^https?://.*\.jpg$', | |
2ca1c5aa | 26 | 'uploader': '7师傅', |
a172d962 | 27 | 'is_live': True, |
2ca1c5aa S |
28 | }, |
29 | 'params': { | |
30 | 'skip_download': True, | |
24ca0e9c | 31 | }, |
8343a033 YCH |
32 | }, { |
33 | 'url': 'http://www.douyutv.com/85982', | |
34 | 'info_dict': { | |
35 | 'id': '85982', | |
36 | 'display_id': '85982', | |
6f4e4132 | 37 | 'ext': 'flv', |
8343a033 YCH |
38 | 'title': 're:^小漠从零单排记!——CSOL2躲猫猫 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$', |
39 | 'description': 'md5:746a2f7a253966a06755a912f0acc0d2', | |
ec85ded8 | 40 | 'thumbnail': r're:^https?://.*\.jpg$', |
8343a033 | 41 | 'uploader': 'douyu小漠', |
8343a033 YCH |
42 | 'is_live': True, |
43 | }, | |
44 | 'params': { | |
45 | 'skip_download': True, | |
24ca0e9c | 46 | }, |
aa9dc24f | 47 | 'skip': 'Room not found', |
24ca0e9c YCH |
48 | }, { |
49 | 'url': 'http://www.douyutv.com/17732', | |
50 | 'info_dict': { | |
51 | 'id': '17732', | |
52 | 'display_id': '17732', | |
6f4e4132 | 53 | 'ext': 'flv', |
c6fe5a7e | 54 | 'title': 're:^清晨醒脑!根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$', |
ec85ded8 YCH |
55 | 'description': r're:.*m7show@163\.com.*', |
56 | 'thumbnail': r're:^https?://.*\.jpg$', | |
24ca0e9c | 57 | 'uploader': '7师傅', |
24ca0e9c YCH |
58 | 'is_live': True, |
59 | }, | |
60 | 'params': { | |
61 | 'skip_download': True, | |
62 | }, | |
3bb33568 YCH |
63 | }, { |
64 | 'url': 'http://www.douyu.com/xiaocang', | |
65 | 'only_matching': True, | |
33da98f4 J |
66 | }, { |
67 | # \"room_id\" | |
68 | 'url': 'http://www.douyu.com/t/lpl', | |
69 | 'only_matching': True, | |
8343a033 | 70 | }] |
a172d962 | 71 | |
72 | def _real_extract(self, url): | |
73 | video_id = self._match_id(url) | |
a172d962 | 74 | |
8343a033 YCH |
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( | |
33da98f4 | 80 | r'"room_id\\?"\s*:\s*(\d+),', page, 'room id') |
8343a033 | 81 | |
6f4e4132 | 82 | # Grab metadata from mobile API |
3b4b82d4 YCH |
83 | room = self._download_json( |
84 | 'http://m.douyu.com/html5/live?roomId=%s' % room_id, video_id, | |
85 | note='Downloading room info')['data'] | |
b281aad2 | 86 | |
b281aad2 | 87 | # 1 = live, 2 = offline |
3b4b82d4 YCH |
88 | if room.get('show_status') == '2': |
89 | raise ExtractorError('Live stream is offline', expected=True) | |
90 | ||
6f4e4132 YCH |
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'] | |
2ca1c5aa | 104 | |
39ca3b5c | 105 | title = unescapeHTML(room['room_name']) |
f4c68ba3 | 106 | description = room.get('show_details') |
b281aad2 | 107 | thumbnail = room.get('room_src') |
108 | uploader = room.get('nickname') | |
a172d962 | 109 | |
110 | return { | |
8343a033 YCH |
111 | 'id': room_id, |
112 | 'display_id': video_id, | |
6f4e4132 | 113 | 'url': video_url, |
a172d962 | 114 | 'title': title, |
2ca1c5aa | 115 | 'description': description, |
a172d962 | 116 | 'thumbnail': thumbnail, |
2ca1c5aa | 117 | 'uploader': uploader, |
a172d962 | 118 | 'is_live': True, |
2ca1c5aa | 119 | } |
7274f3d0 YCH |
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 | } |