0
0
Fork 0
mirror of https://github.com/yt-dlp/yt-dlp.git synced 2024-11-30 03:33:02 +00:00
yt-dlp/yt_dlp/extractor/kakao.py

157 lines
5.8 KiB
Python
Raw Normal View History

2017-08-24 02:32:24 +00:00
# coding: utf-8
from __future__ import unicode_literals
from .common import InfoExtractor
from ..compat import compat_HTTPError
2017-08-24 02:32:24 +00:00
from ..utils import (
ExtractorError,
2017-08-24 02:32:24 +00:00
int_or_none,
strip_or_none,
str_or_none,
2021-08-15 08:57:44 +00:00
traverse_obj,
2017-08-24 02:32:24 +00:00
unified_timestamp,
)
class KakaoIE(InfoExtractor):
_VALID_URL = r'https?://(?:play-)?tv\.kakao\.com/(?:channel/\d+|embed/player)/cliplink/(?P<id>\d+|[^?#&]+@my)'
_API_BASE_TMPL = 'http://tv.kakao.com/api/v1/ft/playmeta/cliplink/%s/'
_CDN_API = 'https://tv.kakao.com/katz/v1/ft/cliplink/%s/readyNplay?'
2017-08-24 02:32:24 +00:00
_TESTS = [{
'url': 'http://tv.kakao.com/channel/2671005/cliplink/301965083',
'md5': '702b2fbdeb51ad82f5c904e8c0766340',
'info_dict': {
'id': '301965083',
'ext': 'mp4',
'title': '乃木坂46 バナナマン 「3期生紹介コーナーが始動顔高低差GPも」 『乃木坂工事中』',
'description': '',
'uploader_id': '2671005',
2017-08-24 02:32:24 +00:00
'uploader': '그랑그랑이',
'timestamp': 1488160199,
'upload_date': '20170227',
'like_count': int,
'thumbnail': r're:http://.+/thumb\.png',
'tags': ['乃木坂'],
'view_count': int,
'duration': 1503,
'comment_count': int,
2017-08-24 02:32:24 +00:00
}
}, {
'url': 'http://tv.kakao.com/channel/2653210/cliplink/300103180',
'md5': 'a8917742069a4dd442516b86e7d66529',
'info_dict': {
'id': '300103180',
'ext': 'mp4',
'description': '러블리즈 - Destiny (나의 지구) (Lovelyz - Destiny)\r\n\r\n[쇼! 음악중심] 20160611, 507회',
'title': '러블리즈 - Destiny (나의 지구) (Lovelyz - Destiny)',
'uploader_id': '2653210',
'uploader': '쇼! 음악중심',
2017-08-24 02:32:24 +00:00
'timestamp': 1485684628,
'upload_date': '20170129',
'like_count': int,
'thumbnail': r're:http://.+/thumb\.png',
'tags': 'count:28',
'view_count': int,
'duration': 184,
'comment_count': int,
2017-08-24 02:32:24 +00:00
}
}, {
# geo restricted
'url': 'https://tv.kakao.com/channel/3643855/cliplink/412069491',
'only_matching': True,
2017-08-24 02:32:24 +00:00
}]
def _real_extract(self, url):
video_id = self._match_id(url)
api_base = self._API_BASE_TMPL % video_id
cdn_api_base = self._CDN_API % video_id
2017-09-23 00:25:15 +00:00
query = {
2017-09-23 00:25:15 +00:00
'player': 'monet_html5',
'referer': url,
'uuid': '',
'service': 'kakao_tv',
'section': '',
'dteType': 'PC',
'fields': ','.join([
'-*', 'tid', 'clipLink', 'displayTitle', 'clip', 'title',
'description', 'channelId', 'createTime', 'duration', 'playCount',
'likeCount', 'commentCount', 'tagList', 'channel', 'name',
'clipChapterThumbnailList', 'thumbnailUrl', 'timeInSec', 'isDefault',
'videoOutputList', 'width', 'height', 'kbps', 'profile', 'label'])
2017-09-23 00:25:15 +00:00
}
2017-08-24 02:32:24 +00:00
api_json = self._download_json(
api_base, video_id, 'Downloading video info')
2017-09-23 00:25:15 +00:00
clip_link = api_json['clipLink']
2017-09-23 00:25:15 +00:00
clip = clip_link['clip']
title = clip.get('title') or clip_link.get('displayTitle')
2017-08-24 02:32:24 +00:00
formats = []
for fmt in clip.get('videoOutputList') or []:
2021-08-15 08:57:44 +00:00
profile_name = fmt.get('profile')
if not profile_name or profile_name == 'AUDIO':
continue
query.update({
'profile': profile_name,
'fields': '-*,code,message,url',
2021-08-15 08:57:44 +00:00
})
try:
fmt_url_json = self._download_json(
cdn_api_base, video_id, query=query,
note='Downloading video URL for profile %s' % profile_name)
except ExtractorError as e:
if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
resp = self._parse_json(e.cause.read().decode(), video_id)
if resp.get('code') == 'GeoBlocked':
self.raise_geo_restricted()
2017-08-24 02:32:24 +00:00
2021-08-15 08:57:44 +00:00
fmt_url = traverse_obj(fmt_url_json, ('videoLocation', 'url'))
if not fmt_url:
continue
formats.append({
'url': fmt_url,
'format_id': profile_name,
'width': int_or_none(fmt.get('width')),
'height': int_or_none(fmt.get('height')),
'format_note': fmt.get('label'),
'filesize': int_or_none(fmt.get('filesize')),
'tbr': int_or_none(fmt.get('kbps')),
})
2017-08-24 02:32:24 +00:00
self._sort_formats(formats)
thumbs = []
2021-08-15 08:57:44 +00:00
for thumb in clip.get('clipChapterThumbnailList') or []:
2017-08-24 02:32:24 +00:00
thumbs.append({
'url': thumb.get('thumbnailUrl'),
'id': str(thumb.get('timeInSec')),
2017-08-24 02:32:24 +00:00
'preference': -1 if thumb.get('isDefault') else 0
})
2017-09-23 00:25:15 +00:00
top_thumbnail = clip.get('thumbnailUrl')
if top_thumbnail:
thumbs.append({
'url': top_thumbnail,
'preference': 10,
})
2017-08-24 02:32:24 +00:00
2017-09-23 00:25:15 +00:00
return {
'id': video_id,
2017-09-23 00:25:15 +00:00
'title': title,
'description': strip_or_none(clip.get('description')),
2021-08-15 08:57:44 +00:00
'uploader': traverse_obj(clip_link, ('channel', 'name')),
'uploader_id': str_or_none(clip_link.get('channelId')),
2017-09-23 00:25:15 +00:00
'thumbnails': thumbs,
'timestamp': unified_timestamp(clip_link.get('createTime')),
'duration': int_or_none(clip.get('duration')),
'view_count': int_or_none(clip.get('playCount')),
'like_count': int_or_none(clip.get('likeCount')),
'comment_count': int_or_none(clip.get('commentCount')),
'formats': formats,
'tags': clip.get('tagList'),
2017-09-23 00:25:15 +00:00
}