]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/naver.py
[extractor/nebula] Add nebula.tv (#4918)
[yt-dlp.git] / yt_dlp / extractor / naver.py
1 import itertools
2 import re
3 from urllib.parse import urlparse, parse_qs
4
5 from .common import InfoExtractor
6 from ..utils import (
7 ExtractorError,
8 clean_html,
9 dict_get,
10 int_or_none,
11 merge_dicts,
12 parse_duration,
13 traverse_obj,
14 try_get,
15 unified_timestamp,
16 update_url_query,
17 )
18
19
20 class NaverBaseIE(InfoExtractor):
21 _CAPTION_EXT_RE = r'\.(?:ttml|vtt)'
22
23 def _extract_video_info(self, video_id, vid, key):
24 video_data = self._download_json(
25 'http://play.rmcnmv.naver.com/vod/play/v2.0/' + vid,
26 video_id, query={
27 'key': key,
28 })
29 meta = video_data['meta']
30 title = meta['subject']
31 formats = []
32 get_list = lambda x: try_get(video_data, lambda y: y[x + 's']['list'], list) or []
33
34 def extract_formats(streams, stream_type, query={}):
35 for stream in streams:
36 stream_url = stream.get('source')
37 if not stream_url:
38 continue
39 stream_url = update_url_query(stream_url, query)
40 encoding_option = stream.get('encodingOption', {})
41 bitrate = stream.get('bitrate', {})
42 formats.append({
43 'format_id': '%s_%s' % (stream.get('type') or stream_type, dict_get(encoding_option, ('name', 'id'))),
44 'url': stream_url,
45 'ext': 'mp4',
46 'width': int_or_none(encoding_option.get('width')),
47 'height': int_or_none(encoding_option.get('height')),
48 'vbr': int_or_none(bitrate.get('video')),
49 'abr': int_or_none(bitrate.get('audio')),
50 'filesize': int_or_none(stream.get('size')),
51 'protocol': 'm3u8_native' if stream_type == 'HLS' else None,
52 })
53
54 extract_formats(get_list('video'), 'H264')
55 for stream_set in video_data.get('streams', []):
56 query = {}
57 for param in stream_set.get('keys', []):
58 query[param['name']] = param['value']
59 stream_type = stream_set.get('type')
60 videos = stream_set.get('videos')
61 if videos:
62 extract_formats(videos, stream_type, query)
63 elif stream_type == 'HLS':
64 stream_url = stream_set.get('source')
65 if not stream_url:
66 continue
67 formats.extend(self._extract_m3u8_formats(
68 update_url_query(stream_url, query), video_id,
69 'mp4', 'm3u8_native', m3u8_id=stream_type, fatal=False))
70 self._sort_formats(formats)
71
72 replace_ext = lambda x, y: re.sub(self._CAPTION_EXT_RE, '.' + y, x)
73
74 def get_subs(caption_url):
75 if re.search(self._CAPTION_EXT_RE, caption_url):
76 return [{
77 'url': replace_ext(caption_url, 'ttml'),
78 }, {
79 'url': replace_ext(caption_url, 'vtt'),
80 }]
81 else:
82 return [{'url': caption_url}]
83
84 automatic_captions = {}
85 subtitles = {}
86 for caption in get_list('caption'):
87 caption_url = caption.get('source')
88 if not caption_url:
89 continue
90 sub_dict = automatic_captions if caption.get('type') == 'auto' else subtitles
91 sub_dict.setdefault(dict_get(caption, ('locale', 'language')), []).extend(get_subs(caption_url))
92
93 user = meta.get('user', {})
94
95 return {
96 'id': video_id,
97 'title': title,
98 'formats': formats,
99 'subtitles': subtitles,
100 'automatic_captions': automatic_captions,
101 'thumbnail': try_get(meta, lambda x: x['cover']['source']),
102 'view_count': int_or_none(meta.get('count')),
103 'uploader_id': user.get('id'),
104 'uploader': user.get('name'),
105 'uploader_url': user.get('url'),
106 }
107
108
109 class NaverIE(NaverBaseIE):
110 _VALID_URL = r'https?://(?:m\.)?tv(?:cast)?\.naver\.com/(?:v|embed)/(?P<id>\d+)'
111 _GEO_BYPASS = False
112 _TESTS = [{
113 'url': 'http://tv.naver.com/v/81652',
114 'info_dict': {
115 'id': '81652',
116 'ext': 'mp4',
117 'title': '[9월 모의고사 해설강의][수학_김상희] 수학 A형 16~20번',
118 'description': '메가스터디 수학 김상희 선생님이 9월 모의고사 수학A형 16번에서 20번까지 해설강의를 공개합니다.',
119 'timestamp': 1378200754,
120 'upload_date': '20130903',
121 'uploader': '메가스터디, 합격불변의 법칙',
122 'uploader_id': 'megastudy',
123 },
124 }, {
125 'url': 'http://tv.naver.com/v/395837',
126 'md5': '8a38e35354d26a17f73f4e90094febd3',
127 'info_dict': {
128 'id': '395837',
129 'ext': 'mp4',
130 'title': '9년이 지나도 아픈 기억, 전효성의 아버지',
131 'description': 'md5:eb6aca9d457b922e43860a2a2b1984d3',
132 'timestamp': 1432030253,
133 'upload_date': '20150519',
134 'uploader': '4가지쇼 시즌2',
135 'uploader_id': 'wrappinguser29',
136 },
137 'skip': 'Georestricted',
138 }, {
139 'url': 'http://tvcast.naver.com/v/81652',
140 'only_matching': True,
141 }]
142
143 def _real_extract(self, url):
144 video_id = self._match_id(url)
145 content = self._download_json(
146 'https://tv.naver.com/api/json/v/' + video_id,
147 video_id, headers=self.geo_verification_headers())
148 player_info_json = content.get('playerInfoJson') or {}
149 current_clip = player_info_json.get('currentClip') or {}
150
151 vid = current_clip.get('videoId')
152 in_key = current_clip.get('inKey')
153
154 if not vid or not in_key:
155 player_auth = try_get(player_info_json, lambda x: x['playerOption']['auth'])
156 if player_auth == 'notCountry':
157 self.raise_geo_restricted(countries=['KR'])
158 elif player_auth == 'notLogin':
159 self.raise_login_required()
160 raise ExtractorError('couldn\'t extract vid and key')
161 info = self._extract_video_info(video_id, vid, in_key)
162 info.update({
163 'description': clean_html(current_clip.get('description')),
164 'timestamp': int_or_none(current_clip.get('firstExposureTime'), 1000),
165 'duration': parse_duration(current_clip.get('displayPlayTime')),
166 'like_count': int_or_none(current_clip.get('recommendPoint')),
167 'age_limit': 19 if current_clip.get('adult') else None,
168 })
169 return info
170
171
172 class NaverLiveIE(InfoExtractor):
173 IE_NAME = 'Naver:live'
174 _VALID_URL = r'https?://(?:m\.)?tv(?:cast)?\.naver\.com/l/(?P<id>\d+)'
175 _GEO_BYPASS = False
176 _TESTS = [{
177 'url': 'https://tv.naver.com/l/52010',
178 'info_dict': {
179 'id': '52010',
180 'ext': 'mp4',
181 'title': '[LIVE] 뉴스특보 : "수도권 거리두기, 2주간 2단계로 조정"',
182 'description': 'md5:df7f0c237a5ed5e786ce5c91efbeaab3',
183 'channel_id': 'NTV-ytnnews24-0',
184 'start_time': 1597026780000,
185 },
186 }, {
187 'url': 'https://tv.naver.com/l/51549',
188 'info_dict': {
189 'id': '51549',
190 'ext': 'mp4',
191 'title': '연합뉴스TV - 코로나19 뉴스특보',
192 'description': 'md5:c655e82091bc21e413f549c0eaccc481',
193 'channel_id': 'NTV-yonhapnewstv-0',
194 'start_time': 1596406380000,
195 },
196 }, {
197 'url': 'https://tv.naver.com/l/54887',
198 'only_matching': True,
199 }]
200
201 def _real_extract(self, url):
202 video_id = self._match_id(url)
203 page = self._download_webpage(url, video_id, 'Downloading Page', 'Unable to download Page')
204 secure_url = self._search_regex(r'sApiF:\s+(?:"|\')([^"\']+)', page, 'secureurl')
205
206 info = self._extract_video_info(video_id, secure_url)
207 info.update({
208 'description': self._og_search_description(page)
209 })
210
211 return info
212
213 def _extract_video_info(self, video_id, url):
214 video_data = self._download_json(url, video_id, headers=self.geo_verification_headers())
215 meta = video_data.get('meta')
216 status = meta.get('status')
217
218 if status == 'CLOSED':
219 raise ExtractorError('Stream is offline.', expected=True)
220 elif status != 'OPENED':
221 raise ExtractorError('Unknown status %s' % status)
222
223 title = meta.get('title')
224 stream_list = video_data.get('streams')
225
226 if stream_list is None:
227 raise ExtractorError('Could not get stream data.', expected=True)
228
229 formats = []
230 for quality in stream_list:
231 if not quality.get('url'):
232 continue
233
234 prop = quality.get('property')
235 if prop.get('abr'): # This abr doesn't mean Average audio bitrate.
236 continue
237
238 formats.extend(self._extract_m3u8_formats(
239 quality.get('url'), video_id, 'mp4',
240 m3u8_id=quality.get('qualityId'), live=True
241 ))
242 self._sort_formats(formats)
243
244 return {
245 'id': video_id,
246 'title': title,
247 'formats': formats,
248 'channel_id': meta.get('channelId'),
249 'channel_url': meta.get('channelUrl'),
250 'thumbnail': meta.get('imgUrl'),
251 'start_time': meta.get('startTime'),
252 'categories': [meta.get('categoryId')],
253 'is_live': True
254 }
255
256
257 class NaverNowIE(NaverBaseIE):
258 IE_NAME = 'navernow'
259 _VALID_URL = r'https?://now\.naver\.com/s/now\.(?P<id>[0-9]+)'
260 _API_URL = 'https://apis.naver.com/now_web/oldnow_web/v4'
261 _TESTS = [{
262 'url': 'https://now.naver.com/s/now.4759?shareReplayId=26331132#replay=',
263 'md5': 'e05854162c21c221481de16b2944a0bc',
264 'info_dict': {
265 'id': '4759-26331132',
266 'title': '아이키X노제\r\n💖꽁냥꽁냥💖(1)',
267 'ext': 'mp4',
268 'thumbnail': r're:^https?://.*\.jpg',
269 'timestamp': 1650369600,
270 'upload_date': '20220419',
271 'uploader_id': 'now',
272 'view_count': int,
273 'uploader_url': 'https://now.naver.com/show/4759',
274 'uploader': '아이키의 떰즈업',
275 },
276 'params': {
277 'noplaylist': True,
278 }
279 }, {
280 'url': 'https://now.naver.com/s/now.4759?shareHightlight=26601461#highlight=',
281 'md5': '9f6118e398aa0f22b2152f554ea7851b',
282 'info_dict': {
283 'id': '4759-26601461',
284 'title': '아이키: 나 리정한테 흔들렸어,,, 질투 폭발하는 노제 여보😾 [아이키의 떰즈업]ㅣ네이버 NOW.',
285 'ext': 'mp4',
286 'thumbnail': r're:^https?://.*\.jpg',
287 'upload_date': '20220504',
288 'timestamp': 1651648311,
289 'uploader_id': 'now',
290 'view_count': int,
291 'uploader_url': 'https://now.naver.com/show/4759',
292 'uploader': '아이키의 떰즈업',
293 },
294 'params': {
295 'noplaylist': True,
296 },
297 }, {
298 'url': 'https://now.naver.com/s/now.4759',
299 'info_dict': {
300 'id': '4759',
301 'title': '아이키의 떰즈업',
302 },
303 'playlist_mincount': 101
304 }, {
305 'url': 'https://now.naver.com/s/now.4759?shareReplayId=26331132#replay',
306 'info_dict': {
307 'id': '4759',
308 'title': '아이키의 떰즈업',
309 },
310 'playlist_mincount': 101,
311 }, {
312 'url': 'https://now.naver.com/s/now.4759?shareHightlight=26601461#highlight=',
313 'info_dict': {
314 'id': '4759',
315 'title': '아이키의 떰즈업',
316 },
317 'playlist_mincount': 101,
318 }]
319
320 def _extract_replay(self, show_id, replay_id):
321 vod_info = self._download_json(f'{self._API_URL}/shows/now.{show_id}/vod/{replay_id}', replay_id)
322 in_key = self._download_json(f'{self._API_URL}/shows/now.{show_id}/vod/{replay_id}/inkey', replay_id)['inKey']
323 return merge_dicts({
324 'id': f'{show_id}-{replay_id}',
325 'title': traverse_obj(vod_info, ('episode', 'title')),
326 'timestamp': unified_timestamp(traverse_obj(vod_info, ('episode', 'start_time'))),
327 'thumbnail': vod_info.get('thumbnail_image_url'),
328 }, self._extract_video_info(replay_id, vod_info['video_id'], in_key))
329
330 def _extract_show_replays(self, show_id):
331 page_size = 15
332 page = 1
333 while True:
334 show_vod_info = self._download_json(
335 f'{self._API_URL}/vod-shows/now.{show_id}', show_id,
336 query={'page': page, 'page_size': page_size},
337 note=f'Downloading JSON vod list for show {show_id} - page {page}'
338 )['response']['result']
339 for v in show_vod_info.get('vod_list') or []:
340 yield self._extract_replay(show_id, v['id'])
341
342 if len(show_vod_info.get('vod_list') or []) < page_size:
343 break
344 page += 1
345
346 def _extract_show_highlights(self, show_id, highlight_id=None):
347 page_size = 10
348 page = 1
349 while True:
350 highlights_videos = self._download_json(
351 f'{self._API_URL}/shows/now.{show_id}/highlights/videos/', show_id,
352 query={'page': page, 'page_size': page_size},
353 note=f'Downloading JSON highlights for show {show_id} - page {page}')
354
355 for highlight in highlights_videos.get('results') or []:
356 if highlight_id and highlight.get('clip_no') != int(highlight_id):
357 continue
358 yield merge_dicts({
359 'id': f'{show_id}-{highlight["clip_no"]}',
360 'title': highlight.get('title'),
361 'timestamp': unified_timestamp(highlight.get('regdate')),
362 'thumbnail': highlight.get('thumbnail_url'),
363 }, self._extract_video_info(highlight['clip_no'], highlight['video_id'], highlight['video_inkey']))
364
365 if len(highlights_videos.get('results') or []) < page_size:
366 break
367 page += 1
368
369 def _extract_highlight(self, show_id, highlight_id):
370 try:
371 return next(self._extract_show_highlights(show_id, highlight_id))
372 except StopIteration:
373 raise ExtractorError(f'Unable to find highlight {highlight_id} for show {show_id}')
374
375 def _real_extract(self, url):
376 show_id = self._match_id(url)
377 qs = parse_qs(urlparse(url).query)
378
379 if not self._yes_playlist(show_id, qs.get('shareHightlight')):
380 return self._extract_highlight(show_id, qs['shareHightlight'][0])
381 elif not self._yes_playlist(show_id, qs.get('shareReplayId')):
382 return self._extract_replay(show_id, qs['shareReplayId'][0])
383
384 show_info = self._download_json(
385 f'{self._API_URL}/shows/now.{show_id}/', show_id,
386 note=f'Downloading JSON vod list for show {show_id}')
387
388 return self.playlist_result(
389 itertools.chain(self._extract_show_replays(show_id), self._extract_show_highlights(show_id)),
390 show_id, show_info.get('title'))