]>
Commit | Line | Data |
---|---|---|
061f62da | 1 | # coding: utf-8 |
25bcd355 | 2 | from __future__ import unicode_literals |
061f62da | 3 | |
b24d6336 | 4 | import re |
9d186afa | 5 | |
061f62da | 6 | from .common import InfoExtractor |
7 | from ..utils import ( | |
52f5889f | 8 | dict_get, |
9d186afa | 9 | ExtractorError, |
52f5889f S |
10 | float_or_none, |
11 | int_or_none, | |
345dec93 | 12 | remove_start, |
061f62da | 13 | ) |
15707c7e | 14 | from ..compat import compat_urllib_parse_urlencode |
061f62da | 15 | |
16 | ||
17 | class VLiveIE(InfoExtractor): | |
18 | IE_NAME = 'vlive' | |
52f5889f | 19 | _VALID_URL = r'https?://(?:(?:www|m)\.)?vlive\.tv/video/(?P<id>[0-9]+)' |
061f62da | 20 | _TEST = { |
b8b465af | 21 | 'url': 'http://www.vlive.tv/video/1326', |
061f62da | 22 | 'md5': 'cc7314812855ce56de70a06a27314983', |
23 | 'info_dict': { | |
24 | 'id': '1326', | |
25 | 'ext': 'mp4', | |
25bcd355 | 26 | 'title': "[V LIVE] Girl's Day's Broadcast", |
52f5889f S |
27 | 'creator': "Girl's Day", |
28 | 'view_count': int, | |
061f62da | 29 | }, |
30 | } | |
061f62da | 31 | |
32 | def _real_extract(self, url): | |
33 | video_id = self._match_id(url) | |
34 | ||
35 | webpage = self._download_webpage( | |
52f5889f | 36 | 'http://www.vlive.tv/video/%s' % video_id, video_id) |
061f62da | 37 | |
b24d6336 | 38 | video_params = self._search_regex( |
25bcd355 | 39 | r'\bvlive\.video\.init\(([^)]+)\)', |
b24d6336 | 40 | webpage, 'video params') |
25bcd355 KH |
41 | status, _, _, live_params, long_video_id, key = re.split( |
42 | r'"\s*,\s*"', video_params)[2:8] | |
345dec93 | 43 | status = remove_start(status, 'PRODUCT_') |
b24d6336 KH |
44 | |
45 | if status == 'LIVE_ON_AIR' or status == 'BIG_EVENT_ON_AIR': | |
46 | live_params = self._parse_json('"%s"' % live_params, video_id) | |
47 | live_params = self._parse_json(live_params, video_id) | |
48 | return self._live(video_id, webpage, live_params) | |
49 | elif status == 'VOD_ON_AIR' or status == 'BIG_EVENT_INTRO': | |
50 | if long_video_id and key: | |
51 | return self._replay(video_id, webpage, long_video_id, key) | |
b24d6336 KH |
52 | else: |
53 | status = 'COMING_SOON' | |
54 | ||
55 | if status == 'LIVE_END': | |
56 | raise ExtractorError('Uploading for replay. Please wait...', | |
57 | expected=True) | |
58 | elif status == 'COMING_SOON': | |
25bcd355 | 59 | raise ExtractorError('Coming soon!', expected=True) |
b24d6336 KH |
60 | elif status == 'CANCELED': |
61 | raise ExtractorError('We are sorry, ' | |
62 | 'but the live broadcast has been canceled.', | |
63 | expected=True) | |
64 | else: | |
65 | raise ExtractorError('Unknown status %s' % status) | |
66 | ||
67 | def _get_common_fields(self, webpage): | |
061f62da | 68 | title = self._og_search_title(webpage) |
b24d6336 KH |
69 | creator = self._html_search_regex( |
70 | r'<div[^>]+class="info_area"[^>]*>\s*<a\s+[^>]*>([^<]+)', | |
71 | webpage, 'creator', fatal=False) | |
72 | thumbnail = self._og_search_thumbnail(webpage) | |
73 | return { | |
74 | 'title': title, | |
75 | 'creator': creator, | |
76 | 'thumbnail': thumbnail, | |
77 | } | |
08354db4 | 78 | |
b24d6336 KH |
79 | def _live(self, video_id, webpage, live_params): |
80 | formats = [] | |
81 | for vid in live_params.get('resolutions', []): | |
82 | formats.extend(self._extract_m3u8_formats( | |
83 | vid['cdnUrl'], video_id, 'mp4', | |
84 | m3u8_id=vid.get('name'), | |
85 | fatal=False, live=True)) | |
86 | self._sort_formats(formats) | |
87 | ||
88 | return dict(self._get_common_fields(webpage), | |
9d186afa YCH |
89 | id=video_id, |
90 | formats=formats, | |
91 | is_live=True) | |
b24d6336 KH |
92 | |
93 | def _replay(self, video_id, webpage, long_video_id, key): | |
52f5889f S |
94 | playinfo = self._download_json( |
95 | 'http://global.apis.naver.com/rmcnmv/rmcnmv/vod_play_videoInfo.json?%s' | |
15707c7e | 96 | % compat_urllib_parse_urlencode({ |
52f5889f S |
97 | 'videoId': long_video_id, |
98 | 'key': key, | |
99 | 'ptc': 'http', | |
100 | 'doct': 'json', # document type (xml or json) | |
101 | 'cpt': 'vtt', # captions type (vtt or ttml) | |
102 | }), video_id) | |
061f62da | 103 | |
52f5889f S |
104 | formats = [{ |
105 | 'url': vid['source'], | |
106 | 'format_id': vid.get('encodingOption', {}).get('name'), | |
107 | 'abr': float_or_none(vid.get('bitrate', {}).get('audio')), | |
108 | 'vbr': float_or_none(vid.get('bitrate', {}).get('video')), | |
109 | 'width': int_or_none(vid.get('encodingOption', {}).get('width')), | |
110 | 'height': int_or_none(vid.get('encodingOption', {}).get('height')), | |
111 | 'filesize': int_or_none(vid.get('size')), | |
112 | } for vid in playinfo.get('videos', {}).get('list', []) if vid.get('source')] | |
061f62da | 113 | self._sort_formats(formats) |
114 | ||
52f5889f S |
115 | view_count = int_or_none(playinfo.get('meta', {}).get('count')) |
116 | ||
061f62da | 117 | subtitles = {} |
b8b465af | 118 | for caption in playinfo.get('captions', {}).get('list', []): |
52f5889f S |
119 | lang = dict_get(caption, ('language', 'locale', 'country', 'label')) |
120 | if lang and caption.get('source'): | |
121 | subtitles[lang] = [{ | |
122 | 'ext': 'vtt', | |
123 | 'url': caption['source']}] | |
061f62da | 124 | |
b24d6336 | 125 | return dict(self._get_common_fields(webpage), |
9d186afa YCH |
126 | id=video_id, |
127 | formats=formats, | |
128 | view_count=view_count, | |
129 | subtitles=subtitles) |