[vlive] Add video params extraction fallback and improve (closes #11375)
[youtube-dl] / 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         return dict(self._get_common_fields(webpage),
124                     id=video_id,
125                     formats=formats,
126                     is_live=True)
127
128     def _replay(self, video_id, webpage, long_video_id, key):
129         playinfo = self._download_json(
130             'http://global.apis.naver.com/rmcnmv/rmcnmv/vod_play_videoInfo.json?%s'
131             % compat_urllib_parse_urlencode({
132                 'videoId': long_video_id,
133                 'key': key,
134                 'ptc': 'http',
135                 'doct': 'json',  # document type (xml or json)
136                 'cpt': 'vtt',  # captions type (vtt or ttml)
137             }), video_id)
138
139         formats = [{
140             'url': vid['source'],
141             'format_id': vid.get('encodingOption', {}).get('name'),
142             'abr': float_or_none(vid.get('bitrate', {}).get('audio')),
143             'vbr': float_or_none(vid.get('bitrate', {}).get('video')),
144             'width': int_or_none(vid.get('encodingOption', {}).get('width')),
145             'height': int_or_none(vid.get('encodingOption', {}).get('height')),
146             'filesize': int_or_none(vid.get('size')),
147         } for vid in playinfo.get('videos', {}).get('list', []) if vid.get('source')]
148         self._sort_formats(formats)
149
150         view_count = int_or_none(playinfo.get('meta', {}).get('count'))
151
152         subtitles = {}
153         for caption in playinfo.get('captions', {}).get('list', []):
154             lang = dict_get(caption, ('locale', 'language', 'country', 'label'))
155             if lang and caption.get('source'):
156                 subtitles[lang] = [{
157                     'ext': 'vtt',
158                     'url': caption['source']}]
159
160         return dict(self._get_common_fields(webpage),
161                     id=video_id,
162                     formats=formats,
163                     view_count=view_count,
164                     subtitles=subtitles)