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