]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/kakao.py
Completely change project name to yt-dlp (#85)
[yt-dlp.git] / yt_dlp / extractor / kakao.py
CommitLineData
1c22d7a7
N
1# coding: utf-8
2
3from __future__ import unicode_literals
4
5from .common import InfoExtractor
f70ddd4a 6from ..compat import compat_str
1c22d7a7
N
7from ..utils import (
8 int_or_none,
274bf5e4 9 strip_or_none,
1c22d7a7
N
10 unified_timestamp,
11)
12
13
14class KakaoIE(InfoExtractor):
274bf5e4 15 _VALID_URL = r'https?://(?:play-)?tv\.kakao\.com/(?:channel/\d+|embed/player)/cliplink/(?P<id>\d+|[^?#&]+@my)'
4cd3053c
U
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?'
1c22d7a7
N
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,
274bf5e4 40 'uploader': '쇼! 음악중심',
1c22d7a7
N
41 'timestamp': 1485684628,
42 'upload_date': '20170129',
43 }
44 }]
45
46 def _real_extract(self, url):
47 video_id = self._match_id(url)
274bf5e4 48 api_base = self._API_BASE_TMPL % video_id
4cd3053c 49 cdn_api_base = self._CDN_API % video_id
f70ddd4a 50
274bf5e4 51 query = {
f70ddd4a
S
52 'player': 'monet_html5',
53 'referer': url,
54 'uuid': '',
55 'service': 'kakao_tv',
56 'section': '',
57 'dteType': 'PC',
274bf5e4
RA
58 'fields': ','.join([
59 '-*', 'tid', 'clipLink', 'displayTitle', 'clip', 'title',
60 'description', 'channelId', 'createTime', 'duration', 'playCount',
61 'likeCount', 'commentCount', 'tagList', 'channel', 'name',
e987ce4b
RA
62 'clipChapterThumbnailList', 'thumbnailUrl', 'timeInSec', 'isDefault',
63 'videoOutputList', 'width', 'height', 'kbps', 'profile', 'label'])
f70ddd4a 64 }
1c22d7a7 65
4cd3053c
U
66 api_json = self._download_json(
67 api_base, video_id, 'Downloading video info')
f70ddd4a 68
4cd3053c 69 clip_link = api_json['clipLink']
f70ddd4a
S
70 clip = clip_link['clip']
71
72 title = clip.get('title') or clip_link.get('displayTitle')
1c22d7a7 73
1c22d7a7 74 formats = []
e987ce4b 75 for fmt in clip.get('videoOutputList', []):
1c22d7a7
N
76 try:
77 profile_name = fmt['profile']
e987ce4b
RA
78 if profile_name == 'AUDIO':
79 continue
274bf5e4
RA
80 query.update({
81 'profile': profile_name,
82 'fields': '-*,url',
83 })
4cd3053c 84
1c22d7a7 85 fmt_url_json = self._download_json(
4cd3053c 86 cdn_api_base, video_id,
f70ddd4a 87 'Downloading video URL for profile %s' % profile_name,
4cd3053c 88 query=query, fatal=False)
1c22d7a7
N
89
90 if fmt_url_json is None:
91 continue
92
4cd3053c
U
93 fmt_vidLocation = fmt_url_json['videoLocation']
94 fmt_url = fmt_vidLocation['url']
1c22d7a7
N
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'),
e987ce4b
RA
101 'filesize': int_or_none(fmt.get('filesize')),
102 'tbr': int_or_none(fmt.get('kbps')),
1c22d7a7
N
103 })
104 except KeyError:
105 pass
1c22d7a7 106 self._sort_formats(formats)
1c22d7a7 107
1c22d7a7
N
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 })
f70ddd4a
S
115 top_thumbnail = clip.get('thumbnailUrl')
116 if top_thumbnail:
117 thumbs.append({
118 'url': top_thumbnail,
119 'preference': 10,
120 })
1c22d7a7 121
f70ddd4a 122 return {
4cd3053c 123 'id': video_id,
f70ddd4a 124 'title': title,
274bf5e4 125 'description': strip_or_none(clip.get('description')),
f70ddd4a
S
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,
274bf5e4 135 'tags': clip.get('tagList'),
f70ddd4a 136 }