]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/sohu.py
[misc] Add `hatch`, `ruff`, `pre-commit` and improve dev docs (#7409)
[yt-dlp.git] / yt_dlp / extractor / sohu.py
CommitLineData
5be7e978 1import base64
f143d86a 2import re
6624a2b0 3
4from .common import InfoExtractor
5c7495a1
YCH
5from ..compat import (
6 compat_str,
15707c7e 7 compat_urllib_parse_urlencode,
5c7495a1 8)
1693bebe
S
9from ..utils import (
10 ExtractorError,
5be7e978 11 float_or_none,
e897bd82
SS
12 int_or_none,
13 traverse_obj,
1693bebe 14 try_get,
e897bd82
SS
15 unified_timestamp,
16 url_or_none,
5be7e978 17 urljoin,
1693bebe 18)
6624a2b0 19
20
21class SohuIE(InfoExtractor):
6d2d21f7 22 _VALID_URL = r'https?://(?P<mytv>my\.)?tv\.sohu\.com/.+?/(?(mytv)|n)(?P<id>\d+)\.shtml.*?'
6624a2b0 23
3dc240e8 24 # Sohu videos give different MD5 sums on Travis CI and my machine
5ee6fc97
YCH
25 _TESTS = [{
26 'note': 'This video is available only in Mainland China',
4c6d2ff8 27 'url': 'http://tv.sohu.com/20130724/n382479172.shtml#super',
4c6d2ff8
PH
28 'info_dict': {
29 'id': '382479172',
30 'ext': 'mp4',
31 'title': 'MV:Far East Movement《The Illest》',
6624a2b0 32 },
051df9ad 33 'skip': 'On available in China',
5ee6fc97
YCH
34 }, {
35 'url': 'http://tv.sohu.com/20150305/n409385080.shtml',
5ee6fc97
YCH
36 'info_dict': {
37 'id': '409385080',
38 'ext': 'mp4',
39 'title': '《2015湖南卫视羊年元宵晚会》唐嫣《花好月圆》',
5be7e978 40 },
41 'skip': 'no longer available',
5ee6fc97
YCH
42 }, {
43 'url': 'http://my.tv.sohu.com/us/232799889/78693464.shtml',
5ee6fc97
YCH
44 'info_dict': {
45 'id': '78693464',
46 'ext': 'mp4',
47 'title': '【爱范品】第31期:MWC见不到的奇葩手机',
5be7e978 48 'uploader': '爱范儿视频',
49 'duration': 213,
50 'timestamp': 1425519600,
51 'upload_date': '20150305',
52 'thumbnail': 'http://e3f49eaa46b57.cdn.sohucs.com//group1/M10/83/FA/MTAuMTAuODguODA=/6_14cbccdde5eg104SysCutcloud_78693464_7_0b.jpg',
53 'tags': ['爱范儿', '爱范品', 'MWC', '手机'],
5ee6fc97 54 }
cd65491c
YCH
55 }, {
56 'note': 'Multipart video',
57 'url': 'http://my.tv.sohu.com/pl/8384802/78910339.shtml',
58 'info_dict': {
59 'id': '78910339',
f158799b 60 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
5be7e978 61 'uploader': '小苍cany',
62 'duration': 744.0,
63 'timestamp': 1426269360,
64 'upload_date': '20150313',
65 'thumbnail': 'http://e3f49eaa46b57.cdn.sohucs.com//group1/M11/89/57/MTAuMTAuODguODA=/6_14cea022a1dg102SysCutcloud_78910339_8_0b.jpg',
66 'tags': ['小苍MM', '英雄联盟', '实战秘籍'],
cd65491c
YCH
67 },
68 'playlist': [{
cd65491c
YCH
69 'info_dict': {
70 'id': '78910339_part1',
71 'ext': 'mp4',
72 'duration': 294,
73 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
74 }
75 }, {
cd65491c
YCH
76 'info_dict': {
77 'id': '78910339_part2',
78 'ext': 'mp4',
79 'duration': 300,
80 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
81 }
82 }, {
cd65491c
YCH
83 'info_dict': {
84 'id': '78910339_part3',
85 'ext': 'mp4',
86 'duration': 150,
87 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
88 }
89 }]
2cb434e5 90 }, {
cd459b1d 91 'note': 'Video with title containing dash',
2cb434e5
YCH
92 'url': 'http://my.tv.sohu.com/us/249884221/78932792.shtml',
93 'info_dict': {
94 'id': '78932792',
95 'ext': 'mp4',
7a5c1cfe 96 'title': 'youtube-dl testing video',
5be7e978 97 'duration': 360,
98 'timestamp': 1426348620,
99 'upload_date': '20150314',
100 'thumbnail': 'http://e3f49eaa46b57.cdn.sohucs.com//group1/M02/8A/00/MTAuMTAuODguNzk=/6_14cee1be192g102SysCutcloud_78932792_7_7b.jpg',
101 'tags': [],
2cb434e5
YCH
102 },
103 'params': {
104 'skip_download': True
105 }
5ee6fc97 106 }]
6624a2b0 107
6624a2b0 108 def _real_extract(self, url):
f143d86a 109
6d2d21f7
JMF
110 def _fetch_data(vid_id, mytv=False):
111 if mytv:
112 base_data_url = 'http://my.tv.sohu.com/play/videonew.do?vid='
113 else:
4c6d2ff8 114 base_data_url = 'http://hot.vrs.sohu.com/vrs_flash.action?vid='
5ac71f0b 115
cd459b1d 116 return self._download_json(
38cce791
YCH
117 base_data_url + vid_id, video_id,
118 'Downloading JSON data for %s' % vid_id,
119 headers=self.geo_verification_headers())
f143d86a 120
5ad28e7f 121 mobj = self._match_valid_url(url)
6624a2b0 122 video_id = mobj.group('id')
6d2d21f7 123 mytv = mobj.group('mytv') is not None
f143d86a 124
6624a2b0 125 webpage = self._download_webpage(url, video_id)
2cb434e5 126
5be7e978 127 title = re.sub(r'( - 高清正版在线观看)? - 搜狐视频$', '', self._og_search_title(webpage))
6624a2b0 128
5ac71f0b
S
129 vid = self._html_search_regex(
130 r'var vid ?= ?["\'](\d+)["\']',
131 webpage, 'video path')
132 vid_data = _fetch_data(vid, mytv)
3dbec410
YCH
133 if vid_data['play'] != 1:
134 if vid_data.get('status') == 12:
135 raise ExtractorError(
c59f7036 136 '%s said: There\'s something wrong in the video.' % self.IE_NAME,
3dbec410
YCH
137 expected=True)
138 else:
c59f7036
RA
139 self.raise_geo_restricted(
140 '%s said: The video is only licensed to users in Mainland China.' % self.IE_NAME)
f143d86a 141
5ac71f0b
S
142 formats_json = {}
143 for format_id in ('nor', 'high', 'super', 'ori', 'h2644k', 'h2654k'):
144 vid_id = vid_data['data'].get('%sVid' % format_id)
145 if not vid_id:
146 continue
147 vid_id = compat_str(vid_id)
148 formats_json[format_id] = vid_data if vid == vid_id else _fetch_data(vid_id, mytv)
f143d86a 149
5ac71f0b 150 part_count = vid_data['data']['totalBlocks']
f143d86a
PH
151
152 playlist = []
153 for i in range(part_count):
5ac71f0b
S
154 formats = []
155 for format_id, format_data in formats_json.items():
3f3308cd 156 allot = format_data['allot']
3f3308cd 157
5ac71f0b 158 data = format_data['data']
5be7e978 159 clip_url = traverse_obj(data, (('clipsURL', 'mp4PlayUrl'), i, {url_or_none}), get_all=False)
160 if not clip_url:
161 raise ExtractorError(f'Unable to extract url for clip {i}')
3f3308cd
YCH
162 su = data['su']
163
98ca1024
YCH
164 video_url = 'newflv.sohu.ccgslb.net'
165 cdnId = None
166 retries = 0
3f3308cd 167
98ca1024
YCH
168 while 'newflv.sohu.ccgslb.net' in video_url:
169 params = {
170 'prot': 9,
5be7e978 171 'file': clip_url,
98ca1024 172 'new': su[i],
5be7e978 173 'prod': 'h5n',
5fd6cd64 174 'rb': 1,
98ca1024 175 }
5ee6fc97 176
98ca1024
YCH
177 if cdnId is not None:
178 params['idc'] = cdnId
179
180 download_note = 'Downloading %s video URL part %d of %d' % (
181 format_id, i + 1, part_count)
182
183 if retries > 0:
184 download_note += ' (retry #%d)' % retries
185 part_info = self._parse_json(self._download_webpage(
15707c7e 186 'http://%s/?%s' % (allot, compat_urllib_parse_urlencode(params)),
98ca1024
YCH
187 video_id, download_note), video_id)
188
189 video_url = part_info['url']
190 cdnId = part_info.get('nid')
191
192 retries += 1
193 if retries > 5:
194 raise ExtractorError('Failed to get video URL')
5ac71f0b
S
195
196 formats.append({
197 'url': video_url,
198 'format_id': format_id,
1693bebe
S
199 'filesize': int_or_none(
200 try_get(data, lambda x: x['clipsBytes'][i])),
201 'width': int_or_none(data.get('width')),
202 'height': int_or_none(data.get('height')),
203 'fps': int_or_none(data.get('fps')),
5ac71f0b 204 })
5ac71f0b
S
205
206 playlist.append({
207 'id': '%s_part%d' % (video_id, i + 1),
6624a2b0 208 'title': title,
5ac71f0b
S
209 'duration': vid_data['data']['clipsDuration'][i],
210 'formats': formats,
211 })
f143d86a
PH
212
213 if len(playlist) == 1:
214 info = playlist[0]
4ec929dc 215 info['id'] = video_id
f143d86a
PH
216 else:
217 info = {
f158799b 218 '_type': 'multi_video',
f143d86a
PH
219 'entries': playlist,
220 'id': video_id,
f158799b 221 'title': title,
5be7e978 222 'duration': traverse_obj(vid_data, ('data', 'totalDuration', {float_or_none})),
f143d86a
PH
223 }
224
5be7e978 225 if mytv:
226 publish_time = unified_timestamp(self._search_regex(
227 r'publishTime:\s*["\'](\d+-\d+-\d+ \d+:\d+)["\']', webpage, 'publish time', fatal=False))
228 else:
229 publish_time = traverse_obj(vid_data, ('tv_application_time', {unified_timestamp}))
230
231 return {
232 'timestamp': publish_time - 8 * 3600 if publish_time else None,
233 **traverse_obj(vid_data, {
234 'alt_title': ('data', 'subName', {str}),
235 'uploader': ('wm_data', 'wm_username', {str}),
236 'thumbnail': ('data', 'coverImg', {url_or_none}),
237 'tags': ('data', 'tag', {str.split}),
238 }),
239 **info,
240 }
241
242
243class SohuVIE(InfoExtractor):
244 _VALID_URL = r'https?://tv\.sohu\.com/v/(?P<id>[\w=-]+)\.html(?:$|[#?])'
245
246 _TESTS = [{
247 'note': 'Multipart video',
248 'url': 'https://tv.sohu.com/v/MjAyMzA2MTQvbjYwMTMxNTE5Mi5zaHRtbA==.html',
249 'info_dict': {
250 'id': '601315192',
251 'title': '《淬火丹心》第1集',
252 'alt_title': '“点天灯”发生事故',
253 'duration': 2701.692,
254 'timestamp': 1686758040,
255 'upload_date': '20230614',
256 'thumbnail': 'http://photocdn.tv.sohu.com/img/20230614/vrsa_hor_1686738763256_454010551.jpg',
257 },
258 'playlist_mincount': 9,
259 'skip': 'Only available in China',
260 }, {
261 'url': 'https://tv.sohu.com/v/dXMvMjMyNzk5ODg5Lzc4NjkzNDY0LnNodG1s.html',
262 'info_dict': {
263 'id': '78693464',
264 'ext': 'mp4',
265 'title': '【爱范品】第31期:MWC见不到的奇葩手机',
266 'uploader': '爱范儿视频',
267 'duration': 213,
268 'timestamp': 1425519600,
269 'upload_date': '20150305',
270 'thumbnail': 'http://e3f49eaa46b57.cdn.sohucs.com//group1/M10/83/FA/MTAuMTAuODguODA=/6_14cbccdde5eg104SysCutcloud_78693464_7_0b.jpg',
271 'tags': ['爱范儿', '爱范品', 'MWC', '手机'],
272 }
273 }, {
274 'note': 'Multipart video',
275 'url': 'https://tv.sohu.com/v/dXMvMjQyNTYyMTYzLzc4OTEwMzM5LnNodG1s.html?src=pl',
276 'info_dict': {
277 'id': '78910339',
278 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
279 'uploader': '小苍cany',
280 'duration': 744.0,
281 'timestamp': 1426269360,
282 'upload_date': '20150313',
283 'thumbnail': 'http://e3f49eaa46b57.cdn.sohucs.com//group1/M11/89/57/MTAuMTAuODguODA=/6_14cea022a1dg102SysCutcloud_78910339_8_0b.jpg',
284 'tags': ['小苍MM', '英雄联盟', '实战秘籍'],
285 },
286 'playlist_mincount': 3,
287 }]
288
289 def _real_extract(self, url):
290 encoded_id = self._match_id(url)
291 path = base64.urlsafe_b64decode(encoded_id).decode()
292 subdomain = 'tv' if re.match(r'\d+/n\d+\.shtml', path) else 'my.tv'
293 return self.url_result(urljoin(f'http://{subdomain}.sohu.com/', path), SohuIE)