logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

sohu.py (6911B)


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