[daum] Do not match a single URL with multiple info extractors
[youtube-dl] / youtube_dl / extractor / daum.py
1 # encoding: utf-8
2
3 from __future__ import unicode_literals
4
5 import re
6 import itertools
7
8 from .common import InfoExtractor
9 from ..compat import (
10     compat_parse_qs,
11     compat_urllib_parse,
12     compat_urllib_parse_unquote,
13     compat_urlparse,
14 )
15 from ..utils import (
16     int_or_none,
17     str_to_int,
18     xpath_text,
19     unescapeHTML,
20 )
21
22
23 class DaumIE(InfoExtractor):
24     _VALID_URL = r'https?://(?:(?:m\.)?tvpot\.daum\.net/v/|videofarm\.daum\.net/controller/player/VodPlayer\.swf\?vid=)(?P<id>[^?#&]+)'
25     IE_NAME = 'daum.net'
26
27     _TESTS = [{
28         'url': 'http://tvpot.daum.net/v/vab4dyeDBysyBssyukBUjBz',
29         'info_dict': {
30             'id': 'vab4dyeDBysyBssyukBUjBz',
31             'ext': 'mp4',
32             'title': '마크 헌트 vs 안토니오 실바',
33             'description': 'Mark Hunt vs Antonio Silva',
34             'upload_date': '20131217',
35             'thumbnail': 're:^https?://.*\.(?:jpg|png)',
36             'duration': 2117,
37             'view_count': int,
38             'comment_count': int,
39         },
40     }, {
41         'url': 'http://m.tvpot.daum.net/v/65139429',
42         'info_dict': {
43             'id': '65139429',
44             'ext': 'mp4',
45             'title': 'md5:a100d65d09cec246d8aa9bde7de45aed',
46             'description': 'md5:79794514261164ff27e36a21ad229fc5',
47             'upload_date': '20150604',
48             'thumbnail': 're:^https?://.*\.(?:jpg|png)',
49             'duration': 154,
50             'view_count': int,
51             'comment_count': int,
52         },
53     }, {
54         'url': 'http://tvpot.daum.net/v/07dXWRka62Y%24',
55         'only_matching': True,
56     }, {
57         'url': 'http://videofarm.daum.net/controller/player/VodPlayer.swf?vid=vwIpVpCQsT8%24&ref=',
58         'info_dict': {
59             'id': 'vwIpVpCQsT8$',
60             'ext': 'flv',
61             'title': '01-Korean War ( Trouble on the horizon )',
62             'description': '\nKorean War 01\nTrouble on the horizon\n전쟁의 먹구름',
63             'upload_date': '20080223',
64             'thumbnail': 're:^https?://.*\.(?:jpg|png)',
65             'duration': 249,
66             'view_count': int,
67             'comment_count': int,
68         },
69     }]
70
71     def _real_extract(self, url):
72         video_id = compat_urllib_parse_unquote(self._match_id(url))
73         query = compat_urllib_parse.urlencode({'vid': video_id})
74         movie_data = self._download_json(
75             'http://videofarm.daum.net/controller/api/closed/v1_2/IntegratedMovieData.json?' + query,
76             video_id, 'Downloading video formats info')
77
78         # For urls like http://m.tvpot.daum.net/v/65139429, where the video_id is really a clipid
79         if not movie_data.get('output_list', {}).get('output_list') and re.match(r'^\d+$', video_id):
80             return self.url_result('http://tvpot.daum.net/clip/ClipView.do?clipid=%s' % video_id)
81
82         info = self._download_xml(
83             'http://tvpot.daum.net/clip/ClipInfoXml.do?' + query, video_id,
84             'Downloading video info')
85
86         formats = []
87         for format_el in movie_data['output_list']['output_list']:
88             profile = format_el['profile']
89             format_query = compat_urllib_parse.urlencode({
90                 'vid': video_id,
91                 'profile': profile,
92             })
93             url_doc = self._download_xml(
94                 'http://videofarm.daum.net/controller/api/open/v1_2/MovieLocation.apixml?' + format_query,
95                 video_id, note='Downloading video data for %s format' % profile)
96             format_url = url_doc.find('result/url').text
97             formats.append({
98                 'url': format_url,
99                 'format_id': profile,
100                 'width': int_or_none(format_el.get('width')),
101                 'height': int_or_none(format_el.get('height')),
102                 'filesize': int_or_none(format_el.get('filesize')),
103             })
104         self._sort_formats(formats)
105
106         return {
107             'id': video_id,
108             'title': info.find('TITLE').text,
109             'formats': formats,
110             'thumbnail': xpath_text(info, 'THUMB_URL'),
111             'description': xpath_text(info, 'CONTENTS'),
112             'duration': int_or_none(xpath_text(info, 'DURATION')),
113             'upload_date': info.find('REGDTTM').text[:8],
114             'view_count': str_to_int(xpath_text(info, 'PLAY_CNT')),
115             'comment_count': str_to_int(xpath_text(info, 'COMMENT_CNT')),
116         }
117
118
119 class DaumClipIE(InfoExtractor):
120     _VALID_URL = r'https?://(?:m\.)?tvpot\.daum\.net/(?:clip/ClipView.(?:do|tv)|mypot/View.do)\?.*?clipid=(?P<id>\d+)'
121     IE_NAME = 'daum.net:clip'
122     _URL_TEMPLATE = 'http://tvpot.daum.net/clip/ClipView.do?clipid=%s'
123
124     _TESTS = [{
125         'url': 'http://tvpot.daum.net/clip/ClipView.do?clipid=52554690',
126         'info_dict': {
127             'id': '52554690',
128             'ext': 'mp4',
129             'title': 'DOTA 2GETHER 시즌2 6회 - 2부',
130             'description': 'DOTA 2GETHER 시즌2 6회 - 2부',
131             'upload_date': '20130831',
132             'thumbnail': 're:^https?://.*\.(?:jpg|png)',
133             'duration': 3868,
134             'view_count': int,
135         },
136     }, {
137         'url': 'http://m.tvpot.daum.net/clip/ClipView.tv?clipid=54999425',
138         'only_matching': True,
139     }]
140
141     @classmethod
142     def suitable(cls, url):
143         return False if DaumPlaylistIE.suitable(url) or DaumUserIE.suitable(url) else super(DaumClipIE, cls).suitable(url)
144
145     def _real_extract(self, url):
146         video_id = self._match_id(url)
147         clip_info = self._download_json(
148             'http://tvpot.daum.net/mypot/json/GetClipInfo.do?clipid=%s' % video_id,
149             video_id, 'Downloading clip info')['clip_bean']
150
151         return {
152             '_type': 'url_transparent',
153             'id': video_id,
154             'url': 'http://tvpot.daum.net/v/%s' % clip_info['vid'],
155             'title': unescapeHTML(clip_info['title']),
156             'thumbnail': clip_info.get('thumb_url'),
157             'description': clip_info.get('contents'),
158             'duration': int_or_none(clip_info.get('duration')),
159             'upload_date': clip_info.get('up_date')[:8],
160             'view_count': int_or_none(clip_info.get('play_count')),
161             'ie_key': 'Daum',
162         }
163
164
165 class DaumListIE(InfoExtractor):
166     def _get_entries(self, list_id, list_id_type):
167         name = None
168         entries = []
169         for pagenum in itertools.count(1):
170             list_info = self._download_json(
171                 'http://tvpot.daum.net/mypot/json/GetClipInfo.do?size=48&init=true&order=date&page=%d&%s=%s' % (
172                     pagenum, list_id_type, list_id), list_id, 'Downloading list info - %s' % pagenum)
173
174             entries.extend([
175                 self.url_result(
176                     'http://tvpot.daum.net/v/%s' % clip['vid'])
177                 for clip in list_info['clip_list']
178             ])
179
180             if not name:
181                 name = list_info.get('playlist_bean', {}).get('name') or \
182                     list_info.get('potInfo', {}).get('name')
183
184             if not list_info.get('has_more'):
185                 break
186
187         return name, entries
188
189     def _check_clip(self, url, list_id):
190         query_dict = compat_parse_qs(compat_urlparse.urlparse(url).query)
191         if 'clipid' in query_dict:
192             clip_id = query_dict['clipid'][0]
193             if self._downloader.params.get('noplaylist'):
194                 self.to_screen('Downloading just video %s because of --no-playlist' % clip_id)
195                 return self.url_result(DaumClipIE._URL_TEMPLATE % clip_id, 'DaumClip')
196             else:
197                 self.to_screen('Downloading playlist %s - add --no-playlist to just download video' % list_id)
198
199
200 class DaumPlaylistIE(DaumListIE):
201     _VALID_URL = r'https?://(?:m\.)?tvpot\.daum\.net/mypot/(?:View\.do|Top\.tv)\?.*?playlistid=(?P<id>[0-9]+)'
202     IE_NAME = 'daum.net:playlist'
203     _URL_TEMPLATE = 'http://tvpot.daum.net/mypot/View.do?playlistid=%s'
204
205     _TESTS = [{
206         'note': 'Playlist url with clipid',
207         'url': 'http://tvpot.daum.net/mypot/View.do?playlistid=6213966&clipid=73806844',
208         'info_dict': {
209             'id': '6213966',
210             'title': 'Woorissica Official',
211         },
212         'playlist_mincount': 181
213     }, {
214         'note': 'Playlist url with clipid - noplaylist',
215         'url': 'http://tvpot.daum.net/mypot/View.do?playlistid=6213966&clipid=73806844',
216         'info_dict': {
217             'id': '73806844',
218             'ext': 'mp4',
219             'title': '151017 Airport',
220             'upload_date': '20160117',
221         },
222         'params': {
223             'noplaylist': True,
224             'skip_download': True,
225         }
226     }]
227
228     @classmethod
229     def suitable(cls, url):
230         return False if DaumUserIE.suitable(url) else super(DaumPlaylistIE, cls).suitable(url)
231
232     def _real_extract(self, url):
233         list_id = self._match_id(url)
234
235         clip_result = self._check_clip(url, list_id)
236         if clip_result:
237             return clip_result
238
239         name, entries = self._get_entries(list_id, 'playlistid')
240
241         return self.playlist_result(entries, list_id, name)
242
243
244 class DaumUserIE(DaumListIE):
245     _VALID_URL = r'https?://(?:m\.)?tvpot\.daum\.net/mypot/(?:View|Top)\.do\?.*?ownerid=(?P<id>[0-9a-zA-Z]+)'
246     IE_NAME = 'daum.net:user'
247
248     _TESTS = [{
249         'url': 'http://tvpot.daum.net/mypot/View.do?ownerid=o2scDLIVbHc0',
250         'info_dict': {
251             'id': 'o2scDLIVbHc0',
252             'title': '마이 리틀 텔레비전',
253         },
254         'playlist_mincount': 213
255     }, {
256         'url': 'http://tvpot.daum.net/mypot/View.do?ownerid=o2scDLIVbHc0&clipid=73801156',
257         'info_dict': {
258             'id': '73801156',
259             'ext': 'mp4',
260             'title': '[미공개] 김구라, 오만석이 부릅니다 \'오케피\' - 마이 리틀 텔레비전 20160116',
261             'upload_date': '20160117',
262             'description': 'md5:5e91d2d6747f53575badd24bd62b9f36'
263         },
264         'params': {
265             'noplaylist': True,
266             'skip_download': True,
267         }
268     }, {
269         'note': 'Playlist url has ownerid and playlistid, playlistid takes precedence',
270         'url': 'http://tvpot.daum.net/mypot/View.do?ownerid=o2scDLIVbHc0&playlistid=6196631',
271         'info_dict': {
272             'id': '6196631',
273             'title': '마이 리틀 텔레비전 - 20160109',
274         },
275         'playlist_count': 11
276     }, {
277         'url': 'http://tvpot.daum.net/mypot/Top.do?ownerid=o2scDLIVbHc0',
278         'only_matching': True,
279     }]
280
281     def _real_extract(self, url):
282         list_id = self._match_id(url)
283
284         clip_result = self._check_clip(url, list_id)
285         if clip_result:
286             return clip_result
287
288         query_dict = compat_parse_qs(compat_urlparse.urlparse(url).query)
289         if 'playlistid' in query_dict:
290             playlist_id = query_dict['playlistid'][0]
291             return self.url_result(DaumPlaylistIE._URL_TEMPLATE % playlist_id, 'DaumPlaylist')
292
293         name, entries = self._get_entries(list_id, 'ownerid')
294
295         return self.playlist_result(entries, list_id, name)