vlive.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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._parse_json(self._search_regex(
  45. r'\bvlive\.video\.init\(([^)]+)\);',
  46. webpage, 'video params'), video_id,
  47. transform_source=lambda s: '[' + s + ']')
  48. status, long_video_id, key = video_params[2], video_params[5], video_params[6]
  49. status = remove_start(status, 'PRODUCT_')
  50. if status == 'LIVE_ON_AIR' or status == 'BIG_EVENT_ON_AIR':
  51. return self._live(video_id, webpage)
  52. elif status == 'VOD_ON_AIR' or status == 'BIG_EVENT_INTRO':
  53. if long_video_id and key:
  54. return self._replay(video_id, webpage, long_video_id, key)
  55. else:
  56. status = 'COMING_SOON'
  57. if status == 'LIVE_END':
  58. raise ExtractorError('Uploading for replay. Please wait...',
  59. expected=True)
  60. elif status == 'COMING_SOON':
  61. raise ExtractorError('Coming soon!', expected=True)
  62. elif status == 'CANCELED':
  63. raise ExtractorError('We are sorry, '
  64. 'but the live broadcast has been canceled.',
  65. expected=True)
  66. else:
  67. raise ExtractorError('Unknown status %s' % status)
  68. def _get_common_fields(self, webpage):
  69. title = self._og_search_title(webpage)
  70. creator = self._html_search_regex(
  71. r'<div[^>]+class="info_area"[^>]*>\s*<a\s+[^>]*>([^<]+)',
  72. webpage, 'creator', fatal=False)
  73. thumbnail = self._og_search_thumbnail(webpage)
  74. return {
  75. 'title': title,
  76. 'creator': creator,
  77. 'thumbnail': thumbnail,
  78. }
  79. def _live(self, video_id, webpage):
  80. init_page = self._download_webpage(
  81. 'http://www.vlive.tv/video/init/view',
  82. video_id, data="videoSeq=%s" % video_id, headers={
  83. 'Referer': 'http://www.vlive.tv/video/%s' % video_id,
  84. 'Content-Type': 'application/x-www-form-urlencoded'
  85. })
  86. live_params = self._search_regex(
  87. r'"liveStreamInfo"\s*:\s*(".*"),',
  88. init_page, 'video params')
  89. live_params = self._parse_json(live_params, video_id)
  90. live_params = self._parse_json(live_params, video_id)
  91. formats = []
  92. for vid in live_params.get('resolutions', []):
  93. formats.extend(self._extract_m3u8_formats(
  94. vid['cdnUrl'], video_id, 'mp4',
  95. m3u8_id=vid.get('name'),
  96. fatal=False, live=True))
  97. self._sort_formats(formats)
  98. return dict(self._get_common_fields(webpage),
  99. id=video_id,
  100. formats=formats,
  101. is_live=True)
  102. def _replay(self, video_id, webpage, long_video_id, key):
  103. playinfo = self._download_json(
  104. 'http://global.apis.naver.com/rmcnmv/rmcnmv/vod_play_videoInfo.json?%s'
  105. % compat_urllib_parse_urlencode({
  106. 'videoId': long_video_id,
  107. 'key': key,
  108. 'ptc': 'http',
  109. 'doct': 'json', # document type (xml or json)
  110. 'cpt': 'vtt', # captions type (vtt or ttml)
  111. }), video_id)
  112. formats = [{
  113. 'url': vid['source'],
  114. 'format_id': vid.get('encodingOption', {}).get('name'),
  115. 'abr': float_or_none(vid.get('bitrate', {}).get('audio')),
  116. 'vbr': float_or_none(vid.get('bitrate', {}).get('video')),
  117. 'width': int_or_none(vid.get('encodingOption', {}).get('width')),
  118. 'height': int_or_none(vid.get('encodingOption', {}).get('height')),
  119. 'filesize': int_or_none(vid.get('size')),
  120. } for vid in playinfo.get('videos', {}).get('list', []) if vid.get('source')]
  121. self._sort_formats(formats)
  122. view_count = int_or_none(playinfo.get('meta', {}).get('count'))
  123. subtitles = {}
  124. for caption in playinfo.get('captions', {}).get('list', []):
  125. lang = dict_get(caption, ('locale', 'language', 'country', 'label'))
  126. if lang and caption.get('source'):
  127. subtitles[lang] = [{
  128. 'ext': 'vtt',
  129. 'url': caption['source']}]
  130. return dict(self._get_common_fields(webpage),
  131. id=video_id,
  132. formats=formats,
  133. view_count=view_count,
  134. subtitles=subtitles)