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.

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