You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
6.1 KiB

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