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