]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/sohu.py
Update changelog.
[youtubedl] / youtube_dl / extractor / sohu.py
1 # encoding: 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_request
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 _TESTS = [{
18 'note': 'This video is available only in Mainland China',
19 'url': 'http://tv.sohu.com/20130724/n382479172.shtml#super',
20 'md5': '29175c8cadd8b5cc4055001e85d6b372',
21 'info_dict': {
22 'id': '382479172',
23 'ext': 'mp4',
24 'title': 'MV:Far East Movement《The Illest》',
25 },
26 'params': {
27 'cn_verification_proxy': 'proxy.uku.im:8888'
28 }
29 }, {
30 'url': 'http://tv.sohu.com/20150305/n409385080.shtml',
31 'md5': 'ac9a5d322b4bf9ae184d53e4711e4f1a',
32 'info_dict': {
33 'id': '409385080',
34 'ext': 'mp4',
35 'title': '《2015湖南卫视羊年元宵晚会》唐嫣《花好月圆》',
36 }
37 }, {
38 'url': 'http://my.tv.sohu.com/us/232799889/78693464.shtml',
39 'md5': '49308ff6dafde5ece51137d04aec311e',
40 'info_dict': {
41 'id': '78693464',
42 'ext': 'mp4',
43 'title': '【爱范品】第31期:MWC见不到的奇葩手机',
44 }
45 }, {
46 'note': 'Multipart video',
47 'url': 'http://my.tv.sohu.com/pl/8384802/78910339.shtml',
48 'info_dict': {
49 'id': '78910339',
50 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
51 },
52 'playlist': [{
53 'md5': '492923eac023ba2f13ff69617c32754a',
54 'info_dict': {
55 'id': '78910339_part1',
56 'ext': 'mp4',
57 'duration': 294,
58 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
59 }
60 }, {
61 'md5': 'de604848c0e8e9c4a4dde7e1347c0637',
62 'info_dict': {
63 'id': '78910339_part2',
64 'ext': 'mp4',
65 'duration': 300,
66 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
67 }
68 }, {
69 'md5': '93584716ee0657c0b205b8aa3d27aa13',
70 'info_dict': {
71 'id': '78910339_part3',
72 'ext': 'mp4',
73 'duration': 150,
74 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
75 }
76 }]
77 }, {
78 'note': 'Video with title containing dash',
79 'url': 'http://my.tv.sohu.com/us/249884221/78932792.shtml',
80 'info_dict': {
81 'id': '78932792',
82 'ext': 'mp4',
83 'title': 'youtube-dl testing video',
84 },
85 'params': {
86 'skip_download': True
87 }
88 }]
89
90 def _real_extract(self, url):
91
92 def _fetch_data(vid_id, mytv=False):
93 if mytv:
94 base_data_url = 'http://my.tv.sohu.com/play/videonew.do?vid='
95 else:
96 base_data_url = 'http://hot.vrs.sohu.com/vrs_flash.action?vid='
97
98 req = compat_urllib_request.Request(base_data_url + vid_id)
99
100 cn_verification_proxy = self._downloader.params.get('cn_verification_proxy')
101 if cn_verification_proxy:
102 req.add_header('Ytdl-request-proxy', cn_verification_proxy)
103
104 return self._download_json(
105 req, video_id,
106 'Downloading JSON data for %s' % vid_id)
107
108 mobj = re.match(self._VALID_URL, url)
109 video_id = mobj.group('id')
110 mytv = mobj.group('mytv') is not None
111
112 webpage = self._download_webpage(url, video_id)
113
114 title = re.sub(r' - 搜狐视频$', '', self._og_search_title(webpage))
115
116 vid = self._html_search_regex(
117 r'var vid ?= ?["\'](\d+)["\']',
118 webpage, 'video path')
119 vid_data = _fetch_data(vid, mytv)
120 if vid_data['play'] != 1:
121 if vid_data.get('status') == 12:
122 raise ExtractorError(
123 'Sohu said: There\'s something wrong in the video.',
124 expected=True)
125 else:
126 raise ExtractorError(
127 'Sohu said: The video is only licensed to users in Mainland China.',
128 expected=True)
129
130 formats_json = {}
131 for format_id in ('nor', 'high', 'super', 'ori', 'h2644k', 'h2654k'):
132 vid_id = vid_data['data'].get('%sVid' % format_id)
133 if not vid_id:
134 continue
135 vid_id = compat_str(vid_id)
136 formats_json[format_id] = vid_data if vid == vid_id else _fetch_data(vid_id, mytv)
137
138 part_count = vid_data['data']['totalBlocks']
139
140 playlist = []
141 for i in range(part_count):
142 formats = []
143 for format_id, format_data in formats_json.items():
144 data = format_data['data']
145
146 # URLs starts with http://newflv.sohu.ccgslb.net/ is not usable
147 # so retry until got a working URL
148 video_url = 'newflv.sohu.ccgslb.net'
149 retries = 0
150 while 'newflv.sohu.ccgslb.net' in video_url and retries < 5:
151 download_note = 'Download information from CDN gateway for format ' + format_id
152 if retries > 0:
153 download_note += ' (retry #%d)' % retries
154 retries += 1
155 cdn_info = self._download_json(
156 'http://data.vod.itc.cn/cdnList?new=' + data['su'][i],
157 video_id, download_note)
158 video_url = cdn_info['url']
159
160 formats.append({
161 'url': video_url,
162 'format_id': format_id,
163 'filesize': data['clipsBytes'][i],
164 'width': data['width'],
165 'height': data['height'],
166 'fps': data['fps'],
167 })
168 self._sort_formats(formats)
169
170 playlist.append({
171 'id': '%s_part%d' % (video_id, i + 1),
172 'title': title,
173 'duration': vid_data['data']['clipsDuration'][i],
174 'formats': formats,
175 })
176
177 if len(playlist) == 1:
178 info = playlist[0]
179 info['id'] = video_id
180 else:
181 info = {
182 '_type': 'multi_video',
183 'entries': playlist,
184 'id': video_id,
185 'title': title,
186 }
187
188 return info