]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/naver.py
[naver] improve extraction
[yt-dlp.git] / youtube_dl / extractor / naver.py
CommitLineData
dcdb292f 1# coding: utf-8
24da5893
JMF
2from __future__ import unicode_literals
3
c88debff
RA
4import re
5
6b95b065 6from .common import InfoExtractor
1cc79574 7from ..utils import (
c88debff
RA
8 clean_html,
9 dict_get,
6b95b065 10 ExtractorError,
c88debff 11 get_element_by_class,
b02b960c 12 int_or_none,
c88debff 13 try_get,
b02b960c 14 update_url_query,
6b95b065
JMF
15)
16
17
c88debff
RA
18class NaverBaseIE(InfoExtractor):
19 _CAPTION_EXT_RE = r'\.(?:ttml|vtt)'
190f6c93 20
c88debff 21 def _extract_video_info(self, video_id, vid, key):
f65dc41b 22 video_data = self._download_json(
190f6c93 23 'http://play.rmcnmv.naver.com/vod/play/v2.0/' + vid,
f65dc41b 24 video_id, query={
c88debff 25 'key': key,
f65dc41b 26 })
b02b960c
RA
27 meta = video_data['meta']
28 title = meta['subject']
6b95b065 29 formats = []
c88debff 30 get_list = lambda x: try_get(video_data, lambda y: y[x + 's']['list'], list) or []
b02b960c
RA
31
32 def extract_formats(streams, stream_type, query={}):
33 for stream in streams:
34 stream_url = stream.get('source')
35 if not stream_url:
36 continue
37 stream_url = update_url_query(stream_url, query)
38 encoding_option = stream.get('encodingOption', {})
39 bitrate = stream.get('bitrate', {})
40 formats.append({
c88debff 41 'format_id': '%s_%s' % (stream.get('type') or stream_type, dict_get(encoding_option, ('name', 'id'))),
b02b960c
RA
42 'url': stream_url,
43 'width': int_or_none(encoding_option.get('width')),
44 'height': int_or_none(encoding_option.get('height')),
45 'vbr': int_or_none(bitrate.get('video')),
46 'abr': int_or_none(bitrate.get('audio')),
47 'filesize': int_or_none(stream.get('size')),
48 'protocol': 'm3u8_native' if stream_type == 'HLS' else None,
087ca2cb 49 })
b02b960c 50
c88debff 51 extract_formats(get_list('video'), 'H264')
b02b960c
RA
52 for stream_set in video_data.get('streams', []):
53 query = {}
54 for param in stream_set.get('keys', []):
55 query[param['name']] = param['value']
56 stream_type = stream_set.get('type')
57 videos = stream_set.get('videos')
58 if videos:
59 extract_formats(videos, stream_type, query)
60 elif stream_type == 'HLS':
61 stream_url = stream_set.get('source')
62 if not stream_url:
63 continue
64 formats.extend(self._extract_m3u8_formats(
65 update_url_query(stream_url, query), video_id,
66 'mp4', 'm3u8_native', m3u8_id=stream_type, fatal=False))
087ca2cb 67 self._sort_formats(formats)
6b95b065 68
c88debff
RA
69 replace_ext = lambda x, y: re.sub(self._CAPTION_EXT_RE, '.' + y, x)
70
71 def get_subs(caption_url):
72 if re.search(self._CAPTION_EXT_RE, caption_url):
73 return [{
74 'url': replace_ext(caption_url, 'ttml'),
75 }, {
76 'url': replace_ext(caption_url, 'vtt'),
77 }]
78 else:
79 return [{'url': caption_url}]
80
81 automatic_captions = {}
b02b960c 82 subtitles = {}
c88debff 83 for caption in get_list('caption'):
b02b960c
RA
84 caption_url = caption.get('source')
85 if not caption_url:
86 continue
c88debff
RA
87 sub_dict = automatic_captions if caption.get('type') == 'auto' else subtitles
88 sub_dict.setdefault(dict_get(caption, ('locale', 'language')), []).extend(get_subs(caption_url))
b02b960c 89
c88debff 90 user = meta.get('user', {})
f65dc41b 91
fb7abb31 92 return {
6b95b065 93 'id': video_id,
b02b960c 94 'title': title,
6b95b065 95 'formats': formats,
b02b960c 96 'subtitles': subtitles,
c88debff
RA
97 'automatic_captions': automatic_captions,
98 'thumbnail': try_get(meta, lambda x: x['cover']['source']),
b02b960c 99 'view_count': int_or_none(meta.get('count')),
c88debff
RA
100 'uploader_id': user.get('id'),
101 'uploader': user.get('name'),
102 'uploader_url': user.get('url'),
6b95b065 103 }
c88debff
RA
104
105
106class NaverIE(NaverBaseIE):
107 _VALID_URL = r'https?://(?:m\.)?tv(?:cast)?\.naver\.com/(?:v|embed)/(?P<id>\d+)'
108 _GEO_BYPASS = False
109 _TESTS = [{
110 'url': 'http://tv.naver.com/v/81652',
111 'info_dict': {
112 'id': '81652',
113 'ext': 'mp4',
114 'title': '[9월 모의고사 해설강의][수학_김상희] 수학 A형 16~20번',
115 'description': '메가스터디 수학 김상희 선생님이 9월 모의고사 수학A형 16번에서 20번까지 해설강의를 공개합니다.',
116 'upload_date': '20130903',
117 'uploader': '메가스터디, 합격불변의 법칙',
118 'uploader_id': 'megastudy',
119 },
120 }, {
121 'url': 'http://tv.naver.com/v/395837',
122 'md5': '8a38e35354d26a17f73f4e90094febd3',
123 'info_dict': {
124 'id': '395837',
125 'ext': 'mp4',
126 'title': '9년이 지나도 아픈 기억, 전효성의 아버지',
127 'description': 'md5:eb6aca9d457b922e43860a2a2b1984d3',
128 'upload_date': '20150519',
129 'uploader': '4가지쇼 시즌2',
130 'uploader_id': 'wrappinguser29',
131 },
132 'skip': 'Georestricted',
133 }, {
134 'url': 'http://tvcast.naver.com/v/81652',
135 'only_matching': True,
136 }]
137
138 def _real_extract(self, url):
139 video_id = self._match_id(url)
140 content = self._download_json(
141 'https://tv.naver.com/api/contents/json/v/' + video_id,
142 video_id, headers=self.geo_verification_headers())
143 player_json = content.get('playerJson') or {}
144
145 vid = player_json.get('videoId')
146 in_key = player_json.get('inKey')
147
148 if not vid or not in_key:
149 player_auth = player_json.get('playerAuth')
150 if player_auth == 'notCountry':
151 self.raise_geo_restricted(countries=['KR'])
152 elif player_auth == 'notLogin':
153 self.raise_login_required()
154 raise ExtractorError('couldn\'t extract vid and key')
155 info = self._extract_video_info(video_id, vid, in_key)
156
157 clip_info_html = content.get('clipInfoHtml')
158 if clip_info_html:
159 info['description'] = clean_html(get_element_by_class('desc', clip_info_html))
160 upload_date = self._search_regex(
161 r'<span[^>]+class="date".*?(\d{4}\.\d{2}\.\d{2})',
162 clip_info_html, 'upload date', fatal=False)
163 if upload_date:
164 info['upload_date'] = upload_date.replace('.', '')
165
166 return info