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.

143 lines
5.4 KiB

9 years ago
  1. # coding: utf-8
  2. from __future__ import division, unicode_literals
  3. import re
  4. import time
  5. from .common import InfoExtractor
  6. from ..utils import (
  7. dict_get,
  8. ExtractorError,
  9. float_or_none,
  10. int_or_none,
  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] 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. # UTC+x - UTC+9 (KST)
  32. tz = time.altzone if time.localtime().tm_isdst == 1 else time.timezone
  33. tz_offset = -tz // 60 - 9 * 60
  34. self._set_cookie('vlive.tv', 'timezoneOffset', '%d' % tz_offset)
  35. status_params = self._download_json(
  36. 'http://www.vlive.tv/video/status?videoSeq=%s' % video_id,
  37. video_id, 'Downloading JSON status',
  38. headers={'Referer': url.encode('utf-8')})
  39. status = status_params.get('status')
  40. air_start = status_params.get('onAirStartAt', '')
  41. is_live = status_params.get('isLive')
  42. video_params = self._search_regex(
  43. r'vlive\.tv\.video\.ajax\.request\.handler\.init\((.+)\)',
  44. webpage, 'video params')
  45. live_params, long_video_id, key = re.split(
  46. r'"\s*,\s*"', video_params)[1:4]
  47. if status == 'LIVE_ON_AIR' or status == 'BIG_EVENT_ON_AIR':
  48. live_params = self._parse_json('"%s"' % live_params, video_id)
  49. live_params = self._parse_json(live_params, video_id)
  50. return self._live(video_id, webpage, live_params)
  51. elif status == 'VOD_ON_AIR' or status == 'BIG_EVENT_INTRO':
  52. if long_video_id and key:
  53. return self._replay(video_id, webpage, long_video_id, key)
  54. elif is_live:
  55. status = 'LIVE_END'
  56. else:
  57. status = 'COMING_SOON'
  58. if status == 'LIVE_END':
  59. raise ExtractorError('Uploading for replay. Please wait...',
  60. expected=True)
  61. elif status == 'COMING_SOON':
  62. raise ExtractorError('Coming soon! %s' % air_start, expected=True)
  63. elif status == 'CANCELED':
  64. raise ExtractorError('We are sorry, '
  65. 'but the live broadcast has been canceled.',
  66. expected=True)
  67. else:
  68. raise ExtractorError('Unknown status %s' % status)
  69. def _get_common_fields(self, webpage):
  70. title = self._og_search_title(webpage)
  71. creator = self._html_search_regex(
  72. r'<div[^>]+class="info_area"[^>]*>\s*<a\s+[^>]*>([^<]+)',
  73. webpage, 'creator', fatal=False)
  74. thumbnail = self._og_search_thumbnail(webpage)
  75. return {
  76. 'title': title,
  77. 'creator': creator,
  78. 'thumbnail': thumbnail,
  79. }
  80. def _live(self, video_id, webpage, live_params):
  81. formats = []
  82. for vid in live_params.get('resolutions', []):
  83. formats.extend(self._extract_m3u8_formats(
  84. vid['cdnUrl'], video_id, 'mp4',
  85. m3u8_id=vid.get('name'),
  86. fatal=False, live=True))
  87. self._sort_formats(formats)
  88. return dict(self._get_common_fields(webpage),
  89. id=video_id,
  90. formats=formats,
  91. is_live=True)
  92. def _replay(self, video_id, webpage, long_video_id, key):
  93. playinfo = self._download_json(
  94. 'http://global.apis.naver.com/rmcnmv/rmcnmv/vod_play_videoInfo.json?%s'
  95. % compat_urllib_parse_urlencode({
  96. 'videoId': long_video_id,
  97. 'key': key,
  98. 'ptc': 'http',
  99. 'doct': 'json', # document type (xml or json)
  100. 'cpt': 'vtt', # captions type (vtt or ttml)
  101. }), video_id)
  102. formats = [{
  103. 'url': vid['source'],
  104. 'format_id': vid.get('encodingOption', {}).get('name'),
  105. 'abr': float_or_none(vid.get('bitrate', {}).get('audio')),
  106. 'vbr': float_or_none(vid.get('bitrate', {}).get('video')),
  107. 'width': int_or_none(vid.get('encodingOption', {}).get('width')),
  108. 'height': int_or_none(vid.get('encodingOption', {}).get('height')),
  109. 'filesize': int_or_none(vid.get('size')),
  110. } for vid in playinfo.get('videos', {}).get('list', []) if vid.get('source')]
  111. self._sort_formats(formats)
  112. view_count = int_or_none(playinfo.get('meta', {}).get('count'))
  113. subtitles = {}
  114. for caption in playinfo.get('captions', {}).get('list', []):
  115. lang = dict_get(caption, ('language', 'locale', 'country', 'label'))
  116. if lang and caption.get('source'):
  117. subtitles[lang] = [{
  118. 'ext': 'vtt',
  119. 'url': caption['source']}]
  120. return dict(self._get_common_fields(webpage),
  121. id=video_id,
  122. formats=formats,
  123. view_count=view_count,
  124. subtitles=subtitles)