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.

94 lines
3.1 KiB

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