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