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.

127 lines
4.7 KiB

9 years ago
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. dict_get,
  7. ExtractorError,
  8. float_or_none,
  9. int_or_none,
  10. )
  11. from ..compat import compat_urllib_parse_urlencode
  12. class VLiveIE(InfoExtractor):
  13. IE_NAME = 'vlive'
  14. _VALID_URL = r'https?://(?:(?:www|m)\.)?vlive\.tv/video/(?P<id>[0-9]+)'
  15. _TEST = {
  16. 'url': 'http://www.vlive.tv/video/1326',
  17. 'md5': 'cc7314812855ce56de70a06a27314983',
  18. 'info_dict': {
  19. 'id': '1326',
  20. 'ext': 'mp4',
  21. 'title': "[V LIVE] Girl's Day's Broadcast",
  22. 'creator': "Girl's Day",
  23. 'view_count': int,
  24. },
  25. }
  26. def _real_extract(self, url):
  27. video_id = self._match_id(url)
  28. webpage = self._download_webpage(
  29. 'http://www.vlive.tv/video/%s' % video_id, video_id)
  30. video_params = self._search_regex(
  31. r'\bvlive\.video\.init\(([^)]+)\)',
  32. webpage, 'video params')
  33. status, _, _, live_params, long_video_id, key = re.split(
  34. r'"\s*,\s*"', video_params)[2:8]
  35. if status == 'LIVE_ON_AIR' or status == 'BIG_EVENT_ON_AIR':
  36. live_params = self._parse_json('"%s"' % live_params, video_id)
  37. live_params = self._parse_json(live_params, video_id)
  38. return self._live(video_id, webpage, live_params)
  39. elif status == 'VOD_ON_AIR' or status == 'BIG_EVENT_INTRO':
  40. if long_video_id and key:
  41. return self._replay(video_id, webpage, long_video_id, key)
  42. else:
  43. status = 'COMING_SOON'
  44. if status == 'LIVE_END':
  45. raise ExtractorError('Uploading for replay. Please wait...',
  46. expected=True)
  47. elif status == 'COMING_SOON':
  48. raise ExtractorError('Coming soon!', expected=True)
  49. elif status == 'CANCELED':
  50. raise ExtractorError('We are sorry, '
  51. 'but the live broadcast has been canceled.',
  52. expected=True)
  53. else:
  54. raise ExtractorError('Unknown status %s' % status)
  55. def _get_common_fields(self, webpage):
  56. title = self._og_search_title(webpage)
  57. creator = self._html_search_regex(
  58. r'<div[^>]+class="info_area"[^>]*>\s*<a\s+[^>]*>([^<]+)',
  59. webpage, 'creator', fatal=False)
  60. thumbnail = self._og_search_thumbnail(webpage)
  61. return {
  62. 'title': title,
  63. 'creator': creator,
  64. 'thumbnail': thumbnail,
  65. }
  66. def _live(self, video_id, webpage, live_params):
  67. formats = []
  68. for vid in live_params.get('resolutions', []):
  69. formats.extend(self._extract_m3u8_formats(
  70. vid['cdnUrl'], video_id, 'mp4',
  71. m3u8_id=vid.get('name'),
  72. fatal=False, live=True))
  73. self._sort_formats(formats)
  74. return dict(self._get_common_fields(webpage),
  75. id=video_id,
  76. formats=formats,
  77. is_live=True)
  78. def _replay(self, video_id, webpage, long_video_id, key):
  79. playinfo = self._download_json(
  80. 'http://global.apis.naver.com/rmcnmv/rmcnmv/vod_play_videoInfo.json?%s'
  81. % compat_urllib_parse_urlencode({
  82. 'videoId': long_video_id,
  83. 'key': key,
  84. 'ptc': 'http',
  85. 'doct': 'json', # document type (xml or json)
  86. 'cpt': 'vtt', # captions type (vtt or ttml)
  87. }), video_id)
  88. formats = [{
  89. 'url': vid['source'],
  90. 'format_id': vid.get('encodingOption', {}).get('name'),
  91. 'abr': float_or_none(vid.get('bitrate', {}).get('audio')),
  92. 'vbr': float_or_none(vid.get('bitrate', {}).get('video')),
  93. 'width': int_or_none(vid.get('encodingOption', {}).get('width')),
  94. 'height': int_or_none(vid.get('encodingOption', {}).get('height')),
  95. 'filesize': int_or_none(vid.get('size')),
  96. } for vid in playinfo.get('videos', {}).get('list', []) if vid.get('source')]
  97. self._sort_formats(formats)
  98. view_count = int_or_none(playinfo.get('meta', {}).get('count'))
  99. subtitles = {}
  100. for caption in playinfo.get('captions', {}).get('list', []):
  101. lang = dict_get(caption, ('language', 'locale', 'country', 'label'))
  102. if lang and caption.get('source'):
  103. subtitles[lang] = [{
  104. 'ext': 'vtt',
  105. 'url': caption['source']}]
  106. return dict(self._get_common_fields(webpage),
  107. id=video_id,
  108. formats=formats,
  109. view_count=view_count,
  110. subtitles=subtitles)