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