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.

109 lines
3.5 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 .common import compat_str
  6. class SohuIE(InfoExtractor):
  7. _VALID_URL = r'https?://(?P<mytv>my\.)?tv\.sohu\.com/.+?/(?(mytv)|n)(?P<id>\d+)\.shtml.*?'
  8. _TEST = {
  9. 'url': 'http://tv.sohu.com/20130724/n382479172.shtml#super',
  10. 'md5': 'bde8d9a6ffd82c63a1eefaef4eeefec7',
  11. 'info_dict': {
  12. 'id': '382479172',
  13. 'ext': 'mp4',
  14. 'title': 'MV:Far East Movement《The Illest》',
  15. },
  16. 'skip': 'Only available from China',
  17. }
  18. def _real_extract(self, url):
  19. def _fetch_data(vid_id, mytv=False):
  20. if mytv:
  21. base_data_url = 'http://my.tv.sohu.com/play/videonew.do?vid='
  22. else:
  23. base_data_url = 'http://hot.vrs.sohu.com/vrs_flash.action?vid='
  24. return self._download_json(
  25. base_data_url + vid_id, video_id,
  26. 'Downloading JSON data for %s' % vid_id)
  27. mobj = re.match(self._VALID_URL, url)
  28. video_id = mobj.group('id')
  29. mytv = mobj.group('mytv') is not None
  30. webpage = self._download_webpage(url, video_id)
  31. raw_title = self._html_search_regex(
  32. r'(?s)<title>(.+?)</title>',
  33. webpage, 'video title')
  34. title = raw_title.partition('-')[0].strip()
  35. vid = self._html_search_regex(
  36. r'var vid ?= ?["\'](\d+)["\']',
  37. webpage, 'video path')
  38. vid_data = _fetch_data(vid, mytv)
  39. formats_json = {}
  40. for format_id in ('nor', 'high', 'super', 'ori', 'h2644k', 'h2654k'):
  41. vid_id = vid_data['data'].get('%sVid' % format_id)
  42. if not vid_id:
  43. continue
  44. vid_id = compat_str(vid_id)
  45. formats_json[format_id] = vid_data if vid == vid_id else _fetch_data(vid_id, mytv)
  46. part_count = vid_data['data']['totalBlocks']
  47. playlist = []
  48. for i in range(part_count):
  49. formats = []
  50. for format_id, format_data in formats_json.items():
  51. allot = format_data['allot']
  52. prot = format_data['prot']
  53. data = format_data['data']
  54. clips_url = data['clipsURL']
  55. su = data['su']
  56. part_str = self._download_webpage(
  57. 'http://%s/?prot=%s&file=%s&new=%s' %
  58. (allot, prot, clips_url[i], su[i]),
  59. video_id,
  60. 'Downloading %s video URL part %d of %d'
  61. % (format_id, i + 1, part_count))
  62. part_info = part_str.split('|')
  63. video_url = '%s%s?key=%s' % (part_info[0], su[i], part_info[3])
  64. formats.append({
  65. 'url': video_url,
  66. 'format_id': format_id,
  67. 'filesize': data['clipsBytes'][i],
  68. 'width': data['width'],
  69. 'height': data['height'],
  70. 'fps': data['fps'],
  71. })
  72. self._sort_formats(formats)
  73. playlist.append({
  74. 'id': '%s_part%d' % (video_id, i + 1),
  75. 'title': title,
  76. 'duration': vid_data['data']['clipsDuration'][i],
  77. 'formats': formats,
  78. })
  79. if len(playlist) == 1:
  80. info = playlist[0]
  81. info['id'] = video_id
  82. else:
  83. info = {
  84. '_type': 'playlist',
  85. 'entries': playlist,
  86. 'id': video_id,
  87. }
  88. return info