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.

170 lines
5.7 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 url_sanitize_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. def _real_extract(self, url):
  73. def _fetch_data(vid_id, mytv=False):
  74. if mytv:
  75. base_data_url = 'http://my.tv.sohu.com/play/videonew.do?vid='
  76. else:
  77. base_data_url = 'http://hot.vrs.sohu.com/vrs_flash.action?vid='
  78. req = compat_urllib_request.Request(base_data_url + vid_id)
  79. cn_verification_proxy = self._downloader.params.get('cn_verification_proxy')
  80. if cn_verification_proxy:
  81. req.add_header('Ytdl-request-proxy', cn_verification_proxy)
  82. return self._download_json(req, video_id,
  83. 'Downloading JSON data for %s' % vid_id)
  84. mobj = re.match(self._VALID_URL, url)
  85. video_id = mobj.group('id')
  86. mytv = mobj.group('mytv') is not None
  87. webpage = self._download_webpage(url, video_id)
  88. raw_title = self._html_search_regex(
  89. r'(?s)<title>(.+?)</title>',
  90. webpage, 'video title')
  91. title = raw_title.partition('-')[0].strip()
  92. vid = self._html_search_regex(
  93. r'var vid ?= ?["\'](\d+)["\']',
  94. webpage, 'video path')
  95. vid_data = _fetch_data(vid, mytv)
  96. formats_json = {}
  97. for format_id in ('nor', 'high', 'super', 'ori', 'h2644k', 'h2654k'):
  98. vid_id = vid_data['data'].get('%sVid' % format_id)
  99. if not vid_id:
  100. continue
  101. vid_id = compat_str(vid_id)
  102. formats_json[format_id] = vid_data if vid == vid_id else _fetch_data(vid_id, mytv)
  103. part_count = vid_data['data']['totalBlocks']
  104. playlist = []
  105. for i in range(part_count):
  106. formats = []
  107. for format_id, format_data in formats_json.items():
  108. allot = format_data['allot']
  109. prot = format_data['prot']
  110. data = format_data['data']
  111. clips_url = data['clipsURL']
  112. su = data['su']
  113. part_str = self._download_webpage(
  114. 'http://%s/?prot=%s&file=%s&new=%s' %
  115. (allot, prot, clips_url[i], su[i]),
  116. video_id,
  117. 'Downloading %s video URL part %d of %d'
  118. % (format_id, i + 1, part_count))
  119. part_info = part_str.split('|')
  120. video_url = url_sanitize_consecutive_slashes(
  121. '%s%s?key=%s' % (part_info[0], su[i], part_info[3]))
  122. formats.append({
  123. 'url': video_url,
  124. 'format_id': format_id,
  125. 'filesize': data['clipsBytes'][i],
  126. 'width': data['width'],
  127. 'height': data['height'],
  128. 'fps': data['fps'],
  129. })
  130. self._sort_formats(formats)
  131. playlist.append({
  132. 'id': '%s_part%d' % (video_id, i + 1),
  133. 'title': title,
  134. 'duration': vid_data['data']['clipsDuration'][i],
  135. 'formats': formats,
  136. })
  137. if len(playlist) == 1:
  138. info = playlist[0]
  139. info['id'] = video_id
  140. else:
  141. info = {
  142. '_type': 'playlist',
  143. 'entries': playlist,
  144. 'id': video_id,
  145. }
  146. return info