]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/naver.py
Merge pull request #8061 from dstftw/introduce-chapter-and-series-fields
[yt-dlp.git] / youtube_dl / extractor / naver.py
CommitLineData
6b95b065 1# encoding: utf-8
24da5893
JMF
2from __future__ import unicode_literals
3
6b95b065 4import re
6b95b065
JMF
5
6from .common import InfoExtractor
1cc79574 7from ..compat import (
6b95b065 8 compat_urllib_parse,
f8d5e1cf 9 compat_urlparse,
1cc79574
PH
10)
11from ..utils import (
6b95b065
JMF
12 ExtractorError,
13)
14
15
16class NaverIE(InfoExtractor):
24b173fa 17 _VALID_URL = r'https?://(?:m\.)?tvcast\.naver\.com/v/(?P<id>\d+)'
6b95b065 18
f8d5e1cf 19 _TESTS = [{
24da5893
JMF
20 'url': 'http://tvcast.naver.com/v/81652',
21 'info_dict': {
22 'id': '81652',
23 'ext': 'mp4',
24 'title': '[9월 모의고사 해설강의][수학_김상희] 수학 A형 16~20번',
25 'description': '합격불변의 법칙 메가스터디 | 메가스터디 수학 김상희 선생님이 9월 모의고사 수학A형 16번에서 20번까지 해설강의를 공개합니다.',
26 'upload_date': '20130903',
6b95b065 27 },
f8d5e1cf
YCH
28 }, {
29 'url': 'http://tvcast.naver.com/v/395837',
30 'md5': '638ed4c12012c458fefcddfd01f173cd',
31 'info_dict': {
32 'id': '395837',
33 'ext': 'mp4',
34 'title': '9년이 지나도 아픈 기억, 전효성의 아버지',
35 'description': 'md5:5bf200dcbf4b66eb1b350d1eb9c753f7',
36 'upload_date': '20150519',
37 },
38 'skip': 'Georestricted',
39 }]
6b95b065
JMF
40
41 def _real_extract(self, url):
1cc79574 42 video_id = self._match_id(url)
6b95b065 43 webpage = self._download_webpage(url, video_id)
1cc79574 44
6b95b065 45 m_id = re.search(r'var rmcPlayer = new nhn.rmcnmv.RMCVideoPlayer\("(.+?)", "(.+?)"',
9e1a5b84 46 webpage)
6b95b065 47 if m_id is None:
f540b937
S
48 error = self._html_search_regex(
49 r'(?s)<div class="(?:nation_error|nation_box|error_box)">\s*(?:<!--.*?-->)?\s*<p class="[^"]+">(?P<msg>.+?)</p>\s*</div>',
50 webpage, 'error', default=None)
51 if error:
52 raise ExtractorError(error, expected=True)
24da5893 53 raise ExtractorError('couldn\'t extract vid and key')
6b95b065
JMF
54 vid = m_id.group(1)
55 key = m_id.group(2)
5f6a1245 56 query = compat_urllib_parse.urlencode({'vid': vid, 'inKey': key, })
6b95b065
JMF
57 query_urls = compat_urllib_parse.urlencode({
58 'masterVid': vid,
59 'protocol': 'p2p',
60 'inKey': key,
61 })
e26f8712 62 info = self._download_xml(
6b95b065 63 'http://serviceapi.rmcnmv.naver.com/flash/videoInfo.nhn?' + query,
24da5893 64 video_id, 'Downloading video info')
e26f8712 65 urls = self._download_xml(
6b95b065 66 'http://serviceapi.rmcnmv.naver.com/flash/playableEncodingOption.nhn?' + query_urls,
24da5893 67 video_id, 'Downloading video formats info')
6b95b065
JMF
68
69 formats = []
70 for format_el in urls.findall('EncodingOptions/EncodingOption'):
71 domain = format_el.find('Domain').text
f8d5e1cf 72 uri = format_el.find('uri').text
087ca2cb 73 f = {
f8d5e1cf 74 'url': compat_urlparse.urljoin(domain, uri),
6b95b065
JMF
75 'ext': 'mp4',
76 'width': int(format_el.find('width').text),
77 'height': int(format_el.find('height').text),
087ca2cb
JMF
78 }
79 if domain.startswith('rtmp'):
f8d5e1cf
YCH
80 # urlparse does not support custom schemes
81 # https://bugs.python.org/issue18828
087ca2cb 82 f.update({
f8d5e1cf 83 'url': domain + uri,
087ca2cb 84 'ext': 'flv',
5f6a1245 85 'rtmp_protocol': '1', # rtmpt
087ca2cb
JMF
86 })
87 formats.append(f)
88 self._sort_formats(formats)
6b95b065 89
fb7abb31 90 return {
6b95b065
JMF
91 'id': video_id,
92 'title': info.find('Subject').text,
93 'formats': formats,
94 'description': self._og_search_description(webpage),
95 'thumbnail': self._og_search_thumbnail(webpage),
96 'upload_date': info.find('WriteDate').text.replace('.', ''),
97 'view_count': int(info.find('PlayCount').text),
98 }