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.

198 lines
6.6 KiB

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