2 from __future__ 
import unicode_literals
 
   8 from .common 
import InfoExtractor
 
  10     compat_urllib_parse_urlencode
, 
  24 class VLiveIE(InfoExtractor
): 
  26     _VALID_URL 
= r
'https?://(?:(?:www|m)\.)?vlive\.tv/video/(?P<id>[0-9]+)' 
  28         'url': 'http://www.vlive.tv/video/1326', 
  29         'md5': 'cc7314812855ce56de70a06a27314983', 
  33             'title': "[V LIVE] Girl's Day's Broadcast", 
  34             'creator': "Girl's Day", 
  38         'url': 'http://www.vlive.tv/video/16937', 
  42             'title': '[V LIVE] 첸백시 걍방', 
  45             'subtitles': 'mincount:12', 
  48             'skip_download': True, 
  53     def suitable(cls
, url
): 
  54         return False if VLivePlaylistIE
.suitable(url
) else super(VLiveIE
, cls
).suitable(url
) 
  56     def _real_extract(self
, url
): 
  57         video_id 
= self
._match
_id
(url
) 
  59         webpage 
= self
._download
_webpage
( 
  60             'http://www.vlive.tv/video/%s' % video_id
, video_id
) 
  62         VIDEO_PARAMS_RE 
= r
'\bvlive\.video\.init\(([^)]+)' 
  63         VIDEO_PARAMS_FIELD 
= 'video params' 
  65         params 
= self
._parse
_json
(self
._search
_regex
( 
  66             VIDEO_PARAMS_RE
, webpage
, VIDEO_PARAMS_FIELD
, default
=''), video_id
, 
  67             transform_source
=lambda s
: '[' + s 
+ ']', fatal
=False) 
  69         if not params 
or len(params
) < 7: 
  70             params 
= self
._search
_regex
( 
  71                 VIDEO_PARAMS_RE
, webpage
, VIDEO_PARAMS_FIELD
) 
  72             params 
= [p
.strip(r
'"') for p 
in re
.split(r
'\s*,\s*', params
)] 
  74         status
, long_video_id
, key 
= params
[2], params
[5], params
[6] 
  75         status 
= remove_start(status
, 'PRODUCT_') 
  77         if status 
in ('LIVE_ON_AIR', 'BIG_EVENT_ON_AIR'): 
  78             return self
._live
(video_id
, webpage
) 
  79         elif status 
in ('VOD_ON_AIR', 'BIG_EVENT_INTRO'): 
  80             if long_video_id 
and key
: 
  81                 return self
._replay
(video_id
, webpage
, long_video_id
, key
) 
  83                 status 
= 'COMING_SOON' 
  85         if status 
