PEP8: applied even more rules
[youtube-dl] / 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 ..utils import (
8     compat_urllib_parse,
9     ExtractorError,
10     clean_html,
11 )
12
13
14 class NaverIE(InfoExtractor):
15     _VALID_URL = r'https?://(?:m\.)?tvcast\.naver\.com/v/(?P<id>\d+)'
16
17     _TEST = {
18         'url': 'http://tvcast.naver.com/v/81652',
19         'info_dict': {
20             'id': '81652',
21             'ext': 'mp4',
22             'title': '[9월 모의고사 해설강의][수학_김상희] 수학 A형 16~20번',
23             'description': '합격불변의 법칙 메가스터디 | 메가스터디 수학 김상희 선생님이 9월 모의고사 수학A형 16번에서 20번까지 해설강의를 공개합니다.',
24             'upload_date': '20130903',
25         },
26     }
27
28     def _real_extract(self, url):
29         mobj = re.match(self._VALID_URL, url)
30         video_id = mobj.group(1)
31         webpage = self._download_webpage(url, video_id)
32         m_id = re.search(r'var rmcPlayer = new nhn.rmcnmv.RMCVideoPlayer\("(.+?)", "(.+?)"',
33                          webpage)
34         if m_id is None:
35             m_error = re.search(
36                 r'(?s)<div class="nation_error">\s*(?:<!--.*?-->)?\s*<p class="[^"]+">(?P<msg>.+?)</p>\s*</div>',
37                 webpage)
38             if m_error:
39                 raise ExtractorError(clean_html(m_error.group('msg')), expected=True)
40             raise ExtractorError('couldn\'t extract vid and key')
41         vid = m_id.group(1)
42         key = m_id.group(2)
43         query = compat_urllib_parse.urlencode({'vid': vid, 'inKey': key, })
44         query_urls = compat_urllib_parse.urlencode({
45             'masterVid': vid,
46             'protocol': 'p2p',
47             'inKey': key,
48         })
49         info = self._download_xml(
50             'http://serviceapi.rmcnmv.naver.com/flash/videoInfo.nhn?' + query,
51             video_id, 'Downloading video info')
52         urls = self._download_xml(
53             'http://serviceapi.rmcnmv.naver.com/flash/playableEncodingOption.nhn?' + query_urls,
54             video_id, 'Downloading video formats info')
55
56         formats = []
57         for format_el in urls.findall('EncodingOptions/EncodingOption'):
58             domain = format_el.find('Domain').text
59             f = {
60                 'url': domain + format_el.find('uri').text,
61                 'ext': 'mp4',
62                 'width': int(format_el.find('width').text),
63                 'height': int(format_el.find('height').text),
64             }
65             if domain.startswith('rtmp'):
66                 f.update({
67                     'ext': 'flv',
68                     'rtmp_protocol': '1',  # rtmpt
69                 })
70             formats.append(f)
71         self._sort_formats(formats)
72
73         return {
74             'id': video_id,
75             'title': info.find('Subject').text,
76             'formats': formats,
77             'description': self._og_search_description(webpage),
78             'thumbnail': self._og_search_thumbnail(webpage),
79             'upload_date': info.find('WriteDate').text.replace('.', ''),
80             'view_count': int(info.find('PlayCount').text),
81         }