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