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.

197 lines
6.6 KiB

10 years ago
10 years ago
10 years ago
  1. # coding: 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. '%s said: There\'s something wrong in the video.' % self.IE_NAME,
  100. expected=True)
  101. else:
  102. self.raise_geo_restricted(
  103. '%s said: The video is only licensed to users in Mainland China.' % self.IE_NAME)
  104. formats_json = {}
  105. for format_id in ('nor', 'high', 'super', 'ori', 'h2644k', 'h2654k'):
  106. vid_id = vid_data['data'].get('%sVid' % format_id)
  107. if not vid_id:
  108. continue
  109. vid_id = compat_str(vid_id)
  110. formats_json[format_id] = vid_data if vid == vid_id else _fetch_data(vid_id, mytv)
  111. part_count = vid_data['data']['totalBlocks']
  112. playlist = []
  113. for i in range(part_count):
  114. formats = []
  115. for format_id, format_data in formats_json.items():
  116. allot = format_data['allot']
  117. data = format_data['data']
  118. clips_url = data['clipsURL']
  119. su = data['su']
  120. video_url = 'newflv.sohu.ccgslb.net'
  121. cdnId = None
  122. retries = 0
  123. while 'newflv.sohu.ccgslb.net' in video_url:
  124. params = {
  125. 'prot': 9,
  126. 'file': clips_url[i],
  127. 'new': su[i],
  128. 'prod': 'flash',
  129. 'rb': 1,
  130. }
  131. if cdnId is not None:
  132. params['idc'] = cdnId
  133. download_note = 'Downloading %s video URL part %d of %d' % (
  134. format_id, i + 1, part_count)
  135. if retries > 0:
  136. download_note += ' (retry #%d)' % retries
  137. part_info = self._parse_json(self._download_webpage(
  138. 'http://%s/?%s' % (allot, compat_urllib_parse_urlencode(params)),
  139. video_id, download_note), video_id)
  140. video_url = part_info['url']
  141. cdnId = part_info.get('nid')
  142. retries += 1
  143. if retries > 5:
  144. raise ExtractorError('Failed to get video URL')
  145. formats.append({
  146. 'url': video_url,
  147. 'format_id': format_id,
  148. 'filesize': data['clipsBytes'][i],
  149. 'width': data['width'],
  150. 'height': data['height'],
  151. 'fps': data['fps'],
  152. })
  153. self._sort_formats(formats)
  154. playlist.append({
  155. 'id': '%s_part%d' % (video_id, i + 1),
  156. 'title': title,
  157. 'duration': vid_data['data']['clipsDuration'][i],
  158. 'formats': formats,
  159. })
  160. if len(playlist) == 1:
  161. info = playlist[0]
  162. info['id'] = video_id
  163. else:
  164. info = {
  165. '_type': 'multi_video',
  166. 'entries': playlist,
  167. 'id': video_id,
  168. 'title': title,
  169. }
  170. return info