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