sohu.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. import base64
  2. import re
  3. import urllib.parse
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. ExtractorError,
  7. float_or_none,
  8. int_or_none,
  9. traverse_obj,
  10. try_get,
  11. unified_timestamp,
  12. url_or_none,
  13. urljoin,
  14. )
  15. class SohuIE(InfoExtractor):
  16. _VALID_URL = r'https?://(?P<mytv>my\.)?tv\.sohu\.com/.+?/(?(mytv)|n)(?P<id>\d+)\.shtml.*?'
  17. # Sohu videos give different MD5 sums on Travis CI and my machine
  18. _TESTS = [{
  19. 'note': 'This video is available only in Mainland China',
  20. 'url': 'http://tv.sohu.com/20130724/n382479172.shtml#super',
  21. 'info_dict': {
  22. 'id': '382479172',
  23. 'ext': 'mp4',
  24. 'title': 'MV:Far East Movement《The Illest》',
  25. },
  26. 'skip': 'On available in China',
  27. }, {
  28. 'url': 'http://tv.sohu.com/20150305/n409385080.shtml',
  29. 'info_dict': {
  30. 'id': '409385080',
  31. 'ext': 'mp4',
  32. 'title': '《2015湖南卫视羊年元宵晚会》唐嫣《花好月圆》',
  33. },
  34. 'skip': 'no longer available',
  35. }, {
  36. 'url': 'http://my.tv.sohu.com/us/232799889/78693464.shtml',
  37. 'info_dict': {
  38. 'id': '78693464',
  39. 'ext': 'mp4',
  40. 'title': '【爱范品】第31期:MWC见不到的奇葩手机',
  41. 'uploader': '爱范儿视频',
  42. 'duration': 213,
  43. 'timestamp': 1425519600,
  44. 'upload_date': '20150305',
  45. 'thumbnail': 'http://e3f49eaa46b57.cdn.sohucs.com//group1/M10/83/FA/MTAuMTAuODguODA=/6_14cbccdde5eg104SysCutcloud_78693464_7_0b.jpg',
  46. 'tags': ['爱范儿', '爱范品', 'MWC', '手机'],
  47. },
  48. }, {
  49. 'note': 'Multipart video',
  50. 'url': 'http://my.tv.sohu.com/pl/8384802/78910339.shtml',
  51. 'info_dict': {
  52. 'id': '78910339',
  53. 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
  54. 'uploader': '小苍cany',
  55. 'duration': 744.0,
  56. 'timestamp': 1426269360,
  57. 'upload_date': '20150313',
  58. 'thumbnail': 'http://e3f49eaa46b57.cdn.sohucs.com//group1/M11/89/57/MTAuMTAuODguODA=/6_14cea022a1dg102SysCutcloud_78910339_8_0b.jpg',
  59. 'tags': ['小苍MM', '英雄联盟', '实战秘籍'],
  60. },
  61. 'playlist': [{
  62. 'info_dict': {
  63. 'id': '78910339_part1',
  64. 'ext': 'mp4',
  65. 'duration': 294,
  66. 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
  67. },
  68. }, {
  69. 'info_dict': {
  70. 'id': '78910339_part2',
  71. 'ext': 'mp4',
  72. 'duration': 300,
  73. 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
  74. },
  75. }, {
  76. 'info_dict': {
  77. 'id': '78910339_part3',
  78. 'ext': 'mp4',
  79. 'duration': 150,
  80. 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
  81. },
  82. }],
  83. }, {
  84. 'note': 'Video with title containing dash',
  85. 'url': 'http://my.tv.sohu.com/us/249884221/78932792.shtml',
  86. 'info_dict': {
  87. 'id': '78932792',
  88. 'ext': 'mp4',
  89. 'title': 'youtube-dl testing video',
  90. 'duration': 360,
  91. 'timestamp': 1426348620,
  92. 'upload_date': '20150314',
  93. 'thumbnail': 'http://e3f49eaa46b57.cdn.sohucs.com//group1/M02/8A/00/MTAuMTAuODguNzk=/6_14cee1be192g102SysCutcloud_78932792_7_7b.jpg',
  94. 'tags': [],
  95. },
  96. 'params': {
  97. 'skip_download': True,
  98. },
  99. }]
  100. def _real_extract(self, url):
  101. def _fetch_data(vid_id, mytv=False):
  102. if mytv:
  103. base_data_url = 'http://my.tv.sohu.com/play/videonew.do?vid='
  104. else:
  105. base_data_url = 'http://hot.vrs.sohu.com/vrs_flash.action?vid='
  106. return self._download_json(
  107. base_data_url + vid_id, video_id,
  108. f'Downloading JSON data for {vid_id}',
  109. headers=self.geo_verification_headers())
  110. mobj = self._match_valid_url(url)
  111. video_id = mobj.group('id')
  112. mytv = mobj.group('mytv') is not None
  113. webpage = self._download_webpage(url, video_id)
  114. title = re.sub(r'( - 高清正版在线观看)? - 搜狐视频$', '', self._og_search_title(webpage))
  115. vid = self._html_search_regex(
  116. r'var vid ?= ?["\'](\d+)["\']',
  117. webpage, 'video path')
  118. vid_data = _fetch_data(vid, mytv)
  119. if vid_data['play'] != 1:
  120. if vid_data.get('status') == 12:
  121. raise ExtractorError(
  122. f'{self.IE_NAME} said: There\'s something wrong in the video.',
  123. expected=True)
  124. else:
  125. self.raise_geo_restricted(
  126. f'{self.IE_NAME} said: The video is only licensed to users in Mainland China.')
  127. formats_json = {}
  128. for format_id in ('nor', 'high', 'super', 'ori', 'h2644k', 'h2654k'):
  129. vid_id = vid_data['data'].get(f'{format_id}Vid')
  130. if not vid_id:
  131. continue
  132. vid_id = str(vid_id)
  133. formats_json[format_id] = vid_data if vid == vid_id else _fetch_data(vid_id, mytv)
  134. part_count = vid_data['data']['totalBlocks']
  135. playlist = []
  136. for i in range(part_count):
  137. formats = []
  138. for format_id, format_data in formats_json.items():
  139. allot = format_data['allot']
  140. data = format_data['data']
  141. clip_url = traverse_obj(data, (('clipsURL', 'mp4PlayUrl'), i, {url_or_none}), get_all=False)
  142. if not clip_url:
  143. raise ExtractorError(f'Unable to extract url for clip {i}')
  144. su = data['su']
  145. video_url = 'newflv.sohu.ccgslb.net'
  146. cdn_id = None
  147. retries = 0
  148. while 'newflv.sohu.ccgslb.net' in video_url:
  149. params = {
  150. 'prot': 9,
  151. 'file': clip_url,
  152. 'new': su[i],
  153. 'prod': 'h5n',
  154. 'rb': 1,
  155. }
  156. if cdn_id is not None:
  157. params['idc'] = cdn_id
  158. download_note = f'Downloading {format_id} video URL part {i + 1} of {part_count}'
  159. if retries > 0:
  160. download_note += f' (retry #{retries})'
  161. part_info = self._parse_json(self._download_webpage(
  162. f'http://{allot}/?{urllib.parse.urlencode(params)}',
  163. video_id, download_note), video_id)
  164. video_url = part_info['url']
  165. cdn_id = part_info.get('nid')
  166. retries += 1
  167. if retries > 5:
  168. raise ExtractorError('Failed to get video URL')
  169. formats.append({
  170. 'url': video_url,
  171. 'format_id': format_id,
  172. 'filesize': int_or_none(
  173. try_get(data, lambda x: x['clipsBytes'][i])),
  174. 'width': int_or_none(data.get('width')),
  175. 'height': int_or_none(data.get('height')),
  176. 'fps': int_or_none(data.get('fps')),
  177. })
  178. playlist.append({
  179. 'id': f'{video_id}_part{i + 1}',
  180. 'title': title,
  181. 'duration': vid_data['data']['clipsDuration'][i],
  182. 'formats': formats,
  183. })
  184. if len(playlist) == 1:
  185. info = playlist[0]
  186. info['id'] = video_id
  187. else:
  188. info = {
  189. '_type': 'multi_video',
  190. 'entries': playlist,
  191. 'id': video_id,
  192. 'title': title,
  193. 'duration': traverse_obj(vid_data, ('data', 'totalDuration', {float_or_none})),
  194. }
  195. if mytv:
  196. publish_time = unified_timestamp(self._search_regex(
  197. r'publishTime:\s*["\'](\d+-\d+-\d+ \d+:\d+)["\']', webpage, 'publish time', fatal=False))
  198. else:
  199. publish_time = traverse_obj(vid_data, ('tv_application_time', {unified_timestamp}))
  200. return {
  201. 'timestamp': publish_time - 8 * 3600 if publish_time else None,
  202. **traverse_obj(vid_data, {
  203. 'alt_title': ('data', 'subName', {str}),
  204. 'uploader': ('wm_data', 'wm_username', {str}),
  205. 'thumbnail': ('data', 'coverImg', {url_or_none}),
  206. 'tags': ('data', 'tag', {str.split}),
  207. }),
  208. **info,
  209. }
  210. class SohuVIE(InfoExtractor):
  211. _VALID_URL = r'https?://tv\.sohu\.com/v/(?P<id>[\w=-]+)\.html(?:$|[#?])'
  212. _TESTS = [{
  213. 'note': 'Multipart video',
  214. 'url': 'https://tv.sohu.com/v/MjAyMzA2MTQvbjYwMTMxNTE5Mi5zaHRtbA==.html',
  215. 'info_dict': {
  216. 'id': '601315192',
  217. 'title': '《淬火丹心》第1集',
  218. 'alt_title': '“点天灯”发生事故',
  219. 'duration': 2701.692,
  220. 'timestamp': 1686758040,
  221. 'upload_date': '20230614',
  222. 'thumbnail': 'http://photocdn.tv.sohu.com/img/20230614/vrsa_hor_1686738763256_454010551.jpg',
  223. },
  224. 'playlist_mincount': 9,
  225. 'skip': 'Only available in China',
  226. }, {
  227. 'url': 'https://tv.sohu.com/v/dXMvMjMyNzk5ODg5Lzc4NjkzNDY0LnNodG1s.html',
  228. 'info_dict': {
  229. 'id': '78693464',
  230. 'ext': 'mp4',
  231. 'title': '【爱范品】第31期:MWC见不到的奇葩手机',
  232. 'uploader': '爱范儿视频',
  233. 'duration': 213,
  234. 'timestamp': 1425519600,
  235. 'upload_date': '20150305',
  236. 'thumbnail': 'http://e3f49eaa46b57.cdn.sohucs.com//group1/M10/83/FA/MTAuMTAuODguODA=/6_14cbccdde5eg104SysCutcloud_78693464_7_0b.jpg',
  237. 'tags': ['爱范儿', '爱范品', 'MWC', '手机'],
  238. },
  239. }, {
  240. 'note': 'Multipart video',
  241. 'url': 'https://tv.sohu.com/v/dXMvMjQyNTYyMTYzLzc4OTEwMzM5LnNodG1s.html?src=pl',
  242. 'info_dict': {
  243. 'id': '78910339',
  244. 'title': '【神探苍实战秘籍】第13期 战争之影 赫卡里姆',
  245. 'uploader': '小苍cany',
  246. 'duration': 744.0,
  247. 'timestamp': 1426269360,
  248. 'upload_date': '20150313',
  249. 'thumbnail': 'http://e3f49eaa46b57.cdn.sohucs.com//group1/M11/89/57/MTAuMTAuODguODA=/6_14cea022a1dg102SysCutcloud_78910339_8_0b.jpg',
  250. 'tags': ['小苍MM', '英雄联盟', '实战秘籍'],
  251. },
  252. 'playlist_mincount': 3,
  253. }]
  254. def _real_extract(self, url):
  255. encoded_id = self._match_id(url)
  256. path = base64.urlsafe_b64decode(encoded_id).decode()
  257. subdomain = 'tv' if re.match(r'\d+/n\d+\.shtml', path) else 'my.tv'
  258. return self.url_result(urljoin(f'http://{subdomain}.sohu.com/', path), SohuIE)