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.

97 lines
3.2 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import json
  4. import re
  5. from .common import InfoExtractor
  6. from ..utils import ExtractorError
  7. class SohuIE(InfoExtractor):
  8. _VALID_URL = r'https?://(?P<mytv>my\.)?tv\.sohu\.com/.+?/(?(mytv)|n)(?P<id>\d+)\.shtml.*?'
  9. _TEST = {
  10. 'url': 'http://tv.sohu.com/20130724/n382479172.shtml#super',
  11. 'md5': 'bde8d9a6ffd82c63a1eefaef4eeefec7',
  12. 'info_dict': {
  13. 'id': '382479172',
  14. 'ext': 'mp4',
  15. 'title': 'MV:Far East Movement《The Illest》',
  16. },
  17. 'skip': 'Only available from China',
  18. }
  19. def _real_extract(self, url):
  20. def _fetch_data(vid_id, mytv=False):
  21. if mytv:
  22. base_data_url = 'http://my.tv.sohu.com/play/videonew.do?vid='
  23. else:
  24. base_data_url = 'http://hot.vrs.sohu.com/vrs_flash.action?vid='
  25. data_url = base_data_url + str(vid_id)
  26. data_json = self._download_webpage(
  27. data_url, video_id,
  28. note='Downloading JSON data for ' + str(vid_id))
  29. return json.loads(data_json)
  30. mobj = re.match(self._VALID_URL, url)
  31. video_id = mobj.group('id')
  32. mytv = mobj.group('mytv') is not None
  33. webpage = self._download_webpage(url, video_id)
  34. raw_title = self._html_search_regex(r'(?s)<title>(.+?)</title>',
  35. webpage, 'video title')
  36. title = raw_title.partition('-')[0].strip()
  37. vid = self._html_search_regex(r'var vid ?= ?["\'](\d+)["\']', webpage,
  38. 'video path')
  39. data = _fetch_data(vid, mytv)
  40. QUALITIES = ('ori', 'super', 'high', 'nor')
  41. vid_ids = [data['data'][q + 'Vid']
  42. for q in QUALITIES
  43. if data['data'][q + 'Vid'] != 0]
  44. if not vid_ids:
  45. raise ExtractorError('No formats available for this video')
  46. # For now, we just pick the highest available quality
  47. vid_id = vid_ids[-1]
  48. format_data = data if vid == vid_id else _fetch_data(vid_id, mytv)
  49. part_count = format_data['data']['totalBlocks']
  50. allot = format_data['allot']
  51. prot = format_data['prot']
  52. clipsURL = format_data['data']['clipsURL']
  53. su = format_data['data']['su']
  54. playlist = []
  55. for i in range(part_count):
  56. part_url = ('http://%s/?prot=%s&file=%s&new=%s' %
  57. (allot, prot, clipsURL[i], su[i]))
  58. part_str = self._download_webpage(
  59. part_url, video_id,
  60. note='Downloading part %d of %d' % (i + 1, part_count))
  61. part_info = part_str.split('|')
  62. video_url = '%s%s?key=%s' % (part_info[0], su[i], part_info[3])
  63. video_info = {
  64. 'id': '%s_part%02d' % (video_id, i + 1),
  65. 'title': title,
  66. 'url': video_url,
  67. 'ext': 'mp4',
  68. }
  69. playlist.append(video_info)
  70. if len(playlist) == 1:
  71. info = playlist[0]
  72. info['id'] = video_id
  73. else:
  74. info = {
  75. '_type': 'playlist',
  76. 'entries': playlist,
  77. 'id': video_id,
  78. }
  79. return info