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.

186 lines
6.5 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 ExtractorError
  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. 'skip': 'On available in China',
  22. }, {
  23. 'url': 'http://tv.sohu.com/20150305/n409385080.shtml',
  24. 'md5': 'ac9a5d322b4bf9ae184d53e4711e4f1a',
  25. 'info_dict': {
  26. 'id': '409385080',
  27. 'ext': 'mp4',
  28. 'title': '《2015湖南卫视羊年元宵晚会》唐嫣《花好月圆》',
  29. }
  30. }, {
  31. 'url': 'http://my.tv.sohu.com/us/232799889/78693464.shtml',
  32. 'md5': '49308ff6dafde5ece51137d04aec311e',
  33. 'info_dict': {
  34. 'id': '78693464',
  35. 'ext': 'mp4',
  36. 'title': '【爱范品】第31期:MWC见不到的奇葩手机',
  37. }
  38. }, {
  39. 'note': 'Multipart video',
  40. 'url': 'http://my.tv.sohu.com/pl/8384802/78910339.shtml',
  41. 'info_dict': {
  42. 'id': '78910339',
  43. 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
  44. },
  45. 'playlist': [{
  46. 'md5': '492923eac023ba2f13ff69617c32754a',
  47. 'info_dict': {
  48. 'id': '78910339_part1',
  49. 'ext': 'mp4',
  50. 'duration': 294,
  51. 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
  52. }
  53. }, {
  54. 'md5': 'de604848c0e8e9c4a4dde7e1347c0637',
  55. 'info_dict': {
  56. 'id': '78910339_part2',
  57. 'ext': 'mp4',
  58. 'duration': 300,
  59. 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
  60. }
  61. }, {
  62. 'md5': '93584716ee0657c0b205b8aa3d27aa13',
  63. 'info_dict': {
  64. 'id': '78910339_part3',
  65. 'ext': 'mp4',
  66. 'duration': 150,
  67. 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
  68. }
  69. }]
  70. }, {
  71. 'note': 'Video with title containing dash',
  72. 'url': 'http://my.tv.sohu.com/us/249884221/78932792.shtml',
  73. 'info_dict': {
  74. 'id': '78932792',
  75. 'ext': 'mp4',
  76. 'title': 'youtube-dl testing video',
  77. },
  78. 'params': {
  79. 'skip_download': True
  80. }
  81. }]
  82. def _real_extract(self, url):
  83. def _fetch_data(vid_id, mytv=False):
  84. if mytv:
  85. base_data_url = 'http://my.tv.sohu.com/play/videonew.do?vid='
  86. else:
  87. base_data_url = 'http://hot.vrs.sohu.com/vrs_flash.action?vid='
  88. req = compat_urllib_request.Request(base_data_url + vid_id)
  89. cn_verification_proxy = self._downloader.params.get('cn_verification_proxy')
  90. if cn_verification_proxy:
  91. req.add_header('Ytdl-request-proxy', cn_verification_proxy)
  92. return self._download_json(
  93. req, video_id,
  94. 'Downloading JSON data for %s' % vid_id)
  95. mobj = re.match(self._VALID_URL, url)
  96. video_id = mobj.group('id')
  97. mytv = mobj.group('mytv') is not None
  98. webpage = self._download_webpage(url, video_id)
  99. title = re.sub(r' - 搜狐视频$', '', self._og_search_title(webpage))
  100. vid = self._html_search_regex(
  101. r'var vid ?= ?["\'](\d+)["\']',
  102. webpage, 'video path')
  103. vid_data = _fetch_data(vid, mytv)
  104. if vid_data['play'] != 1:
  105. if vid_data.get('status') == 12:
  106. raise ExtractorError(
  107. 'Sohu said: There\'s something wrong in the video.',
  108. expected=True)
  109. else:
  110. raise ExtractorError(
  111. 'Sohu said: The video is only licensed to users in Mainland China.',
  112. expected=True)
  113. formats_json = {}
  114. for format_id in ('nor', 'high', 'super', 'ori', 'h2644k', 'h2654k'):
  115. vid_id = vid_data['data'].get('%sVid' % format_id)
  116. if not vid_id:
  117. continue
  118. vid_id = compat_str(vid_id)
  119. formats_json[format_id] = vid_data if vid == vid_id else _fetch_data(vid_id, mytv)
  120. part_count = vid_data['data']['totalBlocks']
  121. playlist = []
  122. for i in range(part_count):
  123. formats = []
  124. for format_id, format_data in formats_json.items():
  125. data = format_data['data']
  126. # URLs starts with http://newflv.sohu.ccgslb.net/ is not usable
  127. # so retry until got a working URL
  128. video_url = 'newflv.sohu.ccgslb.net'
  129. retries = 0
  130. while 'newflv.sohu.ccgslb.net' in video_url and retries < 5:
  131. download_note = 'Download information from CDN gateway for format ' + format_id
  132. if retries > 0:
  133. download_note += ' (retry #%d)' % retries
  134. retries += 1
  135. cdn_info = self._download_json(
  136. 'http://data.vod.itc.cn/cdnList?new=' + data['su'][i],
  137. video_id, download_note)
  138. video_url = cdn_info['url']
  139. formats.append({
  140. 'url': video_url,
  141. 'format_id': format_id,
  142. 'filesize': data['clipsBytes'][i],
  143. 'width': data['width'],
  144. 'height': data['height'],
  145. 'fps': data['fps'],
  146. })
  147. self._sort_formats(formats)
  148. playlist.append({
  149. 'id': '%s_part%d' % (video_id, i + 1),
  150. 'title': title,
  151. 'duration': vid_data['data']['clipsDuration'][i],
  152. 'formats': formats,
  153. })
  154. if len(playlist) == 1:
  155. info = playlist[0]
  156. info['id'] = video_id
  157. else:
  158. info = {
  159. '_type': 'multi_video',
  160. 'entries': playlist,
  161. 'id': video_id,
  162. 'title': title,
  163. }
  164. return info