]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/sohu.py
[misc] Add `hatch`, `ruff`, `pre-commit` and improve dev docs (#7409)
[yt-dlp.git] / yt_dlp / extractor / sohu.py
1 import base64
2 import re
3
4 from .common import InfoExtractor
5 from ..compat import (
6 compat_str,
7 compat_urllib_parse_urlencode,
8 )
9 from ..utils import (
10 ExtractorError,
11 float_or_none,
12 int_or_none,
13 traverse_obj,
14 try_get,
15 unified_timestamp,
16 url_or_none,
17 urljoin,
18 )
19
20
21 class SohuIE(InfoExtractor):
22 _VALID_URL = r'https?://(?P<mytv>my\.)?tv\.sohu\.com/.+?/(?(mytv)|n)(?P<id>\d+)\.shtml.*?'
23
24 # Sohu videos give different MD5 sums on Travis CI and my machine
25 _TESTS = [{
26 'note': 'This video is available only in Mainland China',
27 'url': 'http://tv.sohu.com/20130724/n382479172.shtml#super',
28 'info_dict': {
29 'id': '382479172',
30 'ext': 'mp4',
31 'title': 'MV:Far East Movement《The Illest》',
32 },
33 'skip': 'On available in China',
34 }, {
35 'url': 'http://tv.sohu.com/20150305/n409385080.shtml',
36 'info_dict': {
37 'id': '409385080',
38 'ext': 'mp4',
39 'title': '《2015湖南卫视羊年元宵晚会》唐嫣《花好月圆》',
40 },
41 'skip': 'no longer available',
42 }, {
43 'url': 'http://my.tv.sohu.com/us/232799889/78693464.shtml',
44 'info_dict': {
45 'id': '78693464',
46 'ext': 'mp4',
47 'title': '【爱范品】第31期:MWC见不到的奇葩手机',
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', '手机'],
54 }
55 }, {
56 'note': 'Multipart video',
57 'url': 'http://my.tv.sohu.com/pl/8384802/78910339.shtml',
58 'info_dict': {
59 'id': '78910339',
60 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
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', '英雄联盟', '实战秘籍'],
67 },
68 'playlist': [{
69 'info_dict': {
70 'id': '78910339_part1',
71 'ext': 'mp4',
72 'duration': 294,
73 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
74 }
75 }, {
76 'info_dict': {
77 'id': '78910339_part2',
78 'ext': 'mp4',
79 'duration': 300,
80 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
81 }
82 }, {
83 'info_dict': {
84 'id': '78910339_part3',
85 'ext': 'mp4',
86 'duration': 150,
87 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
88 }
89 }]
90 }, {
91 'note': 'Video with title containing dash',
92 'url': 'http://my.tv.sohu.com/us/249884221/78932792.shtml',
93 'info_dict': {
94 'id': '78932792',
95 'ext': 'mp4',
96 'title': 'youtube-dl testing video',
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': [],
102 },
103 'params': {
104 'skip_download': True
105 }
106 }]
107
108 def _real_extract(self, url):
109
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:
114 base_data_url = 'http://hot.vrs.sohu.com/vrs_flash.action?vid='
115
116 return self._download_json(
117 base_data_url + vid_id, video_id,
118 'Downloading JSON data for %s' % vid_id,
119 headers=self.geo_verification_headers())
120
121 mobj = self._match_valid_url(url)
122 video_id = mobj.group('id')
123 mytv = mobj.group('mytv') is not None
124
125 webpage = self._download_webpage(url, video_id)
126
127 title = re.sub(r'( - 高清正版在线观看)? - 搜狐视频$', '', self._og_search_title(webpage))
128
129 vid = self._html_search_regex(
130 r'var vid ?= ?["\'](\d+)["\']',
131 webpage, 'video path')
132 vid_data = _fetch_data(vid, mytv)
133 if vid_data['play'] != 1:
134 if vid_data.get('status') == 12:
135 raise ExtractorError(
136 '%s said: There\'s something wrong in the video.' % self.IE_NAME,
137 expected=True)
138 else:
139 self.raise_geo_restricted(
140 '%s said: The video is only licensed to users in Mainland China.' % self.IE_NAME)
141
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)
149
150 part_count = vid_data['data']['totalBlocks']
151
152 playlist = []
153 for i in range(part_count):
154 formats = []
155 for format_id, format_data in formats_json.items():
156 allot = format_data['allot']
157
158 data = format_data['data']
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}')
162 su = data['su']
163
164 video_url = 'newflv.sohu.ccgslb.net'
165 cdnId = None
166 retries = 0
167
168 while 'newflv.sohu.ccgslb.net' in video_url:
169 params = {
170 'prot': 9,
171 'file': clip_url,
172 'new': su[i],
173 'prod': 'h5n',
174 'rb': 1,
175 }
176
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(
186 'http://%s/?%s' % (allot, compat_urllib_parse_urlencode(params)),
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')
195
196 formats.append({
197 'url': video_url,
198 'format_id': format_id,
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')),
204 })
205
206 playlist.append({
207 'id': '%s_part%d' % (video_id, i + 1),
208 'title': title,
209 'duration': vid_data['data']['clipsDuration'][i],
210 'formats': formats,
211 })
212
213 if len(playlist) == 1:
214 info = playlist[0]
215 info['id'] = video_id
216 else:
217 info = {
218 '_type': 'multi_video',
219 'entries': playlist,
220 'id': video_id,
221 'title': title,
222 'duration': traverse_obj(vid_data, ('data', 'totalDuration', {float_or_none})),
223 }
224
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
243 class 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)