2 from __future__ import division, unicode_literals
7 from .common import InfoExtractor
14 from ..compat import compat_urllib_parse_urlencode
17 class VLiveIE(InfoExtractor):
19 _VALID_URL = r'https?://(?:(?:www|m)\.)?vlive\.tv/video/(?P<id>[0-9]+)'
21 'url': 'http://www.vlive.tv/video/1326',
22 'md5': 'cc7314812855ce56de70a06a27314983',
26 'title': "[V] Girl's Day's Broadcast",
27 'creator': "Girl's Day",
32 def _real_extract(self, url):
33 video_id = self._match_id(url)
35 webpage = self._download_webpage(
36 'http://www.vlive.tv/video/%s' % video_id, video_id)
39 tz = time.altzone if time.localtime().tm_isdst == 1 else time.timezone
40 tz_offset = -tz // 60 - 9 * 60
41 self._set_cookie('vlive.tv', 'timezoneOffset', '%d' % tz_offset)
43 status_params = self._download_json(
44 'http://www.vlive.tv/video/status?videoSeq=%s' % video_id,
45 video_id, 'Downloading JSON status',
46 headers={'Referer': url.encode('utf-8')})
47 status = status_params.get('status')
48 air_start = status_params.get('onAirStartAt', '')
49 is_live = status_params.get('isLive')
51 video_params = self._search_regex(
52 r'vlive\.tv\.video\.ajax\.request\.handler\.init\((.+)\)',
53 webpage, 'video params')
54 live_params, long_video_id, key = re.split(
55 r'"\s*,\s*"', video_params)[1:4]
57 if status == 'LIVE_ON_AIR' or status == 'BIG_EVENT_ON_AIR':
58 live_params = self._parse_json('"%s"' % live_params, video_id)
59 live_params = self._parse_json(live_params, video_id)
60 return self._live(video_id, webpage, live_params)
61 elif status == 'VOD_ON_AIR' or status == 'BIG_EVENT_INTRO':
62 if long_video_id and key:
63 return self._replay(video_id, webpage, long_video_id, key)
67 status = 'COMING_SOON'
69 if status == 'LIVE_END':
70 raise ExtractorError('Uploading for replay. Please wait...',
72 elif status == 'COMING_SOON':
73 raise ExtractorError('Coming soon! %s' % air_start, expected=True)
74 elif status == 'CANCELED':
75 raise ExtractorError('We are sorry, '
76 'but the live broadcast has been canceled.',
79 raise ExtractorError('Unknown status %s' % status)
81 def _get_common_fields(self, webpage):
82 title = self._og_search_title(webpage)
83 creator = self._html_search_regex(
84 r'<div[^>]+class="info_area"[^>]*>\s*<a\s+[^>]*>([^<]+)',
85 webpage, 'creator', fatal=False)
86 thumbnail = self._og_search_thumbnail(webpage)
90 'thumbnail': thumbnail,
93 def _live(self, video_id, webpage, live_params):
95 for vid in live_params.get('resolutions', []):
96 formats.extend(self._extract_m3u8_formats(
97 vid['cdnUrl'], video_id, 'mp4',
98 m3u8_id=vid.get('name'),
99 fatal=False, live=True))
100 self._sort_formats(formats)
102 return dict(self._get_common_fields(webpage),
107 def _replay(self, video_id, webpage, long_video_id, key):
108 playinfo = self._download_json(
109 'http://global.apis.naver.com/rmcnmv/rmcnmv/vod_play_videoInfo.json?%s'
110 % compat_urllib_parse_urlencode({
111 'videoId': long_video_id,
114 'doct': 'json', # document type (xml or json)
115 'cpt': 'vtt', # captions type (vtt or ttml)
119 'url': vid['source'],
120 'format_id': vid.get('encodingOption', {}).get('name'),
121 'abr': float_or_none(vid.get('bitrate', {}).get('audio')),
122 'vbr': float_or_none(vid.get('bitrate', {}).get('video')),
123 'width': int_or_none(vid.get('encodingOption', {}).get('width')),
124 'height': int_or_none(vid.get('encodingOption', {}).get('height')),
125 'filesize': int_or_none(vid.get('size')),
126 } for vid in playinfo.get('videos', {}).get('list', []) if vid.get('source')]
127 self._sort_formats(formats)
129 view_count = int_or_none(playinfo.get('meta', {}).get('count'))
132 for caption in playinfo.get('captions', {}).get('list', []):
133 lang = dict_get(caption, ('language', 'locale', 'country', 'label'))
134 if lang and caption.get('source'):
137 'url': caption['source']}]
139 return dict(self._get_common_fields(webpage),
142 view_count=view_count,