Merge branch 'daum' of https://github.com/remitamine/youtube-dl into remitamine-daum
[youtube-dl] / youtube_dl / extractor / daum.py
1 # encoding: utf-8
2
3 from __future__ import unicode_literals
4
5 from .common import InfoExtractor
6 from ..compat import compat_urllib_parse
7 from ..utils import (
8     int_or_none,
9     str_to_int,
10     xpath_text,
11 )
12
13
14 class DaumIE(InfoExtractor):
15     _VALID_URL = r'https?://(?:m\.)?tvpot\.daum\.net/v/(?P<id>[^?#&]+)'
16     IE_NAME = 'daum.net'
17
18     _TESTS = [{
19         'url': 'http://tvpot.daum.net/v/vab4dyeDBysyBssyukBUjBz',
20         'info_dict': {
21             'id': 'vab4dyeDBysyBssyukBUjBz',
22             'ext': 'mp4',
23             'title': '마크 헌트 vs 안토니오 실바',
24             'description': 'Mark Hunt vs Antonio Silva',
25             'upload_date': '20131217',
26             'duration': 2117,
27             'view_count': int,
28             'comment_count': int,
29         },
30     }, {
31         'url': 'http://tvpot.daum.net/v/07dXWRka62Y%24',
32         'only_matching': True,
33     }]
34
35     def _real_extract(self, url):
36         video_id = self._match_id(url)
37         query = compat_urllib_parse.urlencode({'vid': video_id})
38         info = self._download_xml(
39             'http://tvpot.daum.net/clip/ClipInfoXml.do?' + query, video_id,
40             'Downloading video info')
41         movie_data = self._download_json(
42             'http://videofarm.daum.net/controller/api/closed/v1_2/IntegratedMovieData.json?' + query,
43             video_id, 'Downloading video formats info')
44
45         formats = []
46         for format_el in movie_data['output_list']['output_list']:
47             profile = format_el['profile']
48             format_query = compat_urllib_parse.urlencode({
49                 'vid': video_id,
50                 'profile': profile,
51             })
52             url_doc = self._download_xml(
53                 'http://videofarm.daum.net/controller/api/open/v1_2/MovieLocation.apixml?' + format_query,
54                 video_id, note='Downloading video data for %s format' % profile)
55             format_url = url_doc.find('result/url').text
56             formats.append({
57                 'url': format_url,
58                 'format_id': profile,
59                 'width': int_or_none(format_el.get('width')),
60                 'height': int_or_none(format_el.get('height')),
61                 'filesize': int_or_none(format_el.get('filesize')),
62             })
63         self._sort_formats(formats)
64
65         return {
66             'id': video_id,
67             'title': info.find('TITLE').text,
68             'formats': formats,
69             'thumbnail': xpath_text(info, 'THUMB_URL'),
70             'description': xpath_text(info, 'CONTENTS'),
71             'duration': int_or_none(xpath_text(info, 'DURATION')),
72             'upload_date': info.find('REGDTTM').text[:8],
73             'view_count': str_to_int(xpath_text(info, 'PLAY_CNT')),
74             'comment_count': str_to_int(xpath_text(info, 'COMMENT_CNT')),
75         }
76
77
78 class DaumClipIE(InfoExtractor):
79     _VALID_URL = r'https?://(?:m\.)?tvpot\.daum\.net/(?:clip/ClipView.do|mypot/View.do)\?.*?clipid=(?P<id>\d+)'
80     IE_NAME = 'daum.net'
81
82     _TESTS = [{
83         'url': 'http://tvpot.daum.net/clip/ClipView.do?clipid=52554690',
84         'info_dict': {
85             'id': '52554690',
86             'ext': 'mp4',
87             'title': 'DOTA 2GETHER 시즌2 6회 - 2부',
88             'description': 'DOTA 2GETHER 시즌2 6회 - 2부',
89             'upload_date': '20130831',
90             'duration': 3868,
91             'view_count': int,
92         },
93     }]
94
95     def _real_extract(self, url):
96         video_id = self._match_id(url)
97         clip_info = self._download_json(
98             'http://tvpot.daum.net/mypot/json/GetClipInfo.do?clipid=%s' % video_id,
99             video_id, 'Downloading clip info')['clip_bean']
100
101         return {
102             '_type': 'url_transparent',
103             'id': video_id,
104             'url': 'http://tvpot.daum.net/v/%s' % clip_info['vid'],
105             'title': clip_info['title'],
106             'thumbnail': clip_info.get('thumb_url'),
107             'description': clip_info.get('contents'),
108             'duration': int_or_none(clip_info.get('duration')),
109             'upload_date': clip_info.get('up_date')[:8],
110             'view_count': int_or_none(clip_info.get('play_count')),
111             'ie_key': 'Daum',
112         }