]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/naver.py
Annotate changelog with bug closed.
[youtubedl] / 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 ..compat import (
8 compat_urllib_parse,
9 compat_urlparse,
10 )
11 from ..utils import (
12 ExtractorError,
13 )
14
15
16 class NaverIE(InfoExtractor):
17 _VALID_URL = r'https?://(?:m\.)?tvcast\.naver\.com/v/(?P<id>\d+)'
18
19 _TESTS = [{
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',
27 },
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 }]
40
41 def _real_extract(self, url):
42 video_id = self._match_id(url)
43 webpage = self._download_webpage(url, video_id)
44
45 m_id = re.search(r'var rmcPlayer = new nhn.rmcnmv.RMCVideoPlayer\("(.+?)", "(.+?)"',
46 webpage)
47 if m_id is None:
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)
53 raise ExtractorError('couldn\'t extract vid and key')
54 vid = m_id.group(1)
55 key = m_id.group(2)
56 query = compat_urllib_parse.urlencode({'vid': vid, 'inKey': key, })
57 query_urls = compat_urllib_parse.urlencode({
58 'masterVid': vid,
59 'protocol': 'p2p',
60 'inKey': key,
61 })
62 info = self._download_xml(
63 'http://serviceapi.rmcnmv.naver.com/flash/videoInfo.nhn?' + query,
64 video_id, 'Downloading video info')
65 urls = self._download_xml(
66 'http://serviceapi.rmcnmv.naver.com/flash/playableEncodingOption.nhn?' + query_urls,
67 video_id, 'Downloading video formats info')
68
69 formats = []
70 for format_el in urls.findall('EncodingOptions/EncodingOption'):
71 domain = format_el.find('Domain').text
72 uri = format_el.find('uri').text
73 f = {
74 'url': compat_urlparse.urljoin(domain, uri),
75 'ext': 'mp4',
76 'width': int(format_el.find('width').text),
77 'height': int(format_el.find('height').text),
78 }
79 if domain.startswith('rtmp'):
80 # urlparse does not support custom schemes
81 # https://bugs.python.org/issue18828
82 f.update({
83 'url': domain + uri,
84 'ext': 'flv',
85 'rtmp_protocol': '1', # rtmpt
86 })
87 formats.append(f)
88 self._sort_formats(formats)
89
90 return {
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 }