]> jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/naver.py
[naver] improve extraction(closes #8096)
[yt-dlp.git] / youtube_dl / extractor / naver.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8 ExtractorError,
9 int_or_none,
10 update_url_query,
11 )
12
13
14 class NaverIE(InfoExtractor):
15 _VALID_URL = r'https?://(?:m\.)?tvcast\.naver\.com/v/(?P<id>\d+)'
16
17 _TESTS = [{
18 'url': 'http://tvcast.naver.com/v/81652',
19 'info_dict': {
20 'id': '81652',
21 'ext': 'mp4',
22 'title': '[9월 모의고사 해설강의][수학_김상희] 수학 A형 16~20번',
23 'description': '합격불변의 법칙 메가스터디 | 메가스터디 수학 김상희 선생님이 9월 모의고사 수학A형 16번에서 20번까지 해설강의를 공개합니다.',
24 },
25 }, {
26 'url': 'http://tvcast.naver.com/v/395837',
27 'md5': '638ed4c12012c458fefcddfd01f173cd',
28 'info_dict': {
29 'id': '395837',
30 'ext': 'mp4',
31 'title': '9년이 지나도 아픈 기억, 전효성의 아버지',
32 'description': 'md5:5bf200dcbf4b66eb1b350d1eb9c753f7',
33 },
34 'skip': 'Georestricted',
35 }]
36
37 def _real_extract(self, url):
38 video_id = self._match_id(url)
39 webpage = self._download_webpage(url, video_id)
40
41 m_id = re.search(r'var rmcPlayer = new nhn.rmcnmv.RMCVideoPlayer\("(.+?)", "(.+?)"',
42 webpage)
43 if m_id is None:
44 error = self._html_search_regex(
45 r'(?s)<div class="(?:nation_error|nation_box|error_box)">\s*(?:<!--.*?-->)?\s*<p class="[^"]+">(?P<msg>.+?)</p>\s*</div>',
46 webpage, 'error', default=None)
47 if error:
48 raise ExtractorError(error, expected=True)
49 raise ExtractorError('couldn\'t extract vid and key')
50 video_data = self._download_json('http://play.rmcnmv.naver.com/vod/play/v2.0/' + m_id.group(1), video_id, query={
51 'key': m_id.group(2),
52 })
53 meta = video_data['meta']
54 title = meta['subject']
55 formats = []
56
57 def extract_formats(streams, stream_type, query={}):
58 for stream in streams:
59 stream_url = stream.get('source')
60 if not stream_url:
61 continue
62 stream_url = update_url_query(stream_url, query)
63 encoding_option = stream.get('encodingOption', {})
64 bitrate = stream.get('bitrate', {})
65 formats.append({
66 'format_id': '%s_%s' % (stream.get('type') or stream_type, encoding_option.get('id') or encoding_option.get('name')),
67 'url': stream_url,
68 'width': int_or_none(encoding_option.get('width')),
69 'height': int_or_none(encoding_option.get('height')),
70 'vbr': int_or_none(bitrate.get('video')),
71 'abr': int_or_none(bitrate.get('audio')),
72 'filesize': int_or_none(stream.get('size')),
73 'protocol': 'm3u8_native' if stream_type == 'HLS' else None,
74 })
75
76 extract_formats(video_data.get('videos', {}).get('list', []), 'H264')
77 for stream_set in video_data.get('streams', []):
78 query = {}
79 for param in stream_set.get('keys', []):
80 query[param['name']] = param['value']
81 stream_type = stream_set.get('type')
82 videos = stream_set.get('videos')
83 if videos:
84 extract_formats(videos, stream_type, query)
85 elif stream_type == 'HLS':
86 stream_url = stream_set.get('source')
87 if not stream_url:
88 continue
89 formats.extend(self._extract_m3u8_formats(
90 update_url_query(stream_url, query), video_id,
91 'mp4', 'm3u8_native', m3u8_id=stream_type, fatal=False))
92 self._sort_formats(formats)
93
94 subtitles = {}
95 for caption in video_data.get('captions', {}).get('list', []):
96 caption_url = caption.get('source')
97 if not caption_url:
98 continue
99 subtitles.setdefault(caption.get('language') or caption.get('locale'), []).append({
100 'url': caption_url,
101 })
102
103 return {
104 'id': video_id,
105 'title': title,
106 'formats': formats,
107 'subtitles': subtitles,
108 'description': self._og_search_description(webpage),
109 'thumbnail': meta.get('cover', {}).get('source') or self._og_search_thumbnail(webpage),
110 'view_count': int_or_none(meta.get('count')),
111 }