Use the new '_download_xml' helper in more extractors
[youtube-dl] / youtube_dl / extractor / daum.py
1 # encoding: utf-8
2 import re
3
4 from .common import InfoExtractor
5 from ..utils import (
6     compat_urllib_parse,
7     determine_ext,
8 )
9
10
11 class DaumIE(InfoExtractor):
12     _VALID_URL = r'https?://tvpot\.daum\.net/.*?clipid=(?P<id>\d+)'
13     IE_NAME = u'daum.net'
14
15     _TEST = {
16         u'url': u'http://tvpot.daum.net/clip/ClipView.do?clipid=52554690',
17         u'file': u'52554690.mp4',
18         u'info_dict': {
19             u'title': u'DOTA 2GETHER 시즌2 6회 - 2부',
20             u'description': u'DOTA 2GETHER 시즌2 6회 - 2부',
21             u'upload_date': u'20130831',
22             u'duration': 3868,
23         },
24     }
25
26     def _real_extract(self, url):
27         mobj = re.match(self._VALID_URL, url)
28         video_id = mobj.group(1)
29         canonical_url = 'http://tvpot.daum.net/v/%s' % video_id
30         webpage = self._download_webpage(canonical_url, video_id)
31         full_id = self._search_regex(r'<link rel="video_src" href=".+?vid=(.+?)"',
32             webpage, u'full id')
33         query = compat_urllib_parse.urlencode({'vid': full_id})
34         info = self._download_xml(
35             'http://tvpot.daum.net/clip/ClipInfoXml.do?' + query, video_id,
36             u'Downloading video info')
37         urls = self._download_xml(
38             'http://videofarm.daum.net/controller/api/open/v1_2/MovieData.apixml?' + query,
39             video_id, u'Downloading video formats info')
40
41         self.to_screen(u'%s: Getting video urls' % video_id)
42         formats = []
43         for format_el in urls.findall('result/output_list/output_list'):
44             profile = format_el.attrib['profile']
45             format_query = compat_urllib_parse.urlencode({
46                 'vid': full_id,
47                 'profile': profile,
48             })
49             url_doc = self._download_xml(
50                 'http://videofarm.daum.net/controller/api/open/v1_2/MovieLocation.apixml?' + format_query,
51                 video_id, note=False)
52             format_url = url_doc.find('result/url').text
53             formats.append({
54                 'url': format_url,
55                 'ext': determine_ext(format_url),
56                 'format_id': profile,
57             })
58
59         info = {
60             'id': video_id,
61             'title': info.find('TITLE').text,
62             'formats': formats,
63             'thumbnail': self._og_search_thumbnail(webpage),
64             'description': info.find('CONTENTS').text,
65             'duration': int(info.find('DURATION').text),
66             'upload_date': info.find('REGDTTM').text[:8],
67         }
68         # TODO: Remove when #980 has been merged
69         info.update(formats[-1])
70         return info