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