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, 
  52     def _real_extract(self
, url
): 
  53         video_id 
= self
._match
_id
(url
) 
  55         webpage 
= self
._download
_webpage
( 
  56             'http://www.vlive.tv/video/%s' % video_id
, video_id
) 
  58         VIDEO_PARAMS_RE 
= r
'\bvlive\.video\.init\(([^)]+)' 
  59         VIDEO_PARAMS_FIELD 
= 'video params' 
  61         params 
= self
._parse
_json
(self
._search
_regex
( 
  62             VIDEO_PARAMS_RE
, webpage
, VIDEO_PARAMS_FIELD
, default
=''), video_id
, 
  63             transform_source
=lambda s
: '[' + s 
+ ']', fatal
=False) 
  65         if not params 
or len(params
) < 7: 
  66             params 
= self
._search
_regex
( 
  67                 VIDEO_PARAMS_RE
, webpage
, VIDEO_PARAMS_FIELD
) 
  68             params 
= [p
.strip(r
'"') for p 
in re
.split(r
'\s*,\s*', params
)] 
  70         status
, long_video_id
, key 
= params
[2], params
[5], params
[6] 
  71         status 
= remove_start(status
, 'PRODUCT_') 
  73         if status 
in ('LIVE_ON_AIR', 'BIG_EVENT_ON_AIR'): 
  74             return self
._live
(video_id
, webpage
) 
  75         elif status 
in ('VOD_ON_AIR', 'BIG_EVENT_INTRO'): 
  76             if long_video_id 
and key
: 
  77                 return self
._replay
(video_id
, webpage
, long_video_id
, key
) 
  79                 status 
= 'COMING_SOON' 
  81         if status 
