sohu.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_str,
  7. compat_urllib_request
  8. )
  9. from ..utils import (
  10. sanitize_url_path_consecutive_slashes,
  11. ExtractorError,
  12. )
  13. class SohuIE(InfoExtractor):
  14. _VALID_URL = r'https?://(?P<mytv>my\.)?tv\.sohu\.com/.+?/(?(mytv)|n)(?P<id>\d+)\.shtml.*?'
  15. _TESTS = [{
  16. 'note': 'This video is available only in Mainland China',
  17. 'url': 'http://tv.sohu.com/20130724/n382479172.shtml#super',
  18. 'md5': '29175c8cadd8b5cc4055001e85d6b372',
  19. 'info_dict': {
  20. 'id': '382479172',
  21. 'ext': 'mp4',
  22. 'title': 'MV:Far East Movement《The Illest》',
  23. },
  24. 'params': {
  25. 'cn_verification_proxy': 'proxy.uku.im:8888'
  26. }
  27. }, {
  28. 'url': 'http://tv.sohu.com/20150305/n409385080.shtml',
  29. 'md5': '699060e75cf58858dd47fb9c03c42cfb',
  30. 'info_dict': {
  31. 'id': '409385080',
  32. 'ext': 'mp4',
  33. 'title': '《2015湖南卫视羊年元宵晚会》唐嫣《花好月圆》',
  34. }
  35. }, {
  36. 'url': 'http://my.tv.sohu.com/us/232799889/78693464.shtml',
  37. 'md5': '9bf34be48f2f4dadcb226c74127e203c',
  38. 'info_dict': {
  39. 'id': '78693464',
  40. 'ext': 'mp4',
  41. 'title': '【爱范品】第31期:MWC见不到的奇葩手机',
  42. }
  43. }, {
  44. 'note': 'Multipart video',
  45. 'url': 'http://my.tv.sohu.com/pl/8384802/78910339.shtml',
  46. 'info_dict': {
  47. 'id': '78910339',
  48. 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
  49. },
  50. 'playlist': [{
  51. 'md5': 'bdbfb8f39924725e6589c146bc1883ad',
  52. 'info_dict': {
  53. 'id': '78910339_part1',
  54. 'ext': 'mp4',
  55. 'duration': 294,
  56. 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
  57. }
  58. }, {
  59. 'md5': '3e1f46aaeb95354fd10e7fca9fc1804e',
  60. 'info_dict': {
  61. 'id': '78910339_part2',
  62. 'ext': 'mp4',
  63. 'duration': 300,
  64. 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
  65. }
  66. }, {
  67. 'md5': '8407e634175fdac706766481b9443450',
  68. 'info_dict': {
  69. 'id': '78910339_part3',
  70. 'ext': 'mp4',
  71. 'duration': 150,
  72. 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
  73. }
  74. }]
  75. }, {
  76. 'note': 'Video with title containing dash',
  77. 'url': 'http://my.tv.sohu.com/us/249884221/78932792.shtml',
  78. 'info_dict': {
  79. 'id': '78932792',
  80. 'ext': 'mp4',
  81. 'title': 'youtube-dl testing video',
  82. },
  83. 'params': {
  84. 'skip_download': True
  85. }
  86. }]
  87. def _real_extract(self, url):
  88. def _fetch_data(vid_id, mytv=False):
  89. if mytv:
  90. base_data_url = 'http://my.tv.sohu.com/play/videonew.do?vid='
  91. else:
  92. base_data_url = 'http://hot.vrs.sohu.com/vrs_flash.action?vid='
  93. req = compat_urllib_request.Request(base_data_url + vid_id)
  94. cn_verification_proxy = self._downloader.params.get('cn_verification_proxy')
  95. if cn_verification_proxy:
  96. req.add_header('Ytdl-request-proxy', cn_verification_proxy)
  97. return self._download_json(
  98. req, video_id,
  99. 'Downloading JSON data for %s' % vid_id)
  100. mobj = re.match(self._VALID_URL, url)
  101. video_id = mobj.group('id')
  102. mytv = mobj.group('mytv') is not None
  103. webpage = self._download_webpage(url, video_id)
  104. title = re.sub(r' - 搜狐视频$', '', self._og_search_title(webpage))
  105. vid = self._html_search_regex(
  106. r'var vid ?= ?["\'](\d+)["\']',
  107. webpage, 'video path')
  108. vid_data = _fetch_data(vid, mytv)
  109. if vid_data['play'] != 1:
  110. if vid_data.get('status') == 12:
  111. raise ExtractorError(
  112. 'Sohu said: There\'s something wrong in the video.',
  113. expected=True)
  114. else:
  115. raise ExtractorError(
  116. 'Sohu said: The video is only licensed to users in Mainland China.',
  117. expected=True)
  118. formats_json = {}
  119. for format_id in ('nor', 'high', 'super', 'ori', 'h2644k', 'h2654k'):
  120. vid_id = vid_data['data'].get('%sVid' % format_id)
  121. if not vid_id:
  122. continue
  123. vid_id = compat_str(vid_id)
  124. formats_json[format_id] = vid_data if vid == vid_id else _fetch_data(vid_id, mytv)
  125. part_count = vid_data['data']['totalBlocks']
  126. playlist = []
  127. for i in range(part_count):
  128. formats = []
  129. for format_id, format_data in formats_json.items():
  130. allot = format_data['allot']
  131. prot = format_data['prot']
  132. data = format_data['data']
  133. clips_url = data['clipsURL']
  134. su = data['su']
  135. part_str = self._download_webpage(
  136. 'http://%s/?prot=%s&file=%s&new=%s' %
  137. (allot, prot, clips_url[i], su[i]),
  138. video_id,
  139. 'Downloading %s video URL part %d of %d'
  140. % (format_id, i + 1, part_count))
  141. part_info = part_str.split('|')
  142. video_url = sanitize_url_path_consecutive_slashes(
  143. '%s%s?key=%s' % (part_info[0], su[i], part_info[3]))
  144. formats.append({
  145. 'url': video_url,
  146. 'format_id': format_id,
  147. 'filesize': data['clipsBytes'][i],
  148. 'width': data['width'],
  149. 'height': data['height'],
  150. 'fps': data['fps'],
  151. })
  152. self._sort_formats(formats)
  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. if len(playlist) == 1:
  160. info = playlist[0]
  161. info['id'] = video_id
  162. else:
  163. info = {
  164. '_type': 'multi_video',
  165. 'entries': playlist,
  166. 'id': video_id,
  167. 'title': title,
  168. }
  169. return info