== 'LIVE_END': 
  86             raise ExtractorError('Uploading for replay. Please wait...', 
  88         elif status 
== 'COMING_SOON': 
  89             raise ExtractorError('Coming soon!', expected
=True) 
  90         elif status 
== 'CANCELED': 
  91             raise ExtractorError('We are sorry, ' 
  92                                  'but the live broadcast has been canceled.', 
  95             raise ExtractorError('Unknown status %s' % status
) 
  97     def _get_common_fields(self
, webpage
): 
  98         title 
= self
._og
_search
_title
(webpage
) 
  99         creator 
= self
._html
_search
_regex
( 
 100             r
'<div[^>]+class="info_area"[^>]*>\s*<a\s+[^>]*>([^<]+)', 
 101             webpage
, 'creator', fatal
=False) 
 102         thumbnail 
= self
._og
_search
_thumbnail
(webpage
) 
 106             'thumbnail': thumbnail
, 
 109     def _live(self
, video_id
, webpage
): 
 110         init_page 
= self
._download
_webpage
( 
 111             'http://www.vlive.tv/video/init/view', 
 112             video_id
, note
='Downloading live webpage', 
 113             data
=urlencode_postdata({'videoSeq': video_id
}), 
 115                 'Referer': 'http://www.vlive.tv/video/%s' % video_id
, 
 116                 'Content-Type': 'application/x-www-form-urlencoded' 
 119         live_params 
= self
._search
_regex
( 
 120             r
'"liveStreamInfo"\s*:\s*(".*"),', 
 121             init_page
, 'live stream info') 
 122         live_params 
= self
._parse
_json
(live_params
, video_id
) 
 123         live_params 
= self
._parse
_json
(live_params
, video_id
) 
 126         for vid 
in live_params
.get('resolutions', []): 
 127             formats
.extend(self
._extract
_m
3u8_formats
( 
 128                 vid
['cdnUrl'], video_id
, 'mp4', 
 129                 m3u8_id
=vid
.get('name'), 
 130                 fatal
=False, live
=True)) 
 131         self
._sort
_formats
(formats
) 
 133         info 
= self
._get
_common
_fields
(webpage
) 
 135             'title': self
._live
_title
(info
['title']), 
 142     def _replay(self
, video_id
, webpage
, long_video_id
, key
): 
 143         playinfo 
= self
._download
_json
( 
 144             'http://global.apis.naver.com/rmcnmv/rmcnmv/vod_play_videoInfo.json?%s' 
 145             % compat_urllib_parse_urlencode({ 
 146                 'videoId': long_video_id
, 
 149                 'doct': 'json',  # document type (xml or json) 
 150                 'cpt': 'vtt',  # captions type (vtt or ttml) 
 154             'url': vid
['source'], 
 155             'format_id': vid
.get('encodingOption', {}).get('name'), 
 156             'abr': float_or_none(vid
.get('bitrate', {}).get('audio')), 
 157             'vbr': float_or_none(vid
.get('bitrate', {}).get('video')), 
 158             'width': int_or_none(vid
.get('encodingOption', {}).get('width')), 
 159             'height': int_or_none(vid
.get('encodingOption', {}).get('height')), 
 160             'filesize': int_or_none(vid
.get('size')), 
 161         } for vid 
in playinfo
.get('videos', {}).get('list', []) if vid
.get('source')] 
 162         self
._sort
_formats
(formats
) 
 164         view_count 
= int_or_none(playinfo
.get('meta', {}).get('count')) 
 167         for caption 
in playinfo
.get('captions', {}).get('list', []): 
 168             lang 
= dict_get(caption
, ('locale', 'language', 'country', 'label')) 
 169             if lang 
and caption
.get('source'): 
 172                     'url': caption
['source']}] 
 174         info 
= self
._get
_common
_fields
(webpage
) 
 178             'view_count': view_count
, 
 179             'subtitles': subtitles
, 
 184 class VLiveChannelIE(InfoExtractor
): 
 185     IE_NAME 
= 'vlive:channel' 
 186     _VALID_URL 
= r
'https?://channels\.vlive\.tv/(?P<id>[0-9A-Z]+)' 
 188         'url': 'http://channels.vlive.tv/FCD4B', 
 193         'playlist_mincount': 110 
 195     _APP_ID 
= '8c6cc7b45d2568fb668be6e05b6e5a3b' 
 197     def _real_extract(self
, url
): 
 198         channel_code 
= self
._match
_id
(url
) 
 200         webpage 
= self
._download
_webpage
( 
 201             'http://channels.vlive.tv/%s/video' % channel_code
, channel_code
) 
 205         app_js_url 
= self
._search
_regex
( 
 206             r
'<script[^>]+src=(["\'])(?P
<url
>http
.+?
/app\
.js
.*?
)\
1', 
 207             webpage, 'app js
', default=None, group='url
') 
 210             app_js = self._download_webpage( 
 211                 app_js_url, channel_code, 'Downloading app JS
', fatal=False) 
 213                 app_id = self._search_regex( 
 214                     r'Global\
.VFAN_APP_ID\s
*=\s
*[\'"]([^\'"]+)[\'"]', 
 215                     app_js, 'app id', default=None) 
 217         app_id = app_id or self._APP_ID 
 219         channel_info = self._download_json( 
 220             'http://api.vfan.vlive.tv/vproxy/channelplus/decodeChannelCode', 
 221             channel_code, note='Downloading decode channel code', 
 224                 'channelCode': channel_code, 
 225                 '_': int(time.time()) 
 228         channel_seq = channel_info['result']['channelSeq'] 
 232         for page_num in itertools.count(1): 
 233             video_list = self._download_json( 
 234                 'http://api.vfan.vlive.tv/vproxy/channelplus/getChannelVideoList', 
 235                 channel_code, note='Downloading channel list page #%d' % page_num, 
 238                     'channelSeq': channel_seq, 
 239                     # Large values of maxNumOfRows (~300 or above) may cause 
 240                     # empty responses (see [1]), e.g. this happens for [2] that 
 241                     # has more than 300 videos. 
 242                     # 1. https://github.com/rg3/youtube-dl/issues/13830 
 243                     # 2. http://channels.vlive.tv/EDBF. 
 245                     '_': int(time.time()), 
 251                 channel_name = try_get( 
 253                     lambda x: x['result']['channelInfo']['channelName'], 
 257                 video_list, lambda x: x['result']['videoList'], list) 
 262                 video_id = video.get('videoSeq') 
 265                 video_id = compat_str(video_id) 
 268                         'http://www.vlive.tv/video/%s' % video_id, 
 269                         ie=VLiveIE.ie_key(), video_id=video_id)) 
 271         return self.playlist_result( 
 272             entries, channel_code, channel_name) 
 275 class VLivePlaylistIE(InfoExtractor): 
 276     IE_NAME = 'vlive:playlist' 
 277     _VALID_URL = r'https?://(?:(?:www|m)\.)?vlive\.tv/video/(?P<video_id>[0-9]+)/playlist/(?P<id>[0-9]+)' 
 279         'url': 'http://www.vlive.tv/video/22867/playlist/22912', 
 282             'title': 'Valentine Day Message from TWICE' 
 284         'playlist_mincount': 9 
 287     def _real_extract(self, url): 
 288         mobj = re.match(self._VALID_URL, url) 
 289         video_id, playlist_id = mobj.group('video_id', 'id') 
 291         VIDEO_URL_TEMPLATE = 'http://www.vlive.tv/video/%s' 
 292         if self._downloader.params.get('noplaylist'): 
 294                 'Downloading just video %s because of --no-playlist' % video_id) 
 295             return self.url_result( 
 296                 VIDEO_URL_TEMPLATE % video_id, 
 297                 ie=VLiveIE.ie_key(), video_id=video_id) 
 300             'Downloading playlist %s - add --no-playlist to just download video' 
 303         webpage = self._download_webpage( 
 304             'http://www.vlive.tv/video/%s/playlist/%s' 
 305             % (video_id, playlist_id), playlist_id) 
 307         item_ids = self._parse_json( 
 309                 r'playlistVideoSeqs\s*=\s*(\[[^]]+\])', webpage, 
 310                 'playlist video seqs'), 
 315                 VIDEO_URL_TEMPLATE % item_id, ie=VLiveIE.ie_key(), 
 316                 video_id=compat_str(item_id)) 
 317             for item_id in item_ids] 
 319         playlist_name = self._html_search_regex( 
 320             r'<div[^>]+class="[^
"]*multicam_playlist[^>]*>\s*<h3[^>]+>([^<]+)', 
 321             webpage, 'playlist title', fatal=False) 
 323         return self.playlist_result(entries, playlist_id, playlist_name)