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]+)' 
  27     _NETRC_MACHINE 
= 'vlive' 
  29         'url': 'http://www.vlive.tv/video/1326', 
  30         'md5': 'cc7314812855ce56de70a06a27314983', 
  34             'title': "[V LIVE] Girl's Day's Broadcast", 
  35             'creator': "Girl's Day", 
  39         'url': 'http://www.vlive.tv/video/16937', 
  43             'title': '[V LIVE] 첸백시 걍방', 
  46             'subtitles': 'mincount:12', 
  49             'skip_download': True, 
  52         'url': 'https://www.vlive.tv/video/129100', 
  53         'md5': 'ca2569453b79d66e5b919e5d308bff6b', 
  57             'title': '[V LIVE] [BTS+] Run BTS! 2019 - EP.71 :: Behind the scene', 
  60             'subtitles': 'mincount:10', 
  62         'skip': 'This video is only available for CH+ subscribers', 
  66     def suitable(cls
, url
): 
  67         return False if VLivePlaylistIE
.suitable(url
) else super(VLiveIE
, cls
).suitable(url
) 
  69     def _real_initialize(self
): 
  73         email
, password 
= self
._get
_login
_info
() 
  74         if None in (email
, password
): 
  78             login_info 
= self
._download
_json
( 
  79                 'https://www.vlive.tv/auth/loginInfo', None, 
  80                 note
='Downloading login info', 
  81                 headers
={'Referer': 'https://www.vlive.tv/home'}) 
  83                 login_info
, lambda x
: x
['message']['login'], bool) or False 
  85         LOGIN_URL 
= 'https://www.vlive.tv/auth/email/login' 
  86         self
._request
_webpage
( 
  87             LOGIN_URL
, None, note
='Downloading login cookies') 
  89         self
