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.

188 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. 'params': {
  22. 'cn_verification_proxy': 'proxy.uku.im:8888'
  23. }
  24. }, {
  25. 'url': 'http://tv.sohu.com/20150305/n409385080.shtml',
  26. 'md5': 'ac9a5d322b4bf9ae184d53e4711e4f1a',
  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': '49308ff6dafde5ece51137d04aec311e',
  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': '492923eac023ba2f13ff69617c32754a',
  49. 'info_dict': {
  50. 'id': '78910339_part1',
  51. 'ext': 'mp4',
  52. 'duration': 294,
  53. 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
  54. }
  55. }, {
  56. 'md5': 'de604848c0e8e9c4a4dde7e1347c0637',
  57. 'info_dict': {
  58. 'id': '78910339_part2',
  59. 'ext': 'mp4',
  60. 'duration': 300,
  61. 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
  62. }
  63. }, {
  64. 'md5': '93584716ee0657c0b205b8aa3d27aa13',
  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. if vid_data['play'] != 1:
  107. if vid_data.get('status') == 12:
  108. raise ExtractorError(
  109. 'Sohu said: There\'s something wrong in the video.',
  110. expected=True)
  111. else:
  112. raise ExtractorError(
  113. 'Sohu said: The video is only licensed to users in Mainland China.',
  114. expected=True)
  115. formats_json = {}
  116. for format_id in ('nor', 'high', 'super', 'ori', 'h2644k', 'h2654k'):
  117. vid_id = vid_data['data'].get('%sVid' % format_id)
  118. if not vid_id:
  119. continue
  120. vid_id = compat_str(vid_id)
  121. formats_json[format_id] = vid_data if vid == vid_id else _fetch_data(vid_id, mytv)
  122. part_count = vid_data['data']['totalBlocks']
  123. playlist = []
  124. for i in range(part_count):
  125. formats = []
  126. for format_id, format_data in formats_json.items():
  127. data = format_data['data']
  128. # URLs starts with http://newflv.sohu.ccgslb.net/ is not usable
  129. # so retry until got a working URL
  130. video_url = 'newflv.sohu.ccgslb.net'
  131. retries = 0
  132. while 'newflv.sohu.ccgslb.net' in video_url and retries < 5:
  133. download_note = 'Download information from CDN gateway for format ' + format_id
  134. if retries > 0:
  135. download_note += ' (retry #%d)' % retries
  136. retries += 1
  137. cdn_info = self._download_json(
  138. 'http://data.vod.itc.cn/cdnList?new=' + data['su'][i],
  139. video_id, download_note)
  140. video_url = cdn_info['url']
  141. formats.append({
  142. 'url': video_url,
  143. 'format_id': format_id,
  144. 'filesize': data['clipsBytes'][i],
  145. 'width': data['width'],
  146. 'height': data['height'],
  147. 'fps': data['fps'],
  148. })
  149. self._sort_formats(formats)
  150. playlist.append({
  151. 'id': '%s_part%d' % (video_id, i + 1),
  152. 'title': title,
  153. 'duration': vid_data['data']['clipsDuration'][i],
  154. 'formats': formats,
  155. })
  156. if len(playlist) == 1:
  157. info = playlist[0]
  158. info['id'] = video_id
  159. else:
  160. info = {
  161. '_type': 'multi_video',
  162. 'entries': playlist,
  163. 'id': video_id,
  164. 'title': title,
  165. }
  166. return info