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