]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/kakao.py
8ad1d9efd8d9da0598efda2214c81e8416b0fa82
[yt-dlp.git] / yt_dlp / extractor / kakao.py
1 from .common import InfoExtractor
2 from ..compat import compat_HTTPError
3 from ..utils import (
4 ExtractorError,
5 int_or_none,
6 strip_or_none,
7 str_or_none,
8 traverse_obj,
9 unified_timestamp,
10 )
11
12
13 class KakaoIE(InfoExtractor):
14 _VALID_URL = r'https?://(?:play-)?tv\.kakao\.com/(?:channel/\d+|embed/player)/cliplink/(?P<id>\d+|[^?#&]+@my)'
15 _API_BASE_TMPL = 'http://tv.kakao.com/api/v1/ft/playmeta/cliplink/%s/'
16 _CDN_API = 'https://tv.kakao.com/katz/v1/ft/cliplink/%s/readyNplay?'
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 'description': '',
26 'uploader_id': '2671005',
27 'uploader': '그랑그랑이',
28 'timestamp': 1488160199,
29 'upload_date': '20170227',
30 'like_count': int,
31 'thumbnail': r're:http://.+/thumb\.png',
32 'tags': ['乃木坂'],
33 'view_count': int,
34 'duration': 1503,
35 'comment_count': int,
36 }
37 }, {
38 'url': 'http://tv.kakao.com/channel/2653210/cliplink/300103180',
39 'md5': 'a8917742069a4dd442516b86e7d66529',
40 'info_dict': {
41 'id': '300103180',
42 'ext': 'mp4',
43 'description': '러블리즈 - Destiny (나의 지구) (Lovelyz - Destiny)\r\n\r\n[쇼! 음악중심] 20160611, 507회',
44 'title': '러블리즈 - Destiny (나의 지구) (Lovelyz - Destiny)',
45 'uploader_id': '2653210',
46 'uploader': '쇼! 음악중심',
47 'timestamp': 1485684628,
48 'upload_date': '20170129',
49 'like_count': int,
50 'thumbnail': r're:http://.+/thumb\.png',
51 'tags': 'count:28',
52 'view_count': int,
53 'duration': 184,
54 'comment_count': int,
55 }
56 }, {
57 # geo restricted
58 'url': 'https://tv.kakao.com/channel/3643855/cliplink/412069491',
59 'only_matching': True,
60 }]
61
62 def _real_extract(self, url):
63 video_id = self._match_id(url)
64 api_base = self._API_BASE_TMPL % video_id
65 cdn_api_base = self._CDN_API % video_id
66
67 query = {
68 'player': 'monet_html5',
69 'referer': url,
70 'uuid': '',
71 'service': 'kakao_tv',
72 'section': '',
73 'dteType': 'PC',
74 'fields': ','.join([
75 '-*', 'tid', 'clipLink', 'displayTitle', 'clip', 'title',
76 'description', 'channelId', 'createTime', 'duration', 'playCount',
77 'likeCount', 'commentCount', 'tagList', 'channel', 'name',
78 'clipChapterThumbnailList', 'thumbnailUrl', 'timeInSec', 'isDefault',
79 'videoOutputList', 'width', 'height', 'kbps', 'profile', 'label'])
80 }
81
82 api_json = self._download_json(
83 api_base, video_id, 'Downloading video info')
84
85 clip_link = api_json['clipLink']
86 clip = clip_link['clip']
87
88 title = clip.get('title') or clip_link.get('displayTitle')
89
90 formats = []
91 for fmt in clip.get('videoOutputList') or []:
92 profile_name = fmt.get('profile')
93 if not profile_name or profile_name == 'AUDIO':
94 continue
95 query.update({
96 'profile': profile_name,
97 'fields': '-*,code,message,url',
98 })
99 try:
100 fmt_url_json = self._download_json(
101 cdn_api_base, video_id, query=query,
102 note='Downloading video URL for profile %s' % profile_name)
103 except ExtractorError as e:
104 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
105 resp = self._parse_json(e.cause.read().decode(), video_id)
106 if resp.get('code') == 'GeoBlocked':
107 self.raise_geo_restricted()
108
109 fmt_url = traverse_obj(fmt_url_json, ('videoLocation', 'url'))
110 if not fmt_url:
111 continue
112
113 formats.append({
114 'url': fmt_url,
115 'format_id': profile_name,
116 'width': int_or_none(fmt.get('width')),
117 'height': int_or_none(fmt.get('height')),
118 'format_note': fmt.get('label'),
119 'filesize': int_or_none(fmt.get('filesize')),
120 'tbr': int_or_none(fmt.get('kbps')),
121 })
122 self._sort_formats(formats)
123
124 thumbs = []
125 for thumb in clip.get('clipChapterThumbnailList') or []:
126 thumbs.append({
127 'url': thumb.get('thumbnailUrl'),
128 'id': str(thumb.get('timeInSec')),
129 'preference': -1 if thumb.get('isDefault') else 0
130 })
131 top_thumbnail = clip.get('thumbnailUrl')
132 if top_thumbnail:
133 thumbs.append({
134 'url': top_thumbnail,
135 'preference': 10,
136 })
137
138 return {
139 'id': video_id,
140 'title': title,
141 'description': strip_or_none(clip.get('description')),
142 'uploader': traverse_obj(clip_link, ('channel', 'name')),
143 'uploader_id': str_or_none(clip_link.get('channelId')),
144 'thumbnails': thumbs,
145 'timestamp': unified_timestamp(clip_link.get('createTime')),
146 'duration': int_or_none(clip.get('duration')),
147 'view_count': int_or_none(clip.get('playCount')),
148 'like_count': int_or_none(clip.get('likeCount')),
149 'comment_count': int_or_none(clip.get('commentCount')),
150 'formats': formats,
151 'tags': clip.get('tagList'),
152 }