[Sohu] Add a multiplart video test case
[youtube-dl] / 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 url_sanitize_consecutive_slashes
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': '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         }
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         }]
76     }]
77
78     def _real_extract(self, url):
79
80         def _fetch_data(vid_id, mytv=False):
81             if mytv:
82                 base_data_url = 'http://my.tv.sohu.com/play/videonew.do?vid='
83             else:
84                 base_data_url = 'http://hot.vrs.sohu.com/vrs_flash.action?vid='
85
86             req = compat_urllib_request.Request(base_data_url + vid_id)
87
88             cn_verification_proxy = self._downloader.params.get('cn_verification_proxy')
89             if cn_verification_proxy:
90                 req.add_header('Ytdl-request-proxy', cn_verification_proxy)
91
92             return self._download_json(req, video_id,
93                                        'Downloading JSON data for %s' % vid_id)
94
95         mobj = re.match(self._VALID_URL, url)
96         video_id = mobj.group('id')
97         mytv = mobj.group('mytv') is not None
98
99         webpage = self._download_webpage(url, video_id)
100         raw_title = self._html_search_regex(
101             r'(?s)<title>(.+?)</title>',
102             webpage, 'video title')
103         title = raw_title.partition('-')[0].strip()
104
105         vid = self._html_search_regex(
106             r'var vid ?= ?["\'](\d+)["\']',
107             webpage, 'video path')
108         vid_data = _fetch_data(vid, mytv)
109
110         formats_json = {}
111         for format_id in ('nor', 'high', 'super', 'ori', 'h2644k', 'h2654k'):
112             vid_id = vid_data['data'].get('%sVid' % format_id)
113             if not vid_id:
114                 continue
115             vid_id = compat_str(vid_id)
116             formats_json[format_id] = vid_data if vid == vid_id else _fetch_data(vid_id, mytv)
117
118         part_count = vid_data['data']['totalBlocks']
119
120         playlist = []
121         for i in range(part_count):
122             formats = []
123             for format_id, format_data in formats_json.items():
124                 allot = format_data['allot']
125                 prot = format_data['prot']
126
127                 data = format_data['data']
128                 clips_url = data['clipsURL']
129                 su = data['su']
130
131                 part_str = self._download_webpage(
132                     'http://%s/?prot=%s&file=%s&new=%s' %
133                     (allot, prot, clips_url[i], su[i]),
134                     video_id,
135                     'Downloading %s video URL part %d of %d'
136                     % (format_id, i + 1, part_count))
137
138                 part_info = part_str.split('|')
139
140                 video_url = url_sanitize_consecutive_slashes(
141                     '%s%s?key=%s' % (part_info[0], su[i], part_info[3]))
142
143                 formats.append({
144                     'url': video_url,
145                     'format_id': format_id,
146                     'filesize': data['clipsBytes'][i],
147                     'width': data['width'],
148                     'height': data['height'],
149                     'fps': data['fps'],
150                 })
151             self._sort_formats(formats)
152
153             playlist.append({
154                 'id': '%s_part%d' % (video_id, i + 1),
155                 'title': title,
156                 'duration': vid_data['data']['clipsDuration'][i],
157                 'formats': formats,
158             })
159
160         if len(playlist) == 1:
161             info = playlist[0]
162             info['id'] = video_id
163         else:
164             info = {
165                 '_type': 'playlist',
166                 'entries': playlist,
167                 'id': video_id,
168             }
169
170         return info