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