]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/kakao.py
[misc] Add `hatch`, `ruff`, `pre-commit` and improve dev docs (#7409)
[yt-dlp.git] / yt_dlp / extractor / kakao.py
CommitLineData
1c22d7a7 1from .common import InfoExtractor
3d2623a8 2from ..networking.exceptions import HTTPError
1c22d7a7 3from ..utils import (
cb3c5682 4 ExtractorError,
1c22d7a7 5 int_or_none,
cb3c5682 6 str_or_none,
e897bd82 7 strip_or_none,
f8fabc99 8 traverse_obj,
1c22d7a7
N
9 unified_timestamp,
10)
11
12
13class KakaoIE(InfoExtractor):
274bf5e4 14 _VALID_URL = r'https?://(?:play-)?tv\.kakao\.com/(?:channel/\d+|embed/player)/cliplink/(?P<id>\d+|[^?#&]+@my)'
4cd3053c
U
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?'
1c22d7a7
N
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も!」 『乃木坂工事中』',
cb3c5682 25 'description': '',
26 'uploader_id': '2671005',
1c22d7a7
N
27 'uploader': '그랑그랑이',
28 'timestamp': 1488160199,
29 'upload_date': '20170227',
cb3c5682 30 'like_count': int,
31 'thumbnail': r're:http://.+/thumb\.png',
32 'tags': ['乃木坂'],
33 'view_count': int,
34 'duration': 1503,
35 'comment_count': int,
1c22d7a7
N
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)',
cb3c5682 45 'uploader_id': '2653210',
274bf5e4 46 'uploader': '쇼! 음악중심',
1c22d7a7
N
47 'timestamp': 1485684628,
48 'upload_date': '20170129',
cb3c5682 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,
1c22d7a7 55 }
cb3c5682 56 }, {
57 # geo restricted
58 'url': 'https://tv.kakao.com/channel/3643855/cliplink/412069491',
59 'only_matching': True,
1c22d7a7
N
60 }]
61
62 def _real_extract(self, url):
63 video_id = self._match_id(url)
274bf5e4 64 api_base = self._API_BASE_TMPL % video_id
4cd3053c 65 cdn_api_base = self._CDN_API % video_id
f70ddd4a 66
274bf5e4 67 query = {
f70ddd4a
S
68 'player': 'monet_html5',
69 'referer': url,
70 'uuid': '',
71 'service': 'kakao_tv',
72 'section': '',
73 'dteType': 'PC',
274bf5e4
RA
74 'fields': ','.join([
75 '-*', 'tid', 'clipLink', 'displayTitle', 'clip', 'title',
76 'description', 'channelId', 'createTime', 'duration', 'playCount',
77 'likeCount', 'commentCount', 'tagList', 'channel', 'name',
e987ce4b
RA
78 'clipChapterThumbnailList', 'thumbnailUrl', 'timeInSec', 'isDefault',
79 'videoOutputList', 'width', 'height', 'kbps', 'profile', 'label'])
f70ddd4a 80 }
1c22d7a7 81
4cd3053c
U
82 api_json = self._download_json(
83 api_base, video_id, 'Downloading video info')
f70ddd4a 84
4cd3053c 85 clip_link = api_json['clipLink']
f70ddd4a
S
86 clip = clip_link['clip']
87
88 title = clip.get('title') or clip_link.get('displayTitle')
1c22d7a7 89
1c22d7a7 90 formats = []
cb3c5682 91 for fmt in clip.get('videoOutputList') or []:
f8fabc99 92 profile_name = fmt.get('profile')
93 if not profile_name or profile_name == 'AUDIO':
94 continue
95 query.update({
96 'profile': profile_name,
cb3c5682 97 'fields': '-*,code,message,url',
f8fabc99 98 })
cb3c5682 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:
3d2623a8 104 if isinstance(e.cause, HTTPError) and e.cause.status == 403:
105 resp = self._parse_json(e.cause.response.read().decode(), video_id)
cb3c5682 106 if resp.get('code') == 'GeoBlocked':
107 self.raise_geo_restricted()
3d3bb168 108 raise
1c22d7a7 109
f8fabc99 110 fmt_url = traverse_obj(fmt_url_json, ('videoLocation', 'url'))
111 if not fmt_url:
112 continue
113
114 formats.append({
115 'url': fmt_url,
116 'format_id': profile_name,
117 'width': int_or_none(fmt.get('width')),
118 'height': int_or_none(fmt.get('height')),
119 'format_note': fmt.get('label'),
120 'filesize': int_or_none(fmt.get('filesize')),
121 'tbr': int_or_none(fmt.get('kbps')),
122 })
1c22d7a7 123
1c22d7a7 124 thumbs = []
f8fabc99 125 for thumb in clip.get('clipChapterThumbnailList') or []:
1c22d7a7
N
126 thumbs.append({
127 'url': thumb.get('thumbnailUrl'),
cb3c5682 128 'id': str(thumb.get('timeInSec')),
1c22d7a7
N
129 'preference': -1 if thumb.get('isDefault') else 0
130 })
f70ddd4a
S
131 top_thumbnail = clip.get('thumbnailUrl')
132 if top_thumbnail:
133 thumbs.append({
134 'url': top_thumbnail,
135 'preference': 10,
136 })
1c22d7a7 137
f70ddd4a 138 return {
4cd3053c 139 'id': video_id,
f70ddd4a 140 'title': title,
274bf5e4 141 'description': strip_or_none(clip.get('description')),
f8fabc99 142 'uploader': traverse_obj(clip_link, ('channel', 'name')),
cb3c5682 143 'uploader_id': str_or_none(clip_link.get('channelId')),
f70ddd4a
S
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,
274bf5e4 151 'tags': clip.get('tagList'),
f70ddd4a 152 }