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