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