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