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