2 from __future__ 
import unicode_literals
 
   6 from .common 
import InfoExtractor
 
   9     compat_urllib_parse_urlencode
, 
  18 class SohuIE(InfoExtractor
): 
  19     _VALID_URL 
= r
'https?://(?P<mytv>my\.)?tv\.sohu\.com/.+?/(?(mytv)|n)(?P<id>\d+)\.shtml.*?' 
  21     # Sohu videos give different MD5 sums on Travis CI and my machine 
  23         'note': 'This video is available only in Mainland China', 
  24         'url': 'http://tv.sohu.com/20130724/n382479172.shtml#super', 
  28             'title': 'MV:Far East Movement《The Illest》', 
  30         'skip': 'On available in China', 
  32         'url': 'http://tv.sohu.com/20150305/n409385080.shtml', 
  36             'title': '《2015湖南卫视羊年元宵晚会》唐嫣《花好月圆》', 
  39         'url': 'http://my.tv.sohu.com/us/232799889/78693464.shtml', 
  43             'title': '【爱范品】第31期:MWC见不到的奇葩手机', 
  46         'note': 'Multipart video', 
  47         'url': 'http://my.tv.sohu.com/pl/8384802/78910339.shtml', 
  50             'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆', 
  54                 'id': '78910339_part1', 
  57                 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆', 
  61                 'id': '78910339_part2', 
  64                 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆', 
  68                 'id': '78910339_part3', 
  71                 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆', 
  75         'note': 'Video with title containing dash', 
  76         'url': 'http://my.tv.sohu.com/us/249884221/78932792.shtml', 
  80             'title': 'youtube-dl testing video', 
  87     def _real_extract(self
, url
): 
  89         def _fetch_data(vid_id
, mytv
=False): 
  91                 base_data_url 
= 'http://my.tv.sohu.com/play/videonew.do?vid=' 
  93                 base_data_url 
= 'http://hot.vrs.sohu.com/vrs_flash.action?vid=' 
  95             return self
._download
_json
( 
  96                 base_data_url 
+ vid_id
, video_id
, 
  97                 'Downloading JSON data for %s' % vid_id
, 
  98                 headers
=self
.geo_verification_headers()) 
 100         mobj 
= re
.match(self
._VALID
_URL
, url
) 
 101         video_id 
= mobj
.group('id') 
 102         mytv 
= mobj
.group('mytv') is not None 
 104         webpage 
= self
._download
_webpage
(url
, video_id
) 
 106         title 
= re
.sub(r
' - 搜狐视频$', '', self
._og
_search
_title
(webpage
)) 
 108         vid 
= self
._html
_search
_regex
( 
 109             r
'var vid ?= ?["\'](\d
+)["\']', 
 110             webpage, 'video path') 
 111         vid_data = _fetch_data(vid, mytv) 
 112         if vid_data['play'] != 1: 
 113             if vid_data.get('status') == 12: 
 114                 raise ExtractorError( 
 115                     '%s said: There\'s something wrong in the video.' % self.IE_NAME, 
 118                 self.raise_geo_restricted( 
 119                     '%s said: The video is only licensed to users in Mainland China.' % self.IE_NAME) 
 122         for format_id in ('nor', 'high', 'super', 'ori', 'h2644k', 'h2654k'): 
 123             vid_id = vid_data['data'].get('%sVid' % format_id) 
 126             vid_id = compat_str(vid_id) 
 127             formats_json[format_id] = vid_data if vid == vid_id else _fetch_data(vid_id, mytv) 
 129         part_count = vid_data['data']['totalBlocks'] 
 132         for i in range(part_count): 
 134             for format_id, format_data in formats_json.items(): 
 135                 allot = format_data['allot'] 
 137                 data = format_data['data'] 
 138                 clips_url = data['clipsURL'] 
 141                 video_url = 'newflv.sohu.ccgslb.net' 
 145                 while 'newflv.sohu.ccgslb.net' in video_url: 
 148                         'file': clips_url[i], 
 154                     if cdnId is not None: 
 155                         params['idc'] = cdnId 
 157                     download_note = 'Downloading %s video URL part %d of %d' % ( 
 158                         format_id, i + 1, part_count) 
 161                         download_note += ' (retry #%d)' % retries 
 162                     part_info = self._parse_json(self._download_webpage( 
 163                         'http://%s/?%s' % (allot, compat_urllib_parse_urlencode(params)), 
 164                         video_id, download_note), video_id) 
 166                     video_url = part_info['url'] 
 167                     cdnId = part_info.get('nid') 
 171                         raise ExtractorError('Failed to get video URL') 
 175                     'format_id': format_id, 
 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')), 
 182             self._sort_formats(formats) 
 185                 'id': '%s_part%d' % (video_id, i + 1), 
 187                 'duration': vid_data['data']['clipsDuration'][i], 
 191         if len(playlist) == 1: 
 193             info['id'] = video_id 
 196                 '_type': 'multi_video',