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