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.

95 lines
3.2 KiB

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