]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/sohu.py
[utils] Place sanitize url function near other sanitizing functions
[yt-dlp.git] / youtube_dl / extractor / sohu.py
CommitLineData
6624a2b0 1# encoding: 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,
9 compat_urllib_request
10)
55969016 11from ..utils import url_sanitize_consecutive_slashes
6624a2b0 12
13
14class SohuIE(InfoExtractor):
6d2d21f7 15 _VALID_URL = r'https?://(?P<mytv>my\.)?tv\.sohu\.com/.+?/(?(mytv)|n)(?P<id>\d+)\.shtml.*?'
6624a2b0 16
5ee6fc97
YCH
17 _TESTS = [{
18 'note': 'This video is available only in Mainland China',
4c6d2ff8 19 'url': 'http://tv.sohu.com/20130724/n382479172.shtml#super',
5ee6fc97 20 'md5': '29175c8cadd8b5cc4055001e85d6b372',
4c6d2ff8
PH
21 'info_dict': {
22 'id': '382479172',
23 'ext': 'mp4',
24 'title': 'MV:Far East Movement《The Illest》',
6624a2b0 25 },
5ee6fc97
YCH
26 'params': {
27 'cn_verification_proxy': 'proxy.uku.im:8888'
28 }
29 }, {
30 'url': 'http://tv.sohu.com/20150305/n409385080.shtml',
31 'md5': '699060e75cf58858dd47fb9c03c42cfb',
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': '9bf34be48f2f4dadcb226c74127e203c',
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',
50 },
51 'playlist': [{
52 'md5': 'bdbfb8f39924725e6589c146bc1883ad',
53 'info_dict': {
54 'id': '78910339_part1',
55 'ext': 'mp4',
56 'duration': 294,
57 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
58 }
59 }, {
60 'md5': '3e1f46aaeb95354fd10e7fca9fc1804e',
61 'info_dict': {
62 'id': '78910339_part2',
63 'ext': 'mp4',
64 'duration': 300,
65 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
66 }
67 }, {
68 'md5': '8407e634175fdac706766481b9443450',
69 'info_dict': {
70 'id': '78910339_part3',
71 'ext': 'mp4',
72 'duration': 150,
73 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
74 }
75 }]
2cb434e5
YCH
76 }, {
77 'info': 'Video with title containing dash',
78 'url': 'http://my.tv.sohu.com/us/249884221/78932792.shtml',
79 'info_dict': {
80 'id': '78932792',
81 'ext': 'mp4',
82 'title': 'youtube-dl testing video',
83 },
84 'params': {
85 'skip_download': True
86 }
5ee6fc97 87 }]
6624a2b0 88
6624a2b0 89 def _real_extract(self, url):
f143d86a 90
6d2d21f7
JMF
91 def _fetch_data(vid_id, mytv=False):
92 if mytv:
93 base_data_url = 'http://my.tv.sohu.com/play/videonew.do?vid='
94 else:
4c6d2ff8 95 base_data_url = 'http://hot.vrs.sohu.com/vrs_flash.action?vid='
5ac71f0b 96
5ee6fc97
YCH
97 req = compat_urllib_request.Request(base_data_url + vid_id)
98
99 cn_verification_proxy = self._downloader.params.get('cn_verification_proxy')
100 if cn_verification_proxy:
101 req.add_header('Ytdl-request-proxy', cn_verification_proxy)
102
103 return self._download_json(req, video_id,
104 'Downloading JSON data for %s' % vid_id)
f143d86a 105
6624a2b0 106 mobj = re.match(self._VALID_URL, url)
107 video_id = mobj.group('id')
6d2d21f7 108 mytv = mobj.group('mytv') is not None
f143d86a 109
6624a2b0 110 webpage = self._download_webpage(url, video_id)
2cb434e5
YCH
111
112 title = self._og_search_title(webpage)
6624a2b0 113
5ac71f0b
S
114 vid = self._html_search_regex(
115 r'var vid ?= ?["\'](\d+)["\']',
116 webpage, 'video path')
117 vid_data = _fetch_data(vid, mytv)
f143d86a 118
5ac71f0b
S
119 formats_json = {}
120 for format_id in ('nor', 'high', 'super', 'ori', 'h2644k', 'h2654k'):
121 vid_id = vid_data['data'].get('%sVid' % format_id)
122 if not vid_id:
123 continue
124 vid_id = compat_str(vid_id)
125 formats_json[format_id] = vid_data if vid == vid_id else _fetch_data(vid_id, mytv)
f143d86a 126
5ac71f0b 127 part_count = vid_data['data']['totalBlocks']
f143d86a
PH
128
129 playlist = []
130 for i in range(part_count):
5ac71f0b
S
131 formats = []
132 for format_id, format_data in formats_json.items():
133 allot = format_data['allot']
134 prot = format_data['prot']
135
136 data = format_data['data']
137 clips_url = data['clipsURL']
138 su = data['su']
139
140 part_str = self._download_webpage(
141 'http://%s/?prot=%s&file=%s&new=%s' %
142 (allot, prot, clips_url[i], su[i]),
143 video_id,
144 'Downloading %s video URL part %d of %d'
145 % (format_id, i + 1, part_count))
146
147 part_info = part_str.split('|')
5ee6fc97 148
55969016
YCH
149 video_url = url_sanitize_consecutive_slashes(
150 '%s%s?key=%s' % (part_info[0], su[i], part_info[3]))
5ac71f0b
S
151
152 formats.append({
153 'url': video_url,
154 'format_id': format_id,
155 'filesize': data['clipsBytes'][i],
156 'width': data['width'],
157 'height': data['height'],
158 'fps': data['fps'],
159 })
160 self._sort_formats(formats)
161
162 playlist.append({
163 'id': '%s_part%d' % (video_id, i + 1),
6624a2b0 164 'title': title,
5ac71f0b
S
165 'duration': vid_data['data']['clipsDuration'][i],
166 'formats': formats,
167 })
f143d86a
PH
168
169 if len(playlist) == 1:
170 info = playlist[0]
4ec929dc 171 info['id'] = video_id
f143d86a
PH
172 else:
173 info = {
174 '_type': 'playlist',
175 'entries': playlist,
176 'id': video_id,
177 }
178
179 return info