Merge pull request #1956 from dstftw/master
[youtube-dl] / youtube_dl / extractor / naver.py
1 # encoding: utf-8
2 import re
3
4 from .common import InfoExtractor
5 from ..utils import (
6     compat_urllib_parse,
7     ExtractorError,
8 )
9
10
11 class NaverIE(InfoExtractor):
12     _VALID_URL = r'https?://(?:m\.)?tvcast\.naver\.com/v/(?P<id>\d+)'
13
14     _TEST = {
15         u'url': u'http://tvcast.naver.com/v/81652',
16         u'file': u'81652.mp4',
17         u'info_dict': {
18             u'title': u'[9월 모의고사 해설강의][수학_김상희] 수학 A형 16~20번',
19             u'description': u'합격불변의 법칙 메가스터디 | 메가스터디 수학 김상희 선생님이 9월 모의고사 수학A형 16번에서 20번까지 해설강의를 공개합니다.',
20             u'upload_date': u'20130903',
21         },
22     }
23
24     def _real_extract(self, url):
25         mobj = re.match(self._VALID_URL, url)
26         video_id = mobj.group(1)
27         webpage = self._download_webpage(url, video_id)
28         m_id = re.search(r'var rmcPlayer = new nhn.rmcnmv.RMCVideoPlayer\("(.+?)", "(.+?)"',
29             webpage)
30         if m_id is None:
31             raise ExtractorError(u'couldn\'t extract vid and key')
32         vid = m_id.group(1)
33         key = m_id.group(2)
34         query = compat_urllib_parse.urlencode({'vid': vid, 'inKey': key,})
35         query_urls = compat_urllib_parse.urlencode({
36             'masterVid': vid,
37             'protocol': 'p2p',
38             'inKey': key,
39         })
40         info = self._download_xml(
41             'http://serviceapi.rmcnmv.naver.com/flash/videoInfo.nhn?' + query,
42             video_id, u'Downloading video info')
43         urls = self._download_xml(
44             'http://serviceapi.rmcnmv.naver.com/flash/playableEncodingOption.nhn?' + query_urls,
45             video_id, u'Downloading video formats info')
46
47         formats = []
48         for format_el in urls.findall('EncodingOptions/EncodingOption'):
49             domain = format_el.find('Domain').text
50             if domain.startswith('rtmp'):
51                 continue
52             formats.append({
53                 'url': domain + format_el.find('uri').text,
54                 'ext': 'mp4',
55                 'width': int(format_el.find('width').text),
56                 'height': int(format_el.find('height').text),
57             })
58
59         return {
60             'id': video_id,
61             'title': info.find('Subject').text,
62             'formats': formats,
63             'description': self._og_search_description(webpage),
64             'thumbnail': self._og_search_thumbnail(webpage),
65             'upload_date': info.find('WriteDate').text.replace('.', ''),
66             'view_count': int(info.find('PlayCount').text),
67         }