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.

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