]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/sohu.py
[extractor] Deprecate `_sort_formats`
[yt-dlp.git] / yt_dlp / extractor / sohu.py
CommitLineData
f143d86a 1import re
6624a2b0 2
3from .common import InfoExtractor
5c7495a1
YCH
4from ..compat import (
5 compat_str,
15707c7e 6 compat_urllib_parse_urlencode,
5c7495a1 7)
1693bebe
S
8from ..utils import (
9 ExtractorError,
10 int_or_none,
11 try_get,
12)
6624a2b0 13
14
15class SohuIE(InfoExtractor):
6d2d21f7 16 _VALID_URL = r'https?://(?P<mytv>my\.)?tv\.sohu\.com/.+?/(?(mytv)|n)(?P<id>\d+)\.shtml.*?'
6624a2b0 17
3dc240e8 18 # Sohu videos give different MD5 sums on Travis CI and my machine
5ee6fc97
YCH
19 _TESTS = [{
20 'note': 'This video is available only in Mainland China',
4c6d2ff8 21 'url': 'http://tv.sohu.com/20130724/n382479172.shtml#super',
4c6d2ff8
PH
22 'info_dict': {
23 'id': '382479172',
24 'ext': 'mp4',
25 'title': 'MV:Far East Movement《The Illest》',
6624a2b0 26 },
051df9ad 27 'skip': 'On available in China',
5ee6fc97
YCH
28 }, {
29 'url': 'http://tv.sohu.com/20150305/n409385080.shtml',
5ee6fc97
YCH
30 'info_dict': {
31 'id': '409385080',
32 'ext': 'mp4',
33 'title': '《2015湖南卫视羊年元宵晚会》唐嫣《花好月圆》',
34 }
35 }, {
36 'url': 'http://my.tv.sohu.com/us/232799889/78693464.shtml',
5ee6fc97
YCH
37 'info_dict': {
38 'id': '78693464',
39 'ext': 'mp4',
40 'title': '【爱范品】第31期:MWC见不到的奇葩手机',
41 }
cd65491c
YCH
42 }, {
43 'note': 'Multipart video',
44 'url': 'http://my.tv.sohu.com/pl/8384802/78910339.shtml',
45 'info_dict': {
46 'id': '78910339',
f158799b 47 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
cd65491c
YCH
48 },
49 'playlist': [{
cd65491c
YCH
50 'info_dict': {
51 'id': '78910339_part1',
52 'ext': 'mp4',
53 'duration': 294,
54 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
55 }
56 }, {
cd65491c
YCH
57 'info_dict': {
58 'id': '78910339_part2',
59 'ext': 'mp4',
60 'duration': 300,
61 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
62 }
63 }, {
cd65491c
YCH
64 'info_dict': {
65 'id': '78910339_part3',
66 'ext': 'mp4',
67 'duration': 150,
68 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
69 }
70 }]
2cb434e5 71 }, {
cd459b1d 72 'note': 'Video with title containing dash',
2cb434e5
YCH
73 'url': 'http://my.tv.sohu.com/us/249884221/78932792.shtml',
74 'info_dict': {
75 'id': '78932792',
76 'ext': 'mp4',
7a5c1cfe 77 'title': 'youtube-dl testing video',
2cb434e5
YCH
78 },
79 'params': {
80 'skip_download': True
81 }
5ee6fc97 82 }]
6624a2b0 83
6624a2b0 84 def _real_extract(self, url):
f143d86a 85
6d2d21f7
JMF
86 def _fetch_data(vid_id, mytv=False):
87 if mytv:
88 base_data_url = 'http://my.tv.sohu.com/play/videonew.do?vid='
89 else:
4c6d2ff8 90 base_data_url = 'http://hot.vrs.sohu.com/vrs_flash.action?vid='
5ac71f0b 91
cd459b1d 92 return self._download_json(
38cce791
YCH
93 base_data_url + vid_id, video_id,
94 'Downloading JSON data for %s' % vid_id,
95 headers=self.geo_verification_headers())
f143d86a 96
5ad28e7f 97 mobj = self._match_valid_url(url)
6624a2b0 98 video_id = mobj.group('id')
6d2d21f7 99 mytv = mobj.group('mytv') is not None
f143d86a 100
6624a2b0 101 webpage = self._download_webpage(url, video_id)
2cb434e5 102
f158799b 103 title = re.sub(r' - 搜狐视频$', '', self._og_search_title(webpage))
6624a2b0 104
5ac71f0b
S
105 vid = self._html_search_regex(
106 r'var vid ?= ?["\'](\d+)["\']',
107 webpage, 'video path')
108 vid_data = _fetch_data(vid, mytv)
3dbec410
YCH
109 if vid_data['play'] != 1:
110 if vid_data.get('status') == 12:
111 raise ExtractorError(
c59f7036 112 '%s said: There\'s something wrong in the video.' % self.IE_NAME,
3dbec410
YCH
113 expected=True)
114 else:
c59f7036
RA
115 self.raise_geo_restricted(
116 '%s said: The video is only licensed to users in Mainland China.' % self.IE_NAME)
f143d86a 117
5ac71f0b
S
118 formats_json = {}
119 for format_id in ('nor', 'high', 'super', 'ori', 'h2644k', 'h2654k'):
120 vid_id = vid_data['data'].get('%sVid' % format_id)
121 if not vid_id:
122 continue
123 vid_id = compat_str(vid_id)
124 formats_json[format_id] = vid_data if vid == vid_id else _fetch_data(vid_id, mytv)
f143d86a 125
5ac71f0b 126 part_count = vid_data['data']['totalBlocks']
f143d86a
PH
127
128 playlist = []
129 for i in range(part_count):
5ac71f0b
S
130 formats = []
131 for format_id, format_data in formats_json.items():
3f3308cd 132 allot = format_data['allot']
3f3308cd 133
5ac71f0b 134 data = format_data['data']
3f3308cd
YCH
135 clips_url = data['clipsURL']
136 su = data['su']
137
98ca1024
YCH
138 video_url = 'newflv.sohu.ccgslb.net'
139 cdnId = None
140 retries = 0
3f3308cd 141
98ca1024
YCH
142 while 'newflv.sohu.ccgslb.net' in video_url:
143 params = {
144 'prot': 9,
145 'file': clips_url[i],
146 'new': su[i],
147 'prod': 'flash',
5fd6cd64 148 'rb': 1,
98ca1024 149 }
5ee6fc97 150
98ca1024
YCH
151 if cdnId is not None:
152 params['idc'] = cdnId
153
154 download_note = 'Downloading %s video URL part %d of %d' % (
155 format_id, i + 1, part_count)
156
157 if retries > 0:
158 download_note += ' (retry #%d)' % retries
159 part_info = self._parse_json(self._download_webpage(
15707c7e 160 'http://%s/?%s' % (allot, compat_urllib_parse_urlencode(params)),
98ca1024
YCH
161 video_id, download_note), video_id)
162
163 video_url = part_info['url']
164 cdnId = part_info.get('nid')
165
166 retries += 1
167 if retries > 5:
168 raise ExtractorError('Failed to get video URL')
5ac71f0b
S
169
170 formats.append({
171 'url': video_url,
172 'format_id': format_id,
1693bebe
S
173 'filesize': int_or_none(
174 try_get(data, lambda x: x['clipsBytes'][i])),
175 'width': int_or_none(data.get('width')),
176 'height': int_or_none(data.get('height')),
177 'fps': int_or_none(data.get('fps')),
5ac71f0b 178 })
5ac71f0b
S
179
180 playlist.append({
181 'id': '%s_part%d' % (video_id, i + 1),
6624a2b0 182 'title': title,
5ac71f0b
S
183 'duration': vid_data['data']['clipsDuration'][i],
184 'formats': formats,
185 })
f143d86a
PH
186
187 if len(playlist) == 1:
188 info = playlist[0]
4ec929dc 189 info['id'] = video_id
f143d86a
PH
190 else:
191 info = {
f158799b 192 '_type': 'multi_video',
f143d86a
PH
193 'entries': playlist,
194 'id': video_id,
f158799b 195 'title': title,
f143d86a
PH
196 }
197
198 return info