]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/sohu.py
Prepare for upload.
[youtubedl] / youtube_dl / extractor / sohu.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5 import re
6
7 from .common import InfoExtractor
8 from ..utils import ExtractorError
9
10
11 class SohuIE(InfoExtractor):
12 _VALID_URL = r'https?://(?P<mytv>my\.)?tv\.sohu\.com/.+?/(?(mytv)|n)(?P<id>\d+)\.shtml.*?'
13
14 _TEST = {
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》',
21 },
22 'skip': 'Only available from China',
23 }
24
25 def _real_extract(self, url):
26
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:
31 base_data_url = 'http://hot.vrs.sohu.com/vrs_flash.action?vid='
32 data_url = base_data_url + str(vid_id)
33 data_json = self._download_webpage(
34 data_url, video_id,
35 note='Downloading JSON data for ' + str(vid_id))
36 return json.loads(data_json)
37
38 mobj = re.match(self._VALID_URL, url)
39 video_id = mobj.group('id')
40 mytv = mobj.group('mytv') is not None
41
42 webpage = self._download_webpage(url, video_id)
43 raw_title = self._html_search_regex(r'(?s)<title>(.+?)</title>',
44 webpage, 'video title')
45 title = raw_title.partition('-')[0].strip()
46
47 vid = self._html_search_regex(r'var vid ?= ?["\'](\d+)["\']', webpage,
48 'video path')
49 data = _fetch_data(vid, mytv)
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:
56 raise ExtractorError('No formats available for this video')
57
58 # For now, we just pick the highest available quality
59 vid_id = vid_ids[-1]
60
61 format_data = data if vid == vid_id else _fetch_data(vid_id, mytv)
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,
74 note='Downloading part %d of %d' % (i + 1, part_count))
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 = {
80 'id': '%s_part%02d' % (video_id, i + 1),
81 'title': title,
82 'url': video_url,
83 'ext': 'mp4',
84 }
85 playlist.append(video_info)
86
87 if len(playlist) == 1:
88 info = playlist[0]
89 info['id'] = video_id
90 else:
91 info = {
92 '_type': 'playlist',
93 'entries': playlist,
94 'id': video_id,
95 }
96
97 return info