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.

203 lines
6.9 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_parse_urlencode,
  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': '699060e75cf58858dd47fb9c03c42cfb',
  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': '9bf34be48f2f4dadcb226c74127e203c',
  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': 'bdbfb8f39924725e6589c146bc1883ad',
  47. 'info_dict': {
  48. 'id': '78910339_part1',
  49. 'ext': 'mp4',
  50. 'duration': 294,
  51. 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
  52. }
  53. }, {
  54. 'md5': '3e1f46aaeb95354fd10e7fca9fc1804e',
  55. 'info_dict': {
  56. 'id': '78910339_part2',
  57. 'ext': 'mp4',
  58. 'duration': 300,
  59. 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
  60. }
  61. }, {
  62. 'md5': '8407e634175fdac706766481b9443450',
  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. return self._download_json(
  89. base_data_url + vid_id, video_id,
  90. 'Downloading JSON data for %s' % vid_id,
  91. headers=self.geo_verification_headers())
  92. mobj = re.match(self._VALID_URL, url)
  93. video_id = mobj.group('id')
  94. mytv = mobj.group('mytv') is not None
  95. webpage = self._download_webpage(url, video_id)
  96. title = re.sub(r' - 搜狐视频$', '', self._og_search_title(webpage))
  97. vid = self._html_search_regex(
  98. r'var vid ?= ?["\'](\d+)["\']',
  99. webpage, 'video path')
  100. vid_data = _fetch_data(vid, mytv)
  101. if vid_data['play'] != 1:
  102. if vid_data.get('status') == 12:
  103. raise ExtractorError(
  104. 'Sohu said: There\'s something wrong in the video.',
  105. expected=True)
  106. else:
  107. raise ExtractorError(
  108. 'Sohu said: The video is only licensed to users in Mainland China.',
  109. expected=True)
  110. formats_json = {}
  111. for format_id in ('nor', 'high', 'super', 'ori', 'h2644k', 'h2654k'):
  112. vid_id = vid_data['data'].get('%sVid' % format_id)
  113. if not vid_id:
  114. continue
  115. vid_id = compat_str(vid_id)
  116. formats_json[format_id] = vid_data if vid == vid_id else _fetch_data(vid_id, mytv)
  117. part_count = vid_data['data']['totalBlocks']
  118. playlist = []
  119. for i in range(part_count):
  120. formats = []
  121. for format_id, format_data in formats_json.items():
  122. allot = format_data['allot']
  123. data = format_data['data']
  124. clips_url = data['clipsURL']
  125. su = data['su']
  126. video_url = 'newflv.sohu.ccgslb.net'
  127. cdnId = None
  128. retries = 0
  129. while 'newflv.sohu.ccgslb.net' in video_url:
  130. params = {
  131. 'prot': 9,
  132. 'file': clips_url[i],
  133. 'new': su[i],
  134. 'prod': 'flash',
  135. 'rb': 1,
  136. }
  137. if cdnId is not None:
  138. params['idc'] = cdnId
  139. download_note = 'Downloading %s video URL part %d of %d' % (
  140. format_id, i + 1, part_count)
  141. if retries > 0:
  142. download_note += ' (retry #%d)' % retries
  143. part_info = self._parse_json(self._download_webpage(
  144. 'http://%s/?%s' % (allot, compat_urllib_parse_urlencode(params)),
  145. video_id, download_note), video_id)
  146. video_url = part_info['url']
  147. cdnId = part_info.get('nid')
  148. retries += 1
  149. if retries > 5:
  150. raise ExtractorError('Failed to get video URL')
  151. formats.append({
  152. 'url': video_url,
  153. 'format_id': format_id,
  154. 'filesize': data['clipsBytes'][i],
  155. 'width': data['width'],
  156. 'height': data['height'],
  157. 'fps': data['fps'],
  158. })
  159. self._sort_formats(formats)
  160. playlist.append({
  161. 'id': '%s_part%d' % (video_id, i + 1),
  162. 'title': title,
  163. 'duration': vid_data['data']['clipsDuration'][i],
  164. 'formats': formats,
  165. })
  166. if len(playlist) == 1:
  167. info = playlist[0]
  168. info['id'] = video_id
  169. else:
  170. info = {
  171. '_type': 'multi_video',
  172. 'entries': playlist,
  173. 'id': video_id,
  174. 'title': title,
  175. }
  176. return info