]>
Commit | Line | Data |
---|---|---|
6624a2b0 | 1 | # encoding: utf-8 |
4c6d2ff8 | 2 | from __future__ import unicode_literals |
6624a2b0 | 3 | |
6624a2b0 | 4 | import json |
f143d86a | 5 | import re |
6624a2b0 | 6 | |
7 | from .common import InfoExtractor | |
f143d86a | 8 | from ..utils import ExtractorError |
6624a2b0 | 9 | |
10 | ||
11 | class SohuIE(InfoExtractor): | |
6d2d21f7 | 12 | _VALID_URL = r'https?://(?P<mytv>my\.)?tv\.sohu\.com/.+?/(?(mytv)|n)(?P<id>\d+)\.shtml.*?' |
6624a2b0 | 13 | |
14 | _TEST = { | |
4c6d2ff8 PH |
15 | 'url': 'http://tv.sohu.com/20130724/n382479172.shtml#super', |
16 | 'md5': 'bde8d9a6ffd82c63a1eefaef4eeefec7', | |
17 | 'info_dict': { | |
18 | 'id': '382479172', | |
19 | 'ext': 'mp4', | |
20 | 'title': 'MV:Far East Movement《The Illest》', | |
6624a2b0 | 21 | }, |
4c6d2ff8 | 22 | 'skip': 'Only available from China', |
6624a2b0 | 23 | } |
24 | ||
6624a2b0 | 25 | def _real_extract(self, url): |
f143d86a | 26 | |
6d2d21f7 JMF |
27 | def _fetch_data(vid_id, mytv=False): |
28 | if mytv: | |
29 | base_data_url = 'http://my.tv.sohu.com/play/videonew.do?vid=' | |
30 | else: | |
4c6d2ff8 | 31 | base_data_url = 'http://hot.vrs.sohu.com/vrs_flash.action?vid=' |
f143d86a PH |
32 | data_url = base_data_url + str(vid_id) |
33 | data_json = self._download_webpage( | |
34 | data_url, video_id, | |
8865bdeb | 35 | note='Downloading JSON data for ' + str(vid_id)) |
f143d86a PH |
36 | return json.loads(data_json) |
37 | ||
6624a2b0 | 38 | mobj = re.match(self._VALID_URL, url) |
39 | video_id = mobj.group('id') | |
6d2d21f7 | 40 | mytv = mobj.group('mytv') is not None |
f143d86a | 41 | |
6624a2b0 | 42 | webpage = self._download_webpage(url, video_id) |
f143d86a | 43 | raw_title = self._html_search_regex(r'(?s)<title>(.+?)</title>', |
4c6d2ff8 | 44 | webpage, 'video title') |
f143d86a | 45 | title = raw_title.partition('-')[0].strip() |
6624a2b0 | 46 | |
6d2d21f7 | 47 | vid = self._html_search_regex(r'var vid ?= ?["\'](\d+)["\']', webpage, |
4c6d2ff8 | 48 | 'video path') |
6d2d21f7 | 49 | data = _fetch_data(vid, mytv) |
f143d86a PH |
50 | |
51 | QUALITIES = ('ori', 'super', 'high', 'nor') | |
52 | vid_ids = [data['data'][q + 'Vid'] | |
53 | for q in QUALITIES | |
54 | if data['data'][q + 'Vid'] != 0] | |
55 | if not vid_ids: | |
8865bdeb | 56 | raise ExtractorError('No formats available for this video') |
f143d86a PH |
57 | |
58 | # For now, we just pick the highest available quality | |
59 | vid_id = vid_ids[-1] | |
60 | ||
6d2d21f7 | 61 | format_data = data if vid == vid_id else _fetch_data(vid_id, mytv) |
f143d86a PH |
62 | part_count = format_data['data']['totalBlocks'] |
63 | allot = format_data['allot'] | |
64 | prot = format_data['prot'] | |
65 | clipsURL = format_data['data']['clipsURL'] | |
66 | su = format_data['data']['su'] | |
67 | ||
68 | playlist = [] | |
69 | for i in range(part_count): | |
70 | part_url = ('http://%s/?prot=%s&file=%s&new=%s' % | |
71 | (allot, prot, clipsURL[i], su[i])) | |
72 | part_str = self._download_webpage( | |
73 | part_url, video_id, | |
8865bdeb | 74 | note='Downloading part %d of %d' % (i + 1, part_count)) |
f143d86a PH |
75 | |
76 | part_info = part_str.split('|') | |
77 | video_url = '%s%s?key=%s' % (part_info[0], su[i], part_info[3]) | |
78 | ||
79 | video_info = { | |
6624a2b0 | 80 | 'id': '%s_part%02d' % (video_id, i + 1), |
81 | 'title': title, | |
f143d86a | 82 | 'url': video_url, |
6624a2b0 | 83 | 'ext': 'mp4', |
84 | } | |
f143d86a PH |
85 | playlist.append(video_info) |
86 | ||
87 | if len(playlist) == 1: | |
88 | info = playlist[0] | |
4ec929dc | 89 | info['id'] = video_id |
f143d86a PH |
90 | else: |
91 | info = { | |
92 | '_type': 'playlist', | |
93 | 'entries': playlist, | |
94 | 'id': video_id, | |
95 | } | |
96 | ||
97 | return info |