._download
_webpage
( 
  90             LOGIN_URL
, None, note
='Logging in', 
  91             data
=urlencode_postdata({'email': email
, 'pwd': password
}), 
  94                 'Content-Type': 'application/x-www-form-urlencoded' 
  97         if not is_logged_in(): 
  98             raise ExtractorError('Unable to log in', expected
=True) 
 100     def _real_extract(self
, url
): 
 101         video_id 
= self
._match
_id
(url
) 
 103         webpage 
= self
._download
_webpage
( 
 104             'https://www.vlive.tv/video/%s' % video_id
, video_id
) 
 106         VIDEO_PARAMS_RE 
= r
'\bvlive\.video\.init\(([^)]+)' 
 107         VIDEO_PARAMS_FIELD 
= 'video params' 
 109         params 
= self
._parse
_json
(self
._search
_regex
( 
 110             VIDEO_PARAMS_RE
, webpage
, VIDEO_PARAMS_FIELD
, default
=''), video_id
, 
 111             transform_source
=lambda s
: '[' + s 
+ ']', fatal
=False) 
 113         if not params 
or len(params
) < 7: 
 114             params 
= self
._search
_regex
( 
 115                 VIDEO_PARAMS_RE
, webpage
, VIDEO_PARAMS_FIELD
) 
 116             params 
= [p
.strip(r
'"') for p 
in re
.split(r
'\s*,\s*', params
)] 
 118         status
, long_video_id
, key 
= params
[2], params
[5], params
[6] 
 119         status 
= remove_start(status
, 'PRODUCT_') 
 121         if status 
in ('LIVE_ON_AIR', 'BIG_EVENT_ON_AIR'): 
 122             return self
._live
(video_id
, webpage
) 
 123         elif status 
in ('VOD_ON_AIR', 'BIG_EVENT_INTRO'): 
 124             return self
._replay
(video_id
, webpage
, long_video_id
, key
) 
 126         if status 
== 'LIVE_END': 
 127             raise ExtractorError('Uploading for replay. Please wait...', 
 129         elif status 
== 'COMING_SOON': 
 130             raise ExtractorError('Coming soon!', expected
=True) 
 131         elif status 
== 'CANCELED': 
 132             raise ExtractorError('We are sorry, ' 
 133                                  'but the live broadcast has been canceled.', 
 135         elif status 
== 'ONLY_APP': 
 136             raise ExtractorError('Unsupported video type', expected
=True) 
 138             raise ExtractorError('Unknown status %s' % status
) 
 140     def _get_common_fields(self
, webpage
): 
 141         title 
= self
._og
_search
_title
(webpage
) 
 142         creator 
= self
._html
_search
_regex
( 
 143             r
'<div[^>]+class="info_area"[^>]*>\s*(?:<em[^>]*>.*?</em\s*>\s*)?<a\s+[^>]*>([^<]+)', 
 144             webpage
, 'creator', fatal
=False) 
 145         thumbnail 
= self
._og
_search
_thumbnail
(webpage
) 
 149             'thumbnail': thumbnail
, 
 152     def _live(self
, video_id
, webpage
): 
 153         init_page 
= self
._download
_init
_page
(video_id
) 
 155         live_params 
= self
._search
_regex
( 
 156             r
'"liveStreamInfo"\s*:\s*(".*"),', 
 157             init_page
, 'live stream info') 
 158         live_params 
= self
._parse
_json
(live_params
, video_id
) 
 159         live_params 
= self
._parse
_json
(live_params
, video_id
) 
 162         for vid 
in live_params
.get('resolutions', []): 
 163             formats
.extend(self
._extract
_m
3u8_formats
( 
 164                 vid
['cdnUrl'], video_id
, 'mp4', 
 165                 m3u8_id
=vid
.get('name'), 
 166                 fatal
=False, live
=True)) 
 167         self
._sort
_formats
(formats
) 
 169         info 
= self
._get
_common
_fields
(webpage
) 
 171             'title': self
._live
_title
(info
['title']), 
 178     def _replay(self
, video_id
, webpage
, long_video_id
, key
): 
 179         if '' in (long_video_id
, key
): 
 180             init_page 
= self
._download
_init
_page
(video_id
) 
 181             video_info 
= self
._parse
_json
(self
._search
_regex
( 
 182                 (r
'(?s)oVideoStatus\s*=\s*({.+?})\s*</script', 
 183                  r
'(?s)oVideoStatus\s*=\s*({.+})'), init_page
, 'video info'), 
 185             if video_info
.get('status') == 'NEED_CHANNEL_PLUS': 
 186                 self
.raise_login_required( 
 187                     'This video is only available for CH+ subscribers') 
 188             long_video_id
, key 
= video_info
['vid'], video_info
['inkey'] 
 190         playinfo 
= self
._download
_json
( 
 191             'http://global.apis.naver.com/rmcnmv/rmcnmv/vod_play_videoInfo.json?%s' 
 192             % compat_urllib_parse_urlencode({ 
 193                 'videoId': long_video_id
, 
 196                 'doct': 'json',  # document type (xml or json) 
 197                 'cpt': 'vtt',  # captions type (vtt or ttml) 
 201             'url': vid
['source'], 
 202             'format_id': vid
.get('encodingOption', {}).get('name'), 
 203             'abr': float_or_none(vid
.get('bitrate', {}).get('audio')), 
 204             'vbr': float_or_none(vid
.get('bitrate', {}).get('video')), 
 205             'width': int_or_none(vid
.get('encodingOption', {}).get('width')), 
 206             'height': int_or_none(vid
.get('encodingOption', {}).get('height')), 
 207             'filesize': int_or_none(vid
.get('size')), 
 208         } for vid 
in playinfo
.get('videos', {}).get('list', []) if vid
.get('source')] 
 209         self
._sort
_formats
(formats
) 
 211         view_count 
= int_or_none(playinfo
.get('meta', {}).get('count')) 
 214         for caption 
in playinfo
.get('captions', {}).get('list', []): 
 215             lang 
= dict_get(caption
, ('locale', 'language', 'country', 'label')) 
 216             if lang 
and caption
.get('source'): 
 219                     'url': caption
['source']}] 
 221         info 
= self
._get
_common
_fields
(webpage
) 
 225             'view_count': view_count
, 
 226             'subtitles': subtitles
, 
 230     def _download_init_page(self
, video_id
): 
 231         return self
._download
_webpage
( 
 232             'https://www.vlive.tv/video/init/view', 
 233             video_id
, note
='Downloading live webpage', 
 234             data
=urlencode_postdata({'videoSeq': video_id
}), 
 236                 'Referer': 'https://www.vlive.tv/video/%s' % video_id
, 
 237                 'Content-Type': 'application/x-www-form-urlencoded' 
 241 class VLiveChannelIE(InfoExtractor
): 
 242     IE_NAME 
= 'vlive:channel' 
 243     _VALID_URL 
= r
'https?://channels\.vlive\.tv/(?P<id>[0-9A-Z]+)' 
 245         'url': 'http://channels.vlive.tv/FCD4B', 
 250         'playlist_mincount': 110 
 252     _APP_ID 
= '8c6cc7b45d2568fb668be6e05b6e5a3b' 
 254     def _real_extract(self
, url
): 
 255         channel_code 
= self
._match
_id
(url
) 
 257         webpage 
= self
._download
_webpage
( 
 258             'http://channels.vlive.tv/%s/video' % channel_code
, channel_code
) 
 262         app_js_url 
= self
._search
_regex
( 
 263             r
'<script[^>]+src=(["\'])(?P
<url
>http
.+?
/app\
.js
.*?
)\
1', 
 264             webpage, 'app js
', default=None, group='url
') 
 267             app_js = self._download_webpage( 
 268                 app_js_url, channel_code, 'Downloading app JS
', fatal=False) 
 270                 app_id = self._search_regex( 
 271                     r'Global\
.VFAN_APP_ID\s
*=\s
*[\'"]([^\'"]+)[\'"]', 
 272                     app_js, 'app id', default=None) 
 274         app_id = app_id or self._APP_ID 
 276         channel_info = self._download_json( 
 277             'http://api.vfan.vlive.tv/vproxy/channelplus/decodeChannelCode', 
 278             channel_code, note='Downloading decode channel code', 
 281                 'channelCode': channel_code, 
 282                 '_': int(time.time()) 
 285         channel_seq = channel_info['result']['channelSeq'] 
 289         for page_num in itertools.count(1): 
 290             video_list = self._download_json( 
 291                 'http://api.vfan.vlive.tv/vproxy/channelplus/getChannelVideoList', 
 292                 channel_code, note='Downloading channel list page #%d' % page_num, 
 295                     'channelSeq': channel_seq, 
 296                     # Large values of maxNumOfRows (~300 or above) may cause 
 297                     # empty responses (see [1]), e.g. this happens for [2] that 
 298                     # has more than 300 videos. 
 299                     # 1. https://github.com/ytdl-org/youtube-dl/issues/13830 
 300                     # 2. http://channels.vlive.tv/EDBF. 
 302                     '_': int(time.time()), 
 308                 channel_name = try_get( 
 310                     lambda x: x['result']['channelInfo']['channelName'], 
 314                 video_list, lambda x: x['result']['videoList'], list) 
 319                 video_id = video.get('videoSeq') 
 322                 video_id = compat_str(video_id) 
 325                         'http://www.vlive.tv/video/%s' % video_id, 
 326                         ie=VLiveIE.ie_key(), video_id=video_id)) 
 328         return self.playlist_result( 
 329             entries, channel_code, channel_name) 
 332 class VLivePlaylistIE(InfoExtractor): 
 333     IE_NAME = 'vlive:playlist' 
 334     _VALID_URL = r'https?://(?:(?:www|m)\.)?vlive\.tv/video/(?P<video_id>[0-9]+)/playlist/(?P<id>[0-9]+)' 
 335     _VIDEO_URL_TEMPLATE = 'http://www.vlive.tv/video/%s' 
 337         # regular working playlist 
 338         'url': 'https://www.vlive.tv/video/117956/playlist/117963', 
 341             'title': '아이돌룸(IDOL ROOM) 41회 - (여자)아이들' 
 343         'playlist_mincount': 10 
 345         # playlist with no playlistVideoSeqs 
 346         'url': 'http://www.vlive.tv/video/22867/playlist/22912', 
 350             'title': '[V LIVE] Valentine Day Message from MINA', 
 355             'skip_download': True, 
 359     def _build_video_result(self, video_id, message): 
 360         self.to_screen(message) 
 361         return self.url_result( 
 362             self._VIDEO_URL_TEMPLATE % video_id, 
 363             ie=VLiveIE.ie_key(), video_id=video_id) 
 365     def _real_extract(self, url): 
 366         mobj = re.match(self._VALID_URL, url) 
 367         video_id, playlist_id = mobj.group('video_id', 'id') 
 369         if self._downloader.params.get('noplaylist'): 
 370             return self._build_video_result( 
 372                 'Downloading just video %s because of --no-playlist' 
 376             'Downloading playlist %s - add --no-playlist to just download video' 
 379         webpage = self._download_webpage( 
 380             'http://www.vlive.tv/video/%s/playlist/%s' 
 381             % (video_id, playlist_id), playlist_id) 
 383         raw_item_ids = self._search_regex( 
 384             r'playlistVideoSeqs\s*=\s*(\[[^]]+\])', webpage, 
 385             'playlist video seqs', default=None, fatal=False) 
 388             return self._build_video_result( 
 390                 'Downloading just video %s because no playlist was found' 
 393         item_ids = self._parse_json(raw_item_ids, playlist_id) 
 397                 self._VIDEO_URL_TEMPLATE % item_id, ie=VLiveIE.ie_key(), 
 398                 video_id=compat_str(item_id)) 
 399             for item_id in item_ids] 
 401         playlist_name = self._html_search_regex( 
 402             r'<div[^>]+class="[^
"]*multicam_playlist[^>]*>\s*<h3[^>]+>([^<]+)', 
 403             webpage, 'playlist title', fatal=False) 
 405         return self.playlist_result(entries, playlist_id, playlist_name)