sohu.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. def _real_extract(self, url):
  42. def _fetch_data(vid_id, mytv=False):
  43. if mytv:
  44. base_data_url = 'http://my.tv.sohu.com/play/videonew.do?vid='
  45. else:
  46. base_data_url = 'http://hot.vrs.sohu.com/vrs_flash.action?vid='
  47. req = compat_urllib_request.Request(base_data_url + vid_id)
  48. cn_verification_proxy = self._downloader.params.get('cn_verification_proxy')
  49. if cn_verification_proxy:
  50. req.add_header('Ytdl-request-proxy', cn_verification_proxy)
  51. return self._download_json(req, video_id,
  52. 'Downloading JSON data for %s' % vid_id)
  53. mobj = re.match(self._VALID_URL, url)
  54. video_id = mobj.group('id')
  55. mytv = mobj.group('mytv') is not None
  56. webpage = self._download_webpage(url, video_id)
  57. raw_title = self._html_search_regex(
  58. r'(?s)<title>(.+?)</title>',
  59. webpage, 'video title')
  60. title = raw_title.partition('-')[0].strip()
  61. vid = self._html_search_regex(
  62. r'var vid ?= ?["\'](\d+)["\']',
  63. webpage, 'video path')
  64. vid_data = _fetch_data(vid, mytv)
  65. formats_json = {}
  66. for format_id in ('nor', 'high', 'super', 'ori', 'h2644k', 'h2654k'):
  67. vid_id = vid_data['data'].get('%sVid' % format_id)
  68. if not vid_id:
  69. continue
  70. vid_id = compat_str(vid_id)
  71. formats_json[format_id] = vid_data if vid == vid_id else _fetch_data(vid_id, mytv)
  72. part_count = vid_data['data']['totalBlocks']
  73. playlist = []
  74. for i in range(part_count):
  75. formats = []
  76. for format_id, format_data in formats_json.items():
  77. allot = format_data['allot']
  78. prot = format_data['prot']
  79. data = format_data['data']
  80. clips_url = data['clipsURL']
  81. su = data['su']
  82. part_str = self._download_webpage(
  83. 'http://%s/?prot=%s&file=%s&new=%s' %
  84. (allot, prot, clips_url[i], su[i]),
  85. video_id,
  86. 'Downloading %s video URL part %d of %d'
  87. % (format_id, i + 1, part_count))
  88. part_info = part_str.split('|')
  89. video_url = url_sanitize_consecutive_slashes(
  90. '%s%s?key=%s' % (part_info[0], su[i], part_info[3]))
  91. formats.append({
  92. 'url': video_url,
  93. 'format_id': format_id,
  94. 'filesize': data['clipsBytes'][i],
  95. 'width': data['width'],
  96. 'height': data['height'],
  97. 'fps': data['fps'],
  98. })
  99. self._sort_formats(formats)
  100. playlist.append({
  101. 'id': '%s_part%d' % (video_id, i + 1),
  102. 'title': title,
  103. 'duration': vid_data['data']['clipsDuration'][i],
  104. 'formats': formats,
  105. })
  106. if len(playlist) == 1:
  107. info = playlist[0]
  108. info['id'] = video_id
  109. else:
  110. info = {
  111. '_type': 'playlist',
  112. 'entries': playlist,
  113. 'id': video_id,
  114. }
  115. return info