]> jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/vlive.py
[discoverygo] Fix JSON data parsing
[yt-dlp.git] / youtube_dl / extractor / vlive.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8 dict_get,
9 ExtractorError,
10 float_or_none,
11 int_or_none,
12 remove_start,
13 urlencode_postdata,
14 )
15 from ..compat import compat_urllib_parse_urlencode
16
17
18 class VLiveIE(InfoExtractor):
19 IE_NAME = 'vlive'
20 _VALID_URL = r'https?://(?:(?:www|m)\.)?vlive\.tv/video/(?P<id>[0-9]+)'
21 _TESTS = [{
22 'url': 'http://www.vlive.tv/video/1326',
23 'md5': 'cc7314812855ce56de70a06a27314983',
24 'info_dict': {
25 'id': '1326',
26 'ext': 'mp4',
27 'title': "[V LIVE] Girl's Day's Broadcast",
28 'creator': "Girl's Day",
29 'view_count': int,
30 },
31 }, {
32 'url': 'http://www.vlive.tv/video/16937',
33 'info_dict': {
34 'id': '16937',
35 'ext': 'mp4',
36 'title': '[V LIVE] 첸백시 걍방',
37 'creator': 'EXO',
38 'view_count': int,
39 'subtitles': 'mincount:12',
40 },
41 'params': {
42 'skip_download': True,
43 },
44 }]
45
46 def _real_extract(self, url):
47 video_id = self._match_id(url)
48
49 webpage = self._download_webpage(
50 'http://www.vlive.tv/video/%s' % video_id, video_id)
51
52 VIDEO_PARAMS_RE = r'\bvlive\.video\.init\(([^)]+)'
53 VIDEO_PARAMS_FIELD = 'video params'
54
55 params = self._parse_json(self._search_regex(
56 VIDEO_PARAMS_RE, webpage, VIDEO_PARAMS_FIELD, default=''), video_id,
57 transform_source=lambda s: '[' + s + ']', fatal=False)
58
59 if not params or len(params) < 7:
60 params = self._search_regex(
61 VIDEO_PARAMS_RE, webpage, VIDEO_PARAMS_FIELD)
62 params = [p.strip(r'"') for p in re.split(r'\s*,\s*', params)]
63
64 status, long_video_id, key = params[2], params[5], params[6]
65 status = remove_start(status, 'PRODUCT_')
66
67 if status == 'LIVE_ON_AIR' or status == 'BIG_EVENT_ON_AIR':
68 return self._live(video_id, webpage)
69 elif status == 'VOD_ON_AIR' or status == 'BIG_EVENT_INTRO':
70 if long_video_id and key:
71 return self._replay(video_id, webpage, long_video_id, key)
72 else:
73 status = 'COMING_SOON'
74
75 if status == 'LIVE_END':
76 raise ExtractorError('Uploading for replay. Please wait...',
77 expected=True)
78 elif status == 'COMING_SOON':
79 raise ExtractorError('Coming soon!', expected=True)
80 elif status == 'CANCELED':
81 raise ExtractorError('We are sorry, '
82 'but the live broadcast has been canceled.',
83 expected=True)
84 else:
85 raise ExtractorError('Unknown status %s' % status)
86
87 def _get_common_fields(self, webpage):
88 title = self._og_search_title(webpage)
89 creator = self._html_search_regex(
90 r'<div[^>]+class="info_area"[^>]*>\s*<a\s+[^>]*>([^<]+)',
91 webpage, 'creator', fatal=False)
92 thumbnail = self._og_search_thumbnail(webpage)
93 return {
94 'title': title,
95 'creator': creator,
96 'thumbnail': thumbnail,
97 }
98
99 def _live(self, video_id, webpage):
100 init_page = self._download_webpage(
101 'http://www.vlive.tv/video/init/view',
102 video_id, note='Downloading live webpage',
103 data=urlencode_postdata({'videoSeq': video_id}),
104 headers={
105 'Referer': 'http://www.vlive.tv/video/%s' % video_id,
106 'Content-Type': 'application/x-www-form-urlencoded'
107 })
108
109 live_params = self._search_regex(
110 r'"liveStreamInfo"\s*:\s*(".*"),',
111 init_page, 'live stream info')
112 live_params = self._parse_json(live_params, video_id)
113 live_params = self._parse_json(live_params, video_id)
114
115 formats = []
116 for vid in live_params.get('resolutions', []):
117 formats.extend(self._extract_m3u8_formats(
118 vid['cdnUrl'], video_id, 'mp4',
119 m3u8_id=vid.get('name'),
120 fatal=False, live=True))
121 self._sort_formats(formats)
122
123 info = self._get_common_fields(webpage)
124 info.update({
125 'title': self._live_title(info['title']),
126 'id': video_id,
127 'formats': formats,
128 'is_live': True,
129 })
130 return info
131
132 def _replay(self, video_id, webpage, long_video_id, key):
133 playinfo = self._download_json(
134 'http://global.apis.naver.com/rmcnmv/rmcnmv/vod_play_videoInfo.json?%s'
135 % compat_urllib_parse_urlencode({
136 'videoId': long_video_id,
137 'key': key,
138 'ptc': 'http',
139 'doct': 'json', # document type (xml or json)
140 'cpt': 'vtt', # captions type (vtt or ttml)
141 }), video_id)
142
143 formats = [{
144 'url': vid['source'],
145 'format_id': vid.get('encodingOption', {}).get('name'),
146 'abr': float_or_none(vid.get('bitrate', {}).get('audio')),
147 'vbr': float_or_none(vid.get('bitrate', {}).get('video')),
148 'width': int_or_none(vid.get('encodingOption', {}).get('width')),
149 'height': int_or_none(vid.get('encodingOption', {}).get('height')),
150 'filesize': int_or_none(vid.get('size')),
151 } for vid in playinfo.get('videos', {}).get('list', []) if vid.get('source')]
152 self._sort_formats(formats)
153
154 view_count = int_or_none(playinfo.get('meta', {}).get('count'))
155
156 subtitles = {}
157 for caption in playinfo.get('captions', {}).get('list', []):
158 lang = dict_get(caption, ('locale', 'language', 'country', 'label'))
159 if lang and caption.get('source'):
160 subtitles[lang] = [{
161 'ext': 'vtt',
162 'url': caption['source']}]
163
164 info = self._get_common_fields(webpage)
165 info.update({
166 'id': video_id,
167 'formats': formats,
168 'view_count': view_count,
169 'subtitles': subtitles,
170 })
171 return info