]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/vlive.py
[ooyala] use api v2 to reduce requests for format extraction
[yt-dlp.git] / youtube_dl / extractor / vlive.py
CommitLineData
061f62da 1# coding: utf-8
b24d6336 2from __future__ import division, unicode_literals
061f62da 3
b24d6336
KH
4import re
5import time
9d186afa 6
061f62da 7from .common import InfoExtractor
8from ..utils import (
52f5889f 9 dict_get,
9d186afa 10 ExtractorError,
52f5889f
S
11 float_or_none,
12 int_or_none,
061f62da 13)
15707c7e 14from ..compat import compat_urllib_parse_urlencode
061f62da 15
16
17class VLiveIE(InfoExtractor):
18 IE_NAME = 'vlive'
52f5889f 19 _VALID_URL = r'https?://(?:(?:www|m)\.)?vlive\.tv/video/(?P<id>[0-9]+)'
061f62da 20 _TEST = {
b8b465af 21 'url': 'http://www.vlive.tv/video/1326',
061f62da 22 'md5': 'cc7314812855ce56de70a06a27314983',
23 'info_dict': {
24 'id': '1326',
25 'ext': 'mp4',
52f5889f
S
26 'title': "[V] Girl's Day's Broadcast",
27 'creator': "Girl's Day",
28 'view_count': int,
061f62da 29 },
30 }
061f62da 31
32 def _real_extract(self, url):
33 video_id = self._match_id(url)
34
35 webpage = self._download_webpage(
52f5889f 36 'http://www.vlive.tv/video/%s' % video_id, video_id)
061f62da 37
b24d6336
KH
38 # UTC+x - UTC+9 (KST)
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)
b8b465af 42
b24d6336
KH
43 status_params = self._download_json(
44 'http://www.vlive.tv/video/status?videoSeq=%s' % video_id,
45 video_id, 'Downloading JSON status',
d41ee7b7 46 headers={'Referer': url.encode('utf-8')})
b24d6336
KH
47 status = status_params.get('status')
48 air_start = status_params.get('onAirStartAt', '')
49 is_live = status_params.get('isLive')
b8b465af 50
b24d6336
KH
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]
56
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)
64 elif is_live:
65 status = 'LIVE_END'
66 else:
67 status = 'COMING_SOON'
68
69 if status == 'LIVE_END':
70 raise ExtractorError('Uploading for replay. Please wait...',
71 expected=True)
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.',
77 expected=True)
78 else:
79 raise ExtractorError('Unknown status %s' % status)
80
81 def _get_common_fields(self, webpage):
061f62da 82 title = self._og_search_title(webpage)
b24d6336
KH
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)
87 return {
88 'title': title,
89 'creator': creator,
90 'thumbnail': thumbnail,
91 }
08354db4 92
b24d6336
KH
93 def _live(self, video_id, webpage, live_params):
94 formats = []
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)
101
102 return dict(self._get_common_fields(webpage),
9d186afa
YCH
103 id=video_id,
104 formats=formats,
105 is_live=True)
b24d6336
KH
106
107 def _replay(self, video_id, webpage, long_video_id, key):
52f5889f
S
108 playinfo = self._download_json(
109 'http://global.apis.naver.com/rmcnmv/rmcnmv/vod_play_videoInfo.json?%s'
15707c7e 110 % compat_urllib_parse_urlencode({
52f5889f
S
111 'videoId': long_video_id,
112 'key': key,
113 'ptc': 'http',
114 'doct': 'json', # document type (xml or json)
115 'cpt': 'vtt', # captions type (vtt or ttml)
116 }), video_id)
061f62da 117
52f5889f
S
118 formats = [{
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')]
061f62da 127 self._sort_formats(formats)
128
52f5889f
S
129 view_count = int_or_none(playinfo.get('meta', {}).get('count'))
130
061f62da 131 subtitles = {}
b8b465af 132 for caption in playinfo.get('captions', {}).get('list', []):
52f5889f
S
133 lang = dict_get(caption, ('language', 'locale', 'country', 'label'))
134 if lang and caption.get('source'):
135 subtitles[lang] = [{
136 'ext': 'vtt',
137 'url': caption['source']}]
061f62da 138
b24d6336 139 return dict(self._get_common_fields(webpage),
9d186afa
YCH
140 id=video_id,
141 formats=formats,
142 view_count=view_count,
143 subtitles=subtitles)