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.

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