]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/kakao.py
[kakao] Fix extractor
[yt-dlp.git] / yt_dlp / 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 strip_or_none,
10 traverse_obj,
11 unified_timestamp,
12 )
13
14
15 class KakaoIE(InfoExtractor):
16 _VALID_URL = r'https?://(?:play-)?tv\.kakao\.com/(?:channel/\d+|embed/player)/cliplink/(?P<id>\d+|[^?#&]+@my)'
17 _API_BASE_TMPL = 'http://tv.kakao.com/api/v1/ft/playmeta/cliplink/%s/'
18 _CDN_API = 'https://tv.kakao.com/katz/v1/ft/cliplink/%s/readyNplay?'
19
20 _TESTS = [{
21 'url': 'http://tv.kakao.com/channel/2671005/cliplink/301965083',
22 'md5': '702b2fbdeb51ad82f5c904e8c0766340',
23 'info_dict': {
24 'id': '301965083',
25 'ext': 'mp4',
26 'title': '乃木坂46 バナナマン 「3期生紹介コーナーが始動!顔高低差GPも!」 『乃木坂工事中』',
27 'uploader_id': 2671005,
28 'uploader': '그랑그랑이',
29 'timestamp': 1488160199,
30 'upload_date': '20170227',
31 }
32 }, {
33 'url': 'http://tv.kakao.com/channel/2653210/cliplink/300103180',
34 'md5': 'a8917742069a4dd442516b86e7d66529',
35 'info_dict': {
36 'id': '300103180',
37 'ext': 'mp4',
38 'description': '러블리즈 - Destiny (나의 지구) (Lovelyz - Destiny)\r\n\r\n[쇼! 음악중심] 20160611, 507회',
39 'title': '러블리즈 - Destiny (나의 지구) (Lovelyz - Destiny)',
40 'uploader_id': 2653210,
41 'uploader': '쇼! 음악중심',
42 'timestamp': 1485684628,
43 'upload_date': '20170129',
44 }
45 }]
46
47 def _real_extract(self, url):
48 video_id = self._match_id(url)
49 api_base = self._API_BASE_TMPL % video_id
50 cdn_api_base = self._CDN_API % video_id
51
52 query = {
53 'player': 'monet_html5',
54 'referer': url,
55 'uuid': '',
56 'service': 'kakao_tv',
57 'section': '',
58 'dteType': 'PC',
59 'fields': ','.join([
60 '-*', 'tid', 'clipLink', 'displayTitle', 'clip', 'title',
61 'description', 'channelId', 'createTime', 'duration', 'playCount',
62 'likeCount', 'commentCount', 'tagList', 'channel', 'name',
63 'clipChapterThumbnailList', 'thumbnailUrl', 'timeInSec', 'isDefault',
64 'videoOutputList', 'width', 'height', 'kbps', 'profile', 'label'])
65 }
66
67 api_json = self._download_json(
68 api_base, video_id, 'Downloading video info')
69
70 clip_link = api_json['clipLink']
71 clip = clip_link['clip']
72
73 title = clip.get('title') or clip_link.get('displayTitle')
74
75 formats = []
76 for fmt in clip.get('videoOutputList', []):
77 profile_name = fmt.get('profile')
78 if not profile_name or profile_name == 'AUDIO':
79 continue
80 query.update({
81 'profile': profile_name,
82 'fields': '-*,url',
83 })
84
85 fmt_url_json = self._download_json(
86 cdn_api_base, video_id,
87 'Downloading video URL for profile %s' % profile_name,
88 query=query, fatal=False)
89 fmt_url = traverse_obj(fmt_url_json, ('videoLocation', 'url'))
90 if not fmt_url:
91 continue
92
93 formats.append({
94 'url': fmt_url,
95 'format_id': profile_name,
96 'width': int_or_none(fmt.get('width')),
97 'height': int_or_none(fmt.get('height')),
98 'format_note': fmt.get('label'),
99 'filesize': int_or_none(fmt.get('filesize')),
100 'tbr': int_or_none(fmt.get('kbps')),
101 })
102 self._sort_formats(formats)
103
104 thumbs = []
105 for thumb in clip.get('clipChapterThumbnailList') or []:
106 thumbs.append({
107 'url': thumb.get('thumbnailUrl'),
108 'id': compat_str(thumb.get('timeInSec')),
109 'preference': -1 if thumb.get('isDefault') else 0
110 })
111 top_thumbnail = clip.get('thumbnailUrl')
112 if top_thumbnail:
113 thumbs.append({
114 'url': top_thumbnail,
115 'preference': 10,
116 })
117
118 return {
119 'id': video_id,
120 'title': title,
121 'description': strip_or_none(clip.get('description')),
122 'uploader': traverse_obj(clip_link, ('channel', 'name')),
123 'uploader_id': clip_link.get('channelId'),
124 'thumbnails': thumbs,
125 'timestamp': unified_timestamp(clip_link.get('createTime')),
126 'duration': int_or_none(clip.get('duration')),
127 'view_count': int_or_none(clip.get('playCount')),
128 'like_count': int_or_none(clip.get('likeCount')),
129 'comment_count': int_or_none(clip.get('commentCount')),
130 'formats': formats,
131 'tags': clip.get('tagList'),
132 }