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.

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