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