]>
jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/naver.py
3 from urllib
.parse
import urlparse
, parse_qs
5 from .common
import InfoExtractor
20 class NaverBaseIE(InfoExtractor
):
21 _CAPTION_EXT_RE
= r
'\.(?:ttml|vtt)'
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
,
29 meta
= video_data
['meta']
30 title
= meta
['subject']
32 get_list
= lambda x
: try_get(video_data
, lambda y
: y
[x
+ 's']['list'], list) or []
34 def extract_formats(streams
, stream_type
, query
={}):
35 for stream
in streams
:
36 stream_url
= stream
.get('source')
39 stream_url
= update_url_query(stream_url
, query
)
40 encoding_option
= stream
.get('encodingOption', {})
41 bitrate
= stream
.get('bitrate', {})
43 'format_id': '%s_%s' % (stream
.get('type') or stream_type
, dict_get(encoding_option
, ('name', 'id'))),
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,
54 extract_formats(get_list('video'), 'H264')
55 for stream_set
in video_data
.get('streams', []):
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')
62 extract_formats(videos
, stream_type
, query
)
63 elif stream_type
== 'HLS':
64 stream_url
= stream_set
.get('source')
67 formats
.extend(self
._extract
_m
3u8_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
)
72 replace_ext
= lambda x
, y
: re
.sub(self
._CAPTION
_EXT
_RE
, '.' + y
, x
)
74 def get_subs(caption_url
):
75 if re
.search(self
._CAPTION
_EXT
_RE
, caption_url
):
77 'url': replace_ext(caption_url
, 'ttml'),
79 'url': replace_ext(caption_url
, 'vtt'),
82 return [{'url': caption_url}
]
84 automatic_captions
= {}
86 for caption
in get_list('caption'):
87 caption_url
= caption
.get('source')
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
))
93 user
= meta
.get('user', {})
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'),
109 class NaverIE(NaverBaseIE
):
110 _VALID_URL
= r
'https?://(?:m\.)?tv(?:cast)?\.naver\.com/(?:v|embed)/(?P<id>\d+)'
113 'url': 'http://tv.naver.com/v/81652',
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',
125 'url': 'http://tv.naver.com/v/395837',
126 'md5': '8a38e35354d26a17f73f4e90094febd3',
130 'title': '9년이 지나도 아픈 기억, 전효성의 아버지',
131 'description': 'md5:eb6aca9d457b922e43860a2a2b1984d3',
132 'timestamp': 1432030253,
133 'upload_date': '20150519',
134 'uploader': '4가지쇼 시즌2',
135 'uploader_id': 'wrappinguser29',
137 'skip': 'Georestricted',
139 'url': 'http://tvcast.naver.com/v/81652',
140 'only_matching': True,
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 {}
151 vid
= current_clip
.get('videoId')
152 in_key
= current_clip
.get('inKey')
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
)
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,
172 class NaverLiveIE(InfoExtractor
):
173 IE_NAME
= 'Naver:live'
174 _VALID_URL
= r
'https?://(?:m\.)?tv(?:cast)?\.naver\.com/l/(?P<id>\d+)'
177 'url': 'https://tv.naver.com/l/52010',
181 'title': '[LIVE] 뉴스특보 : "수도권 거리두기, 2주간 2단계로 조정"',
182 'description': 'md5:df7f0c237a5ed5e786ce5c91efbeaab3',
183 'channel_id': 'NTV-ytnnews24-0',
184 'start_time': 1597026780000,
187 'url': 'https://tv.naver.com/l/51549',
191 'title': '연합뉴스TV - 코로나19 뉴스특보',
192 'description': 'md5:c655e82091bc21e413f549c0eaccc481',
193 'channel_id': 'NTV-yonhapnewstv-0',
194 'start_time': 1596406380000,
197 'url': 'https://tv.naver.com/l/54887',
198 'only_matching': True,
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')
206 info = self._extract_video_info(video_id, secure_url)
208 'description': self._og_search_description(page)
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')
218 if status == 'CLOSED':
219 raise ExtractorError('Stream is offline.', expected=True)
220 elif status != 'OPENED':
221 raise ExtractorError('Unknown status %s' % status)
223 title = meta.get('title')
224 stream_list = video_data.get('streams')
226 if stream_list is None:
227 raise ExtractorError('Could not get stream data.', expected=True)
230 for quality in stream_list:
231 if not quality.get('url'):
234 prop = quality.get('property')
235 if prop.get('abr'): # This abr doesn't mean Average audio bitrate.
238 formats.extend(self._extract_m3u8_formats(
239 quality.get('url'), video_id, 'mp4',
240 m3u8_id=quality.get('qualityId'), live=True
242 self._sort_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')],
257 class NaverNowIE(NaverBaseIE):
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'
262 'url': 'https://now.naver.com/s/now.4759?shareReplayId=26331132#replay=',
263 'md5': 'e05854162c21c221481de16b2944a0bc',
265 'id': '4759-26331132',
266 'title': '아이키X노제\r\n💖꽁냥꽁냥💖(1)',
268 'thumbnail': r're:^https?://.*\.jpg',
269 'timestamp': 1650369600,
270 'upload_date': '20220419',
271 'uploader_id': 'now',
273 'uploader_url': 'https://now.naver.com/show/4759',
274 'uploader': '아이키의 떰즈업',
280 'url': 'https://now.naver.com/s/now.4759?shareHightlight=26601461#highlight=',
281 'md5': '9f6118e398aa0f22b2152f554ea7851b',
283 'id': '4759-26601461',
284 'title': '아이키: 나 리정한테 흔들렸어,,, 질투 폭발하는 노제 여보😾 [아이키의 떰즈업]ㅣ네이버 NOW.',
286 'thumbnail': r're:^https?://.*\.jpg',
287 'upload_date': '20220504',
288 'timestamp': 1651648311,
289 'uploader_id': 'now',
291 'uploader_url': 'https://now.naver.com/show/4759',
292 'uploader': '아이키의 떰즈업',
298 'url': 'https://now.naver.com/s/now.4759',
303 'playlist_mincount': 101
305 'url': 'https://now.naver.com/s/now.4759?shareReplayId=26331132#replay',
310 'playlist_mincount': 101,
312 'url': 'https://now.naver.com/s/now.4759?shareHightlight=26601461#highlight=',
317 'playlist_mincount': 101,
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']
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))
330 def _extract_show_replays(self, show_id):
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'])
342 if len(show_vod_info.get('vod_list') or []) < page_size:
346 def _extract_show_highlights(self, show_id, highlight_id=None):
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}')
355 for highlight in highlights_videos.get('results') or []:
356 if highlight_id and highlight.get('clip_no') != int(highlight_id):
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']))
365 if len(highlights_videos.get('results') or []) < page_size:
369 def _extract_highlight(self, show_id, highlight_id):
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}')
375 def _real_extract(self, url):
376 show_id = self._match_id(url)
377 qs = parse_qs(urlparse(url).query)
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])
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}')
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'))