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