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.

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