== 'LIVE_END': 
  82             raise ExtractorError('Uploading for replay. Please wait...', 
  84         elif status 
== 'COMING_SOON': 
  85             raise ExtractorError('Coming soon!', expected
=True) 
  86         elif status 
== 'CANCELED': 
  87             raise ExtractorError('We are sorry, ' 
  88                                  'but the live broadcast has been canceled.', 
  91             raise ExtractorError('Unknown status %s' % status
) 
  93     def _get_common_fields(self
, webpage
): 
  94         title 
= self
._og
_search
_title
(webpage
) 
  95         creator 
= self
._html
_search
_regex
( 
  96             r
'<div[^>]+class="info_area"[^>]*>\s*<a\s+[^>]*>([^<]+)', 
  97             webpage
, 'creator', fatal
=False) 
  98         thumbnail 
= self
._og
_search
_thumbnail
(webpage
) 
 102             'thumbnail': thumbnail
, 
 105     def _live(self
, video_id
, webpage
): 
 106         init_page 
= self
._download
_webpage
( 
 107             'http://www.vlive.tv/video/init/view', 
 108             video_id
, note
='Downloading live webpage', 
 109             data
=urlencode_postdata({'videoSeq': video_id
}), 
 111                 'Referer': 'http://www.vlive.tv/video/%s' % video_id
, 
 112                 'Content-Type': 'application/x-www-form-urlencoded' 
 115         live_params 
= self
._search
_regex
( 
 116             r
'"liveStreamInfo"\s*:\s*(".*"),', 
 117             init_page
, 'live stream info') 
 118         live_params 
= self
._parse
_json
(live_params
, video_id
) 
 119         live_params 
= self
._parse
_json
(live_params
, video_id
) 
 122         for vid 
in live_params
.get('resolutions', []): 
 123             formats
.extend(self
._extract
_m
3u8_formats
( 
 124                 vid
['cdnUrl'], video_id
, 'mp4', 
 125                 m3u8_id
=vid
.get('name'), 
 126                 fatal
=False, live
=True)) 
 127         self
._sort
_formats
(formats
) 
 129         info 
= self
._get
_common
_fields
(webpage
) 
 131             'title': self
._live
_title
(info
['title']), 
 138     def _replay(self
, video_id
, webpage
, long_video_id
, key
): 
 139         playinfo 
= self
._download
_json
( 
 140             'http://global.apis.naver.com/rmcnmv/rmcnmv/vod_play_videoInfo.json?%s' 
 141             % compat_urllib_parse_urlencode({ 
 142                 'videoId': long_video_id
, 
 145                 'doct': 'json',  # document type (xml or json) 
 146                 'cpt': 'vtt',  # captions type (vtt or ttml) 
 150             'url': vid
['source'], 
 151             'format_id': vid
.get('encodingOption', {}).get('name'), 
 152             'abr': float_or_none(vid
.get('bitrate', {}).get('audio')), 
 153             'vbr': float_or_none(vid
.get('bitrate', {}).get('video')), 
 154             'width': int_or_none(vid
.get('encodingOption', {}).get('width')), 
 155             'height': int_or_none(vid
.get('encodingOption', {}).get('height')), 
 156             'filesize': int_or_none(vid
.get('size')), 
 157         } for vid 
in playinfo
.get('videos', {}).get('list', []) if vid
.get('source')] 
 158         self
._sort
_formats
(formats
) 
 160         view_count 
= int_or_none(playinfo
.get('meta', {}).get('count')) 
 163         for caption 
in playinfo
.get('captions', {}).get('list', []): 
 164             lang 
= dict_get(caption
, ('locale', 'language', 'country', 'label')) 
 165             if lang 
and caption
.get('source'): 
 168                     'url': caption
['source']}] 
 170         info 
= self
._get
_common
_fields
(webpage
) 
 174             'view_count': view_count
, 
 175             'subtitles': subtitles
, 
 180 class VLiveChannelIE(InfoExtractor
): 
 181     IE_NAME 
= 'vlive:channel' 
 182     _VALID_URL 
= r
'https?://channels\.vlive\.tv/(?P<id>[0-9A-Z]+)' 
 184         'url': 'http://channels.vlive.tv/FCD4B', 
 189         'playlist_mincount': 110 
 191     _APP_ID 
= '8c6cc7b45d2568fb668be6e05b6e5a3b' 
 193     def _real_extract(self
, url
): 
 194         channel_code 
= self
._match
_id
(url
) 
 196         webpage 
= self
._download
_webpage
( 
 197             'http://channels.vlive.tv/%s/video' % channel_code
, channel_code
) 
 201         app_js_url 
= self
._search
_regex
( 
 202             r
'<script[^>]+src=(["\'])(?P
<url
>http
.+?
/app\
.js
.*?
)\
1', 
 203             webpage, 'app js
', default=None, group='url
') 
 206             app_js = self._download_webpage( 
 207                 app_js_url, channel_code, 'Downloading app JS
', fatal=False) 
 209                 app_id = self._search_regex( 
 210                     r'Global\
.VFAN_APP_ID\s
*=\s
*[\'"]([^\'"]+)[\'"]', 
 211                     app_js, 'app id', default=None) 
 213         app_id = app_id or self._APP_ID 
 215         channel_info = self._download_json( 
 216             'http://api.vfan.vlive.tv/vproxy/channelplus/decodeChannelCode', 
 217             channel_code, note='Downloading decode channel code', 
 220                 'channelCode': channel_code, 
 221                 '_': int(time.time()) 
 224         channel_seq = channel_info['result']['channelSeq'] 
 228         for page_num in itertools.count(1): 
 229             video_list = self._download_json( 
 230                 'http://api.vfan.vlive.tv/vproxy/channelplus/getChannelVideoList', 
 231                 channel_code, note='Downloading channel list page #%d' % page_num, 
 234                     'channelSeq': channel_seq, 
 235                     'maxNumOfRows': 1000, 
 236                     '_': int(time.time()), 
 242                 channel_name = try_get( 
 244                     lambda x: x['result']['channelInfo']['channelName'], 
 248                 video_list, lambda x: x['result']['videoList'], list) 
 253                 video_id = video.get('videoSeq') 
 256                 video_id = compat_str(video_id) 
 259                         'http://www.vlive.tv/video/%s' % video_id, 
 260                         ie=VLiveIE.ie_key(), video_id=video_id)) 
 262         return self.playlist_result( 
 263             entries, channel_code, channel_name)