[vlive] Add support for channels
[youtube-dl] / youtube_dl / extractor / vlive.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 import time
6 import itertools
7
8 from .common import InfoExtractor
9 from ..utils import (
10     dict_get,
11     ExtractorError,
12     float_or_none,
13     int_or_none,
14     remove_start,
15     urlencode_postdata,
16 )
17 from ..compat import compat_urllib_parse_urlencode
18
19
20 class VLiveIE(InfoExtractor):
21     IE_NAME = 'vlive'
22     _VALID_URL = r'https?://(?:(?:www|m)\.)?vlive\.tv/video/(?P<id>[0-9]+)'
23     _TESTS = [{
24         'url': 'http://www.vlive.tv/video/1326',
25         'md5': 'cc7314812855ce56de70a06a27314983',
26         'info_dict': {
27             'id': '1326',
28             'ext': 'mp4',
29             'title': "[V LIVE] Girl's Day's Broadcast",
30             'creator': "Girl's Day",
31             'view_count': int,
32         },
33     }, {
34         'url': 'http://www.vlive.tv/video/16937',
35         'info_dict': {
36             'id': '16937',
37             'ext': 'mp4',
38             'title': '[V LIVE] 첸백시 걍방',
39             'creator': 'EXO',
40             'view_count': int,
41             'subtitles': 'mincount:12',
42         },
43         'params': {
44             'skip_download': True,
45         },
46     }]
47
48     def _real_extract(self, url):
49         video_id = self._match_id(url)
50
51         webpage = self._download_webpage(
52             'http://www.vlive.tv/video/%s' % video_id, video_id)
53
54         VIDEO_PARAMS_RE = r'\bvlive\.video\.init\(([^)]+)'
55         VIDEO_PARAMS_FIELD = 'video params'
56
57         params = self._parse_json(self._search_regex(
58             VIDEO_PARAMS_RE, webpage, VIDEO_PARAMS_FIELD, default=''), video_id,
59             transform_source=lambda s: '[' + s + ']', fatal=False)
60
61         if not params or len(params) < 7:
62             params = self._search_regex(
63                 VIDEO_PARAMS_RE, webpage, VIDEO_PARAMS_FIELD)
64             params = [p.strip(r'"') for p in re.split(r'\s*,\s*', params)]
65
66         status, long_video_id, key = params[2], params[5], params[6]
67         status = remove_start(status, 'PRODUCT_')
68
69         if status == 'LIVE_ON_AIR' or status == 'BIG_EVENT_ON_AIR':
70             return self._live(video_id, webpage)
71         elif status == 'VOD_ON_AIR' or status == 'BIG_EVENT_INTRO':
72             if long_video_id and key:
73                 return self._replay(video_id, webpage, long_video_id, key)
74             else:
75                 status = 'COMING_SOON'
76
77         if status == 'LIVE_END':
78             raise ExtractorError('Uploading for replay. Please wait...',
79                                  expected=True)
80         elif status == 'COMING_SOON':
81             raise ExtractorError('Coming soon!', expected=True)
82         elif status == 'CANCELED':
83             raise ExtractorError('We are sorry, '
84                                  'but the live broadcast has been canceled.',
85                                  expected=True)
86         else:
87             raise ExtractorError('Unknown status %s' % status)
88
89     def _get_common_fields(self, webpage):
90         title = self._og_search_title(webpage)
91         creator = self._html_search_regex(
92             r'<div[^>]+class="info_area"[^>]*>\s*<a\s+[^>]*>([^<]+)',
93             webpage, 'creator', fatal=False)
94         thumbnail = self._og_search_thumbnail(webpage)
95         return {
96             'title': title,
97             'creator': creator,
98             'thumbnail': thumbnail,
99         }
100
101     def _live(self, video_id, webpage):
102         init_page = self._download_webpage(
103             'http://www.vlive.tv/video/init/view',
104             video_id, note='Downloading live webpage',
105             data=urlencode_postdata({'videoSeq': video_id}),
106             headers={
107                 'Referer': 'http://www.vlive.tv/video/%s' % video_id,
108                 'Content-Type': 'application/x-www-form-urlencoded'
109             })
110
111         live_params = self._search_regex(
112             r'"liveStreamInfo"\s*:\s*(".*"),',
113             init_page, 'live stream info')
114         live_params = self._parse_json(live_params, video_id)
115         live_params = self._parse_json(live_params, video_id)
116
117         formats = []
118         for vid in live_params.get('resolutions', []):
119             formats.extend(self._extract_m3u8_formats(
120                 vid['cdnUrl'], video_id, 'mp4',
121                 m3u8_id=vid.get('name'),
122                 fatal=False, live=True))
123         self._sort_formats(formats)
124
125         info = self._get_common_fields(webpage)
126         info.update({
127             'title': self._live_title(info['title']),
128             'id': video_id,
129             'formats': formats,
130             'is_live': True,
131         })
132         return info
133
134     def _replay(self, video_id, webpage, long_video_id, key):
135         playinfo = self._download_json(
136             'http://global.apis.naver.com/rmcnmv/rmcnmv/vod_play_videoInfo.json?%s'
137             % compat_urllib_parse_urlencode({
138                 'videoId': long_video_id,
139                 'key': key,
140                 'ptc': 'http',
141                 'doct': 'json',  # document type (xml or json)
142                 'cpt': 'vtt',  # captions type (vtt or ttml)
143             }), video_id)
144
145         formats = [{
146             'url': vid['source'],
147             'format_id': vid.get('encodingOption', {}).get('name'),
148             'abr': float_or_none(vid.get('bitrate', {}).get('audio')),
149             'vbr': float_or_none(vid.get('bitrate', {}).get('video')),
150             'width': int_or_none(vid.get('encodingOption', {}).get('width')),
151             'height': int_or_none(vid.get('encodingOption', {}).get('height')),
152             'filesize': int_or_none(vid.get('size')),
153         } for vid in playinfo.get('videos', {}).get('list', []) if vid.get('source')]
154         self._sort_formats(formats)
155
156         view_count = int_or_none(playinfo.get('meta', {}).get('count'))
157
158         subtitles = {}
159         for caption in playinfo.get('captions', {}).get('list', []):
160             lang = dict_get(caption, ('locale', 'language', 'country', 'label'))
161             if lang and caption.get('source'):
162                 subtitles[lang] = [{
163                     'ext': 'vtt',
164                     'url': caption['source']}]
165
166         info = self._get_common_fields(webpage)
167         info.update({
168             'id': video_id,
169             'formats': formats,
170             'view_count': view_count,
171             'subtitles': subtitles,
172         })
173         return info
174
175
176 class VLiveChannelIE(InfoExtractor):
177     IE_NAME = 'vlive:channel'
178     _VALID_URL = r'https?://channels\.vlive\.tv/(?P<id>[0-9A-Z]+)/video'
179     _TEST = {
180         'url': 'http://channels.vlive.tv/FCD4B/video',
181         'info_dict': {
182             'id': 'FCD4B',
183             'title': 'MAMAMOO',
184         },
185         'playlist_mincount': 110
186     }
187     _APP_ID = '8c6cc7b45d2568fb668be6e05b6e5a3b'
188
189     def _real_extract(self, url):
190         channel_code = self._match_id(url)
191
192         webpage = self._download_webpage(
193             'http://channels.vlive.tv/%s/video' % channel_code, channel_code)
194         app_js_url = self._search_regex(
195             r'(http[^\'\"\s]+app\.js)', webpage, 'app js', default='')
196
197         if app_js_url:
198             app_js = self._download_webpage(app_js_url, channel_code, 'app js')
199             app_id = self._search_regex(
200                 r'Global\.VFAN_APP_ID\s*=\s*[\'"]([^\'"]+)[\'"]',
201                 app_js, 'app id', default=self._APP_ID)
202         else:
203             app_id = self._APP_ID
204
205         channel_info = self._download_json(
206             'http://api.vfan.vlive.tv/vproxy/channelplus/decodeChannelCode',
207             channel_code, note='decode channel code',
208             query={'app_id': app_id, 'channelCode': channel_code, '_': int(time.time())})
209
210         channel_seq = channel_info['result']['channelSeq']
211         channel_name = None
212         entries = []
213
214         for page_num in itertools.count(1):
215             video_list = self._download_json(
216                 'http://api.vfan.vlive.tv/vproxy/channelplus/getChannelVideoList',
217                 channel_code, note='channel list %d' % page_num,
218                 query={
219                     'app_id': app_id,
220                     'channelSeq': channel_seq,
221                     'maxNumOfRows': 1000,
222                     '_': int(time.time()),
223                     'pageNo': page_num
224                 }
225             )
226             if not channel_name:
227                 channel_name = video_list['result']['channelInfo']['channelName']
228
229             if not video_list['result'].get('videoList'):
230                 break
231
232             for video in video_list['result']['videoList']:
233                 video_id = str(video['videoSeq'])
234                 entries.append(
235                     self.url_result(
236                         'http://www.vlive.tv/video/%s' % video_id, 'Vlive', video_id))
237
238         return self.playlist_result(
239             entries, channel_code, channel_name)