]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/naver.py
Imported Upstream version 2015.01.16
[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 )
10 from ..utils import (
11 ExtractorError,
12 clean_html,
13 )
14
15
16 class NaverIE(InfoExtractor):
17 _VALID_URL = r'https?://(?:m\.)?tvcast\.naver\.com/v/(?P<id>\d+)'
18
19 _TEST = {
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
30 def _real_extract(self, url):
31 video_id = self._match_id(url)
32 webpage = self._download_webpage(url, video_id)
33
34 m_id = re.search(r'var rmcPlayer = new nhn.rmcnmv.RMCVideoPlayer\("(.+?)", "(.+?)"',
35 webpage)
36 if m_id is None:
37 m_error = re.search(
38 r'(?s)<div class="nation_error">\s*(?:<!--.*?-->)?\s*<p class="[^"]+">(?P<msg>.+?)</p>\s*</div>',
39 webpage)
40 if m_error:
41 raise ExtractorError(clean_html(m_error.group('msg')), expected=True)
42 raise ExtractorError('couldn\'t extract vid and key')
43 vid = m_id.group(1)
44 key = m_id.group(2)
45 query = compat_urllib_parse.urlencode({'vid': vid, 'inKey': key, })
46 query_urls = compat_urllib_parse.urlencode({
47 'masterVid': vid,
48 'protocol': 'p2p',
49 'inKey': key,
50 })
51 info = self._download_xml(
52 'http://serviceapi.rmcnmv.naver.com/flash/videoInfo.nhn?' + query,
53 video_id, 'Downloading video info')
54 urls = self._download_xml(
55 'http://serviceapi.rmcnmv.naver.com/flash/playableEncodingOption.nhn?' + query_urls,
56 video_id, 'Downloading video formats info')
57
58 formats = []
59 for format_el in urls.findall('EncodingOptions/EncodingOption'):
60 domain = format_el.find('Domain').text
61 f = {
62 'url': domain + format_el.find('uri').text,
63 'ext': 'mp4',
64 'width': int(format_el.find('width').text),
65 'height': int(format_el.find('height').text),
66 }
67 if domain.startswith('rtmp'):
68 f.update({
69 'ext': 'flv',
70 'rtmp_protocol': '1', # rtmpt
71 })
72 formats.append(f)
73 self._sort_formats(formats)
74
75 return {
76 'id': video_id,
77 'title': info.find('Subject').text,
78 'formats': formats,
79 'description': self._og_search_description(webpage),
80 'thumbnail': self._og_search_thumbnail(webpage),
81 'upload_date': info.find('WriteDate').text.replace('.', ''),
82 'view_count': int(info.find('PlayCount').text),
83 }