b5c310ccb8042c7bfa44c6a909ead398fc679dd4
[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_unquote,
12     compat_urllib_parse_urlencode,
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': '1297회, \'아빠 아들로 태어나길 잘 했어\' 민수, 감동의 눈물[아빠 어디가] 20150118',
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         # Requires dte_type=WEB (#9972)
71         'url': 'http://tvpot.daum.net/v/s3794Uf1NZeZ1qMpGpeqeRU',
72         'md5': 'a8917742069a4dd442516b86e7d66529',
73         'info_dict': {
74             'id': 's3794Uf1NZeZ1qMpGpeqeRU',
75             'ext': 'mp4',
76             'title': '러블리즈 - Destiny (나의 지구) (Lovelyz - Destiny) [쇼! 음악중심] 508회 20160611',
77             'description': '러블리즈 - Destiny (나의 지구) (Lovelyz - Destiny)\n\n[쇼! 음악중심] 20160611, 507회',
78             'upload_date': '20160611',
79         },
80     }]
81
82     def _real_extract(self, url):
83         video_id = compat_urllib_parse_unquote(self._match_id(url))
84         movie_data = self._download_json(
85             'http://videofarm.daum.net/controller/api/closed/v1_2/IntegratedMovieData.json',
86             video_id, 'Downloading video formats info', query={'vid': video_id, 'dte_type': 'WEB'})
87
88         # For urls like http://m.tvpot.daum.net/v/65139429, where the video_id is really a clipid
89         if not movie_data.get('output_list', {}).get('output_list') and re.match(r'^\d+$', video_id):
90             return self.url_result('http://tvpot.daum.net/clip/ClipView.do?clipid=%s' % video_id)
91
92         info = self._download_xml(
93             'http://tvpot.daum.net/clip/ClipInfoXml.do', video_id,
94             'Downloading video info', query={'vid': video_id})
95
96         formats = []
97         for format_el in movie_data['output_list']['output_list']:
98             profile = format_el['profile']
99             format_query = compat_urllib_parse_urlencode({
100                 'vid': video_id,
101                 'profile': profile,
102             })
103             url_doc = self._download_xml(
104                 'http://videofarm.daum.net/controller/api/open/v1_2/MovieLocation.apixml?' + format_query,
105                 video_id, note='Downloading video data for %s format' % profile)
106             format_url = url_doc.find('result/url').text
107             formats.append({
108                 'url': format_url,
109                 'format_id': profile,
110                 'width': int_or_none(format_el.get('width')),
111                 'height': int_or_none(format_el.get('height')),
112                 'filesize': int_or_none(format_el.get('filesize')),
113             })
114         self._sort_formats(formats)
115
116         return {
117             'id': video_id,
118             'title': info.find('TITLE').text,
119             'formats': formats,
120             'thumbnail': xpath_text(info, 'THUMB_URL'),
121             'description': xpath_text(info, 'CONTENTS'),
122             'duration': int_or_none(xpath_text(info, 'DURATION')),
123             'upload_date': info.find('REGDTTM').text[:8],
124             'view_count': str_to_int(xpath_text(info, 'PLAY_CNT')),
125             'comment_count': str_to_int(xpath_text(info, 'COMMENT_CNT')),
126         }
127
128
129 class DaumClipIE(InfoExtractor):
130     _VALID_URL = r'https?://(?:m\.)?tvpot\.daum\.net/(?:clip/ClipView.(?:do|tv)|mypot/View.do)\?.*?clipid=(?P<id>\d+)'
131     IE_NAME = 'daum.net:clip'
132     _URL_TEMPLATE = 'http://tvpot.daum.net/clip/ClipView.do?clipid=%s'
133
134     _TESTS = [{
135         'url': 'http://tvpot.daum.net/clip/ClipView.do?clipid=52554690',
136         'info_dict': {
137             'id': '52554690',
138             'ext': 'mp4',
139             'title': 'DOTA 2GETHER 시즌2 6회 - 2부',
140             'description': 'DOTA 2GETHER 시즌2 6회 - 2부',
141             'upload_date': '20130831',
142             'thumbnail': 're:^https?://.*\.(?:jpg|png)',
143             'duration': 3868,
144             'view_count': int,
145         },
146     }, {
147         'url': 'http://m.tvpot.daum.net/clip/ClipView.tv?clipid=54999425',
148         'only_matching': True,
149     }]
150
151     @classmethod
152     def suitable(cls, url):
153         return False if DaumPlaylistIE.suitable(url) or DaumUserIE.suitable(url) else super(DaumClipIE, cls).suitable(url)
154
155     def _real_extract(self, url):
156         video_id = self._match_id(url)
157         clip_info = self._download_json(
158             'http://tvpot.daum.net/mypot/json/GetClipInfo.do?clipid=%s' % video_id,
159             video_id, 'Downloading clip info')['clip_bean']
160
161         return {
162             '_type': 'url_transparent',
163             'id': video_id,
164             'url': 'http://tvpot.daum.net/v/%s' % clip_info['vid'],
165             'title': unescapeHTML(clip_info['title']),
166             'thumbnail': clip_info.get('thumb_url'),
167             'description': clip_info.get('contents'),
168             'duration': int_or_none(clip_info.get('duration')),
169             'upload_date': clip_info.get('up_date')[:8],
170             'view_count': int_or_none(clip_info.get('play_count')),
171             'ie_key': 'Daum',
172         }
173
174
175 class DaumListIE(InfoExtractor):
176     def _get_entries(self, list_id, list_id_type):
177         name = None
178         entries = []
179         for pagenum in itertools.count(1):
180             list_info = self._download_json(
181                 'http://tvpot.daum.net/mypot/json/GetClipInfo.do?size=48&init=true&order=date&page=%d&%s=%s' % (
182                     pagenum, list_id_type, list_id), list_id, 'Downloading list info - %s' % pagenum)
183
184             entries.extend([
185                 self.url_result(
186                     'http://tvpot.daum.net/v/%s' % clip['vid'])
187                 for clip in list_info['clip_list']
188             ])
189
190             if not name:
191                 name = list_info.get('playlist_bean', {}).get('name') or \
192                     list_info.get('potInfo', {}).get('name')
193
194             if not list_info.get('has_more'):
195                 break
196
197         return name, entries
198
199     def _check_clip(self, url, list_id):
200         query_dict = compat_parse_qs(compat_urlparse.urlparse(url).query)
201         if 'clipid' in query_dict:
202             clip_id = query_dict['clipid'][0]
203             if self._downloader.params.get('noplaylist'):
204                 self.to_screen('Downloading just video %s because of --no-playlist' % clip_id)
205                 return self.url_result(DaumClipIE._URL_TEMPLATE % clip_id, 'DaumClip')
206             else:
207                 self.to_screen('Downloading playlist %s - add --no-playlist to just download video' % list_id)
208
209
210 class DaumPlaylistIE(DaumListIE):
211     _VALID_URL = r'https?://(?:m\.)?tvpot\.daum\.net/mypot/(?:View\.do|Top\.tv)\?.*?playlistid=(?P<id>[0-9]+)'
212     IE_NAME = 'daum.net:playlist'
213     _URL_TEMPLATE = 'http://tvpot.daum.net/mypot/View.do?playlistid=%s'
214
215     _TESTS = [{
216         'note': 'Playlist url with clipid',
217         'url': 'http://tvpot.daum.net/mypot/View.do?playlistid=6213966&clipid=73806844',
218         'info_dict': {
219             'id': '6213966',
220             'title': 'Woorissica Official',
221         },
222         'playlist_mincount': 181
223     }, {
224         'note': 'Playlist url with clipid - noplaylist',
225         'url': 'http://tvpot.daum.net/mypot/View.do?playlistid=6213966&clipid=73806844',
226         'info_dict': {
227             'id': '73806844',
228             'ext': 'mp4',
229             'title': '151017 Airport',
230             'upload_date': '20160117',
231         },
232         'params': {
233             'noplaylist': True,
234             'skip_download': True,
235         }
236     }]
237
238     @classmethod
239     def suitable(cls, url):
240         return False if DaumUserIE.suitable(url) else super(DaumPlaylistIE, cls).suitable(url)
241
242     def _real_extract(self, url):
243         list_id = self._match_id(url)
244
245         clip_result = self._check_clip(url, list_id)
246         if clip_result:
247             return clip_result
248
249         name, entries = self._get_entries(list_id, 'playlistid')
250
251         return self.playlist_result(entries, list_id, name)
252
253
254 class DaumUserIE(DaumListIE):
255     _VALID_URL = r'https?://(?:m\.)?tvpot\.daum\.net/mypot/(?:View|Top)\.(?:do|tv)\?.*?ownerid=(?P<id>[0-9a-zA-Z]+)'
256     IE_NAME = 'daum.net:user'
257
258     _TESTS = [{
259         'url': 'http://tvpot.daum.net/mypot/View.do?ownerid=o2scDLIVbHc0',
260         'info_dict': {
261             'id': 'o2scDLIVbHc0',
262             'title': '마이 리틀 텔레비전',
263         },
264         'playlist_mincount': 213
265     }, {
266         'url': 'http://tvpot.daum.net/mypot/View.do?ownerid=o2scDLIVbHc0&clipid=73801156',
267         'info_dict': {
268             'id': '73801156',
269             'ext': 'mp4',
270             'title': '[미공개] 김구라, 오만석이 부릅니다 \'오케피\' - 마이 리틀 텔레비전 20160116',
271             'upload_date': '20160117',
272             'description': 'md5:5e91d2d6747f53575badd24bd62b9f36'
273         },
274         'params': {
275             'noplaylist': True,
276             'skip_download': True,
277         }
278     }, {
279         'note': 'Playlist url has ownerid and playlistid, playlistid takes precedence',
280         'url': 'http://tvpot.daum.net/mypot/View.do?ownerid=o2scDLIVbHc0&playlistid=6196631',
281         'info_dict': {
282             'id': '6196631',
283             'title': '마이 리틀 텔레비전 - 20160109',
284         },
285         'playlist_count': 11
286     }, {
287         'url': 'http://tvpot.daum.net/mypot/Top.do?ownerid=o2scDLIVbHc0',
288         'only_matching': True,
289     }, {
290         'url': 'http://m.tvpot.daum.net/mypot/Top.tv?ownerid=45x1okb1If50&playlistid=3569733',
291         'only_matching': True,
292     }]
293
294     def _real_extract(self, url):
295         list_id = self._match_id(url)
296
297         clip_result = self._check_clip(url, list_id)
298         if clip_result:
299             return clip_result
300
301         query_dict = compat_parse_qs(compat_urlparse.urlparse(url).query)
302         if 'playlistid' in query_dict:
303             playlist_id = query_dict['playlistid'][0]
304             return self.url_result(DaumPlaylistIE._URL_TEMPLATE % playlist_id, 'DaumPlaylist')
305
306         name, entries = self._get_entries(list_id, 'ownerid')
307
308         return self.playlist_result(entries, list_id, name)