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