[kakao] Add extractor (closes #12298)
[youtube-dl] / youtube_dl / extractor / kakao.py
1 # coding: utf-8
2
3 from __future__ import unicode_literals
4
5 from .common import InfoExtractor
6 from ..utils import (
7     int_or_none,
8     compat_str,
9     unified_timestamp,
10 )
11
12
13 class KakaoIE(InfoExtractor):
14     _VALID_URL = r'https?://tv.kakao.com/channel/(?P<channel>\d+)/cliplink/(?P<id>\d+)'
15     IE_NAME = 'kakao.com'
16
17     _TESTS = [{
18         'url': 'http://tv.kakao.com/channel/2671005/cliplink/301965083',
19         'md5': '702b2fbdeb51ad82f5c904e8c0766340',
20         'info_dict': {
21             'id': '301965083',
22             'ext': 'mp4',
23             'title': '乃木坂46 バナナマン 「3期生紹介コーナーが始動!顔高低差GPも!」 『乃木坂工事中』',
24             'uploader_id': 2671005,
25             'uploader': '그랑그랑이',
26             'timestamp': 1488160199,
27             'upload_date': '20170227',
28         }
29     }, {
30         'url': 'http://tv.kakao.com/channel/2653210/cliplink/300103180',
31         'md5': 'a8917742069a4dd442516b86e7d66529',
32         'info_dict': {
33             'id': '300103180',
34             'ext': 'mp4',
35             'description': '러블리즈 - Destiny (나의 지구) (Lovelyz - Destiny)\r\n\r\n[쇼! 음악중심] 20160611, 507회',
36             'title': '러블리즈 - Destiny (나의 지구) (Lovelyz - Destiny)',
37             'uploader_id': 2653210,
38             'uploader': '쇼 음악중심',
39             'timestamp': 1485684628,
40             'upload_date': '20170129',
41         }
42     }]
43
44     def _real_extract(self, url):
45         video_id = self._match_id(url)
46
47         player_url = 'http://tv.kakao.com/embed/player/cliplink/' + video_id + \
48             '?service=kakao_tv&autoplay=1&profile=HIGH&wmode=transparent'
49         player_header = {'Referer': player_url}
50
51         impress = self._download_json(
52             'http://tv.kakao.com/api/v1/ft/cliplinks/%s/impress' % video_id,
53             video_id, 'Downloading video info',
54             query={
55                 'player': 'monet_html5',
56                 'referer': url,
57                 'uuid': '',
58                 'service': 'kakao_tv',
59                 'section': '',
60                 'dteType': 'PC',
61                 'fields': 'clipLink,clip,channel,hasPlusFriend,-service,-tagList'
62             }, headers=player_header)
63
64         clipLink = impress['clipLink']
65         clip = clipLink['clip']
66
67         video_info = {
68             'id': video_id,
69             'title': clip['title'],
70             'description': clip.get('description'),
71             'uploader': clipLink.get('channel', {}).get('name'),
72             'uploader_id': clipLink.get('channelId'),
73             'duration': int_or_none(clip.get('duration')),
74             'view_count': int_or_none(clip.get('playCount')),
75             'like_count': int_or_none(clip.get('likeCount')),
76             'comment_count': int_or_none(clip.get('commentCount')),
77         }
78
79         tid = impress.get('tid', '')
80         raw = self._download_json(
81             'http://tv.kakao.com/api/v1/ft/cliplinks/%s/raw' % video_id,
82             video_id, 'Downloading video formats info',
83             query={
84                 'player': 'monet_html5',
85                 'referer': url,
86                 'uuid': '',
87                 'service': 'kakao_tv',
88                 'section': '',
89                 'tid': tid,
90                 'profile': 'HIGH',
91                 'dteType': 'PC',
92             }, headers=player_header, fatal=False)
93
94         formats = []
95         for fmt in raw.get('outputList', []):
96             try:
97                 profile_name = fmt['profile']
98                 fmt_url_json = self._download_json(
99                     'http://tv.kakao.com/api/v1/ft/cliplinks/%s/raw/videolocation' % video_id,
100                     video_id, 'Downloading video URL for profile %s' % profile_name,
101                     query={
102                         'service': 'kakao_tv',
103                         'section': '',
104                         'tid': tid,
105                         'profile': profile_name
106                     }, headers=player_header, fatal=False)
107
108                 if fmt_url_json is None:
109                     continue
110
111                 fmt_url = fmt_url_json['url']
112                 formats.append({
113                     'url': fmt_url,
114                     'format_id': profile_name,
115                     'width': int_or_none(fmt.get('width')),
116                     'height': int_or_none(fmt.get('height')),
117                     'format_note': fmt.get('label'),
118                     'filesize': int_or_none(fmt.get('filesize'))
119                 })
120             except KeyError:
121                 pass
122
123         self._sort_formats(formats)
124         video_info['formats'] = formats
125
126         top_thumbnail = clip.get('thumbnailUrl')
127         thumbs = []
128         for thumb in clip.get('clipChapterThumbnailList', []):
129             thumbs.append({
130                 'url': thumb.get('thumbnailUrl'),
131                 'id': compat_str(thumb.get('timeInSec')),
132                 'preference': -1 if thumb.get('isDefault') else 0
133             })
134         video_info['thumbnail'] = top_thumbnail
135         video_info['thumbnails'] = thumbs
136
137         upload_date = unified_timestamp(clipLink.get('createTime'))
138         video_info['timestamp'] = upload_date
139
140         return video_info