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.

180 lines
5.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_request
  8. )
  9. from ..utils import sanitize_url_path_consecutive_slashes
  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': '699060e75cf58858dd47fb9c03c42cfb',
  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': '9bf34be48f2f4dadcb226c74127e203c',
  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. },
  46. 'playlist': [{
  47. 'md5': 'bdbfb8f39924725e6589c146bc1883ad',
  48. 'info_dict': {
  49. 'id': '78910339_part1',
  50. 'ext': 'mp4',
  51. 'duration': 294,
  52. 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
  53. }
  54. }, {
  55. 'md5': '3e1f46aaeb95354fd10e7fca9fc1804e',
  56. 'info_dict': {
  57. 'id': '78910339_part2',
  58. 'ext': 'mp4',
  59. 'duration': 300,
  60. 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
  61. }
  62. }, {
  63. 'md5': '8407e634175fdac706766481b9443450',
  64. 'info_dict': {
  65. 'id': '78910339_part3',
  66. 'ext': 'mp4',
  67. 'duration': 150,
  68. 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
  69. }
  70. }]
  71. }, {
  72. 'note': 'Video with title containing dash',
  73. 'url': 'http://my.tv.sohu.com/us/249884221/78932792.shtml',
  74. 'info_dict': {
  75. 'id': '78932792',
  76. 'ext': 'mp4',
  77. 'title': 'youtube-dl testing video',
  78. },
  79. 'params': {
  80. 'skip_download': True
  81. }
  82. }]
  83. def _real_extract(self, url):
  84. def _fetch_data(vid_id, mytv=False):
  85. if mytv:
  86. base_data_url = 'http://my.tv.sohu.com/play/videonew.do?vid='
  87. else:
  88. base_data_url = 'http://hot.vrs.sohu.com/vrs_flash.action?vid='
  89. req = compat_urllib_request.Request(base_data_url + vid_id)
  90. cn_verification_proxy = self._downloader.params.get('cn_verification_proxy')
  91. if cn_verification_proxy:
  92. req.add_header('Ytdl-request-proxy', cn_verification_proxy)
  93. return self._download_json(
  94. req, video_id,
  95. 'Downloading JSON data for %s' % vid_id)
  96. mobj = re.match(self._VALID_URL, url)
  97. video_id = mobj.group('id')
  98. mytv = mobj.group('mytv') is not None
  99. webpage = self._download_webpage(url, video_id)
  100. title = self._og_search_title(webpage)
  101. vid = self._html_search_regex(
  102. r'var vid ?= ?["\'](\d+)["\']',
  103. webpage, 'video path')
  104. vid_data = _fetch_data(vid, mytv)
  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. prot = format_data['prot']
  119. data = format_data['data']
  120. clips_url = data['clipsURL']
  121. su = data['su']
  122. part_str = self._download_webpage(
  123. 'http://%s/?prot=%s&file=%s&new=%s' %
  124. (allot, prot, clips_url[i], su[i]),
  125. video_id,
  126. 'Downloading %s video URL part %d of %d'
  127. % (format_id, i + 1, part_count))
  128. part_info = part_str.split('|')
  129. video_url = sanitize_url_path_consecutive_slashes(
  130. '%s%s?key=%s' % (part_info[0], su[i], part_info[3]))
  131. formats.append({
  132. 'url': video_url,
  133. 'format_id': format_id,
  134. 'filesize': data['clipsBytes'][i],
  135. 'width': data['width'],
  136. 'height': data['height'],
  137. 'fps': data['fps'],
  138. })
  139. self._sort_formats(formats)
  140. playlist.append({
  141. 'id': '%s_part%d' % (video_id, i + 1),
  142. 'title': title,
  143. 'duration': vid_data['data']['clipsDuration'][i],
  144. 'formats': formats,
  145. })
  146. if len(playlist) == 1:
  147. info = playlist[0]
  148. info['id'] = video_id
  149. else:
  150. info = {
  151. '_type': 'playlist',
  152. 'entries': playlist,
  153. 'id': video_id,
  154. }
  155